Eight kinds of sorting algorithms that must be known "Java implementation" (b) Select sort, insert sort, Hill algorithm "detailed"

Source: Internet
Author: User

First, select the sort

   1. Basic Ideas: In the set of numbers to be sorted, select the smallest number to exchange with the first position, and then in the remaining number, find the smallest and second position of the number of exchanges, so loop to the penultimate number and the last number comparison.2. Example

  3. Algorithm implementation

/*** Select sorting algorithm * Find the smallest element in an unordered sequence, place it at the start of the sort sequence * and continue looking for the smallest element from the remaining unsorted elements, then at the end of the sort sequence.      * And so on until all elements are sorted. * @paramnumbers*/     Public Static voidSelectsort (int[] numbers) {    intsize = Numbers.length;//Array Length    inttemp = 0;//Intermediate Variables         for(inti = 0; i < size; i++)    {        intK = i;//location to be determined//Select the number that should be in the first position         for(intj = size-1; J > i; j--)        {        if(Numbers[j] <Numbers[k]) {k=J; }        }        //Exchange two numberstemp =Numbers[i]; Numbers[i]=Numbers[k]; NUMBERS[K]=temp; }    }

Second, insert sort

  1, the basic idea : Each step of a record to be sorted, by its sequential code size into the previous sorted word sequence of the appropriate position (from the back to find a suitable position) until the full insertion sort.

  2. Example

  

  3. Algorithm implementation

     /*** Insert Sort * * Starting with the first element, the element can be thought to have been sorted * Take out the next element, scan from backward forward in the ordered sequence of elements * if the element (sorted) is greater than the new element, move the element to the next position * Repeat step 3 until you find the sorted element is less than or equal to the position of the new element * Insert the new element into the position * Repeat step 2 *@paramnumbers array to sort*/       Public Static voidInsertsort (int[] numbers) {    intSize =numbers.length; inttemp = 0 ; intj = 0;  for(inti = 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).

Third, Hill algorithm

1. Basic ideas:

First, the entire sequence of records to be sorted into a number of sub-sequences of the direct insertion of the order, the entire series of records in the "Basic order", then the whole record is inserted in order.

2, Operation Method:

    1. Select an incremental sequence T1,T2,...,TK, where ti>tj,tk=1;
    2. According to the number of increment series K, the sequence is sorted by K-trip;
    3. Each order, according to the corresponding increment ti, the backlog sequence is divided into several sub-sequences of length m, respectively, the sub-table is directly inserted sort. Only the increment factor is 1 o'clock, the entire sequence is treated as a table, and the length of the table is the length of the entire sequence.

Example of a hill sort:

3. Algorithm implementation:

/**the principle of hill sorting: If you want the result to be arranged from large to small, it will first group the array, then move the larger value to the front, the smaller value * to the back, and finally the entire array to be inserted, so that the number of data exchanges and moves is reduced by the insertion sort in the beginning. It can be said that hill sort is enhanced * version of Insert sort * Take array 5, 2, 8, 9, 1, 3,4, array length is 7, when increment is 3 o'clock, array is divided into two sequences * 5,2,8 and 9,1,3,4, first sort, 9 and 5 comparisons, 1 and 2 comparisons, 3 and 8 comparisons, 4 and below Comparison of array values of small increment * This example is arranged from large to small, so the large will be in the front, after the first sorted array is 9, 2, 8, 5, 1, 3,4 * The first time after the increment value becomes 3/2=1, at this time the array to insert sort, * Implementing arrays from large to small rows*/     Public Static voidShellsort (int[] data) {        intj = 0; inttemp = 0; //Shorten the stride length to half the original one at a time         for(intincrement = DATA.LENGTH/2; Increment > 0; Increment/= 2)        {         for(inti = increment; i < data.length; i++) {Temp=Data[i];  for(j = i; J >= increment; J-=increment) {            if(Temp > Data[j-increment])//If you want to change from small to big row only here{Data[j]= Data[j-increment]; }            Else            {                 Break; }} Data[j]=temp; }            }    }

4. Efficiency

Time complexity: O (n^2).

4. Time complexity of various algorithms

  

Thanks: Thank you for your patience and reading!

Eight kinds of sorting algorithms that must be known "Java implementation" (b) Select sort, insert sort, Hill algorithm "detailed"

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.