Three simple sorting algorithms (Java Implementation)

Source: Internet
Author: User

First, bubble sort

Algorithm idea: traverse the array to be sorted, each traversal compares the adjacent two elements, if their permutation order wrong to exchange their position, after a trip to sort, the largest element will float the end of the array. Repeat until the sort is complete.

Sample Demo:

Algorithm implementation:

1  for(inti=0;i<array.length-1;i++) {//up to sort n-1 times2              for(intj=0;j<array.length-i-1;j++) {//number of exchanges required3                 if(array[j]>array[j+1]){4                     inttemp=Array[j];5Array[j]=array[j+1];6array[j+1]=temp;7                 }8             }9}

Algorithm time complexity: O (N2) Outer loop needs to compare n-1 times, inner Loop needs to compare n times.

Second, choose the sort

Algorithm idea: Select a minimum element in the array to be sorted, and swap it with the element at the first position of the array. Then select a minimal element from the remaining elements, swap it with the element in the second position, and if the smallest element is the element at that location, swap it with itself, and so on, until the sort is complete.

Sample Demo:

Algorithm implementation:

1  for(inti=0;i<array.length;i++){2             intmin=i;3              for(intj=i+1;j<array.length;j++){4                 if(array[j]<Array[min]) {5min=J;6                 }7             } 8             inttemp=Array[min];9array[min]=Array[i];Tenarray[i]=temp; One}

Time complexity: O (n2) requires N2/2 comparison and N-Times Exchange

Third, insert sort

Algorithm idea: Iterate from the second element of the array, compare the element to the preceding element, if the element is smaller than the previous element, save the element in a temporary variable, move the previous element back, and then insert the element in the appropriate position. Each time the sort is completed, the elements on the left side of the index must be ordered, but can also be moved. For arrays with fewer inversion, the algorithm is more efficient in sorting.

Note: Inverted: 5 3 6 2 The inverted entry is 5-3 5-2 3-2 6-2

Sample Demo:

Algorithm implementation:

 for (int i=1;i<array.length;i++) {            for (int j=i;j>0&&array[j] <array[j-1];j--) {                int temp=array[j];                ARRAY[J]=array[j-1];                Array[j-1]=temp;            }        }

Time complexity: O (n2) worst case N2/2 times comparison, N2/2 Exchange best case N-1 comparison, 0 Exchange

Note: Rookie one, there is an incorrect place to welcome correct.

Three simple sorting algorithms (Java Implementation)

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.