Algorithm _ Basic sorting algorithm bubble sort, select sort, insert sort and hill sort

Source: Internet
Author: User
Tags comparable

The ordered elements implement the comparable interface to achieve versatility.

The most basic sort is bubble sort, here's the idea:

    1. Compares the adjacent elements. If the first one is bigger than the second one, swap them both.
    2. Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. At this point, the last element should be the maximum number.
    3. Repeat the above steps for all elements, except for the last one.
    4. Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.

Here is its implementation code:

  

 Public classMaopao { Public voidsort (comparable[] a) {intn=a.length;  for(inti=0;i<n;i++) {             for(intj=0;j<n-i-1;j++) {                if(Less (a[j+1],a[j]) {Exch (a,j+1, J); }            }        }    }     Public Static voidExch (comparable[] A,intIintmin) {Comparable T=A[i]; A[i]=A[min]; A[min]=T; }     Public Static BooleanLess (comparable V, comparable W) {returnV.compareto (W) <0; }}

The bubbling sort, in the best case (i.e. already sequenced case) requires N2/2 comparison, 0 exchanges. In the worst case (sequence elements in reverse order), it takes N2/2 times to compare N2/2 times, and the efficiency is very low.

The idea of selecting a sort is as follows:

    1. The smallest element in the array is first found, and then the first element is swapped with the position. (If the first element is the smallest element, swap it with yourself)
    2. Then, in the remaining elements, find the smallest element and swap it with the second element in the array.
    3. So again and again, until the array is sorted.

The code is as follows:

  

 Public classSelection { Public Static voidsort (comparable[] a) {intn=a.length;  for(inti=0;i<n;i++) {            intmin=i;  for(intj=i+1;j<n;j++) {                if(Less (a[j],a[min])) min=j;//(N-1) + (N-2) +...+1 times comparison n*n/2 times Exchange} exch (A,i,min); //There's an Exchange, N-Times.        }    }     Public Static voidExch (comparable[] A,intIintmin) {Comparable T=A[i]; A[i]=A[min]; A[min]=T; }     Public Static BooleanLess (comparable V, comparable W) {returnV.compareto (W) <0; }}

For an array of length n, the selection order takes approximately N2/2 and N-times. There are two distinct features of the selection sort: Runtime and input Independent: an already ordered array and a randomly arranged array of the same size will take as long. Data movement is minimal: each interchange changes the value of two array elements , so the selection is sorted with N-times exchange.

The idea of inserting a sort is as follows:

Can be understood by organizing poker: One by one, each card, inserted into other already ordered in the appropriate position of the cards

    1. Start with the second element of the array, insert it into the preceding element
    2. If the current element is smaller than the previous element, the two elements are swapped for the position, otherwise, the loop is stopped, and the elements in front of the index are all sorted
    3. So back and forth until the array is sorted. When the index reaches the right end of the array, the sort is done.

Here's the code:

  

 Public classInsertion { Public Static voidsort (comparable[] a) {intn=a.length;  for(inti=1;i<n;i++) {             for(intJ=i;j>0&&less (A[j],a[j-1]); j--) {Exch (a,j,j-1); }        }    }     Public Static voidExch (comparable[] A,intIintmin) {Comparable T=A[i]; A[i]=A[min]; A[min]=T; }     Public Static BooleanLess (comparable V, comparable W) {returnV.compareto (W) <0; }}

Best case: (Array already ordered): Swap 0 times, N-1, worst case: (array in reverse order): Swap: ~N2/2; the number of comparisons and exchanges is the same. The average insertion order requires a ~N2/4 comparison and a ~N2/4-second interchange.

Hill sort is an algorithm based on the insertion sort, and the idea of the hill sort is to swap the nonadjacent elements and sort the array parts, and eventually sort the locally ordered array with an insert sort.

The hill sort makes the elements of any interval k in the array orderly, the implementation of the Hill sort requires the H sub-array to be sorted independently for each h, and then the H is reduced by a certain percentage until it is 1. Hill sort weighed the size and order of the sub-array, and each sub-array was very short at the beginning of the sequence, The sub-array is ordered after sorting, both of which are good for inserting the sort. The code for the hill sort is as follows:

 Public classShell { Public Static voidsort (comparable[] a) {intn=a.length; intK=1;  while(K&LT;N/3) k=3*k+1;  while(k>=1) {             for(inti=k;i<n;i++) {                 for(intJ=i;j>=k&&less (A[j],a[j-k]); j-=k) {exch (a,j,j-k); }} k=k/3; Continuously decreases k, when K is 1, is insert sort}} Public Static voidExch (comparable[] A,intIintmin) {Comparable T=A[i]; A[i]=A[min]; A[min]=T; }     Public Static BooleanLess (comparable V, comparable W) {returnV.compareto (W) <0; }}

Algorithm _ Basic sorting algorithm bubble sort, select sort, insert sort and hill sort

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.