Eight sort algorithms that must be known [java Implementation] (2) Select sorting, insert sorting, and Hill algorithm [Detailed description ].

Source: Internet
Author: User

Eight sort algorithms that must be known [java Implementation] (2) Select sorting, insert sorting, and Hill algorithm [Detailed description ].

1. Select sorting

   1. Basic Ideas: In the number of a group to be sorted, the smallest one is selected and the number at the first position is exchanged. Then, the smallest one is found in the remaining number and the number at the second position is exchanged, this loops until the second to last number is compared with the last number. 2. Instance

 

  3. Algorithm Implementation

/***** Select the Sorting Algorithm * Find the minimum element in the unordered sequence, store it to the start position of the sorting sequence * and continue searching for the minimum element from the remaining unordered elements, and put it at the end of the sorting sequence. * Similarly, until all elements are sorted. * @ Param numbers */public static void selectSort (int [] numbers) {int size = numbers. length; // array length int temp = 0; // The intermediate variable for (int I = 0; I <size; I ++) {int k = I; // The location to be determined // select the number of I-th positions for (int j = size-1; j> I; j --) {if (numbers [j] <numbers [k]) {k = j ;}// exchange two numbers temp = numbers [I]; numbers [I] = numbers [k]; numbers [k] = temp ;}}

 

 

Ii. Insert sorting

  1. Basic Ideas: Each step inserts a record to be sorted into the proper position of the sorted word sequence based on the size of its sequence code (after finding the proper position from the back to the Front ), until all inserts are sorted.

  2. Instance

  

  3. Algorithm Implementation

/***** Insert sorting ** starts from the first element. This element can be considered to have been sorted. * The next element is retrieved, scan the sorted element sequence from the back to the front * If the element (sorted) is greater than the new element, move the element to the next position * repeat Step 3, until you find the location where the sorted element is less than or equal to the new element * Insert the new element to this location * repeat Step 2 * @ param numbers array to be sorted */public static void insertSort (int [] numbers) {int size = numbers. length; int temp = 0; int j = 0; for (int I = 0; I <size; I ++) {temp = numbers [I]; // If temp is smaller than the previous value, move the previous value back for (j = I; j> 0 & temp <numbers [J-1]; j --) {numbers [j] = numbers [J-1];} numbers [j] = temp ;}}

  

4. Efficiency:

Time Complexity: O (n ^ 2 ).

 

 

Iii. Hill Algorithm

1. Basic Ideas:

First, the record sequence to be sorted is divided into several sub-sequences for direct insertion and sorting. When the record in the whole sequence is "basic order", all the records are inserted and sorted in sequence.

2. Operation Method:

Example of hill sorting:

 3,Algorithm Implementation:

/** Principle of hill sorting: as required, if you want to arrange the results in ascending to smallest order, it first groups the array and then moves the greater limit to the front, A smaller value * is moved to the backend, and the entire array is inserted and sorted. This reduces the number of data exchanges and movements compared to the insertion sorting at the beginning, it can be said that the hill sorting is enhanced * The insertion sorting * takes the array 5, 2, 8, 9, 1, 3, and 4, the array length is 7, when the increment is 3, arrays are divided into two sequences: * 5, 2, 8, 9, 1, 3, 4, first sort, 9 and 5 Comparison, 1 and 2 Comparison, 3 and 8 comparison, 4. Compare it with the array values with smaller increment values * In this example, the values are arranged from large to small, so the values are ranked first. After the first sorting, the arrays are 9, 2, 8, 5, 1, 3, 4 * after the first time, the value of increment changes to 3/2 = 1. Insert and sort the array, * implement array from large to small */public static void shellSort (int [] data) {int j = 0; int temp = 0; // reduce the step size to half of the original for (int increment = data. length/2; increment> 0; increment/= 2) {for (int I = increment; I <data. length; I ++) {temp = data [I]; for (j = I; j> = increment; j-= increment) {if (temp> data [j-increment]) // you only need to modify the data from small to large. {data [j] = data [j-increment];} else {break ;}} data [j] = temp ;}}}

 

4. Efficiency

Time Complexity: O (n ^ 2 ).

 

 

4. time complexity of various algorithms

  

 

Bubble sort, Quick Sort To view: http://www.cnblogs.com/0201zcr/p/4763806.html

Merge Sorting, heap sorting can be viewed: http://www.cnblogs.com/0201zcr/p/4764705.html

Thank you! Thank you for your patience!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.