Various sorting algorithms

Source: Internet
Author: User

First, bubble sort

If all are sorted, the required number of keyword comparisons [n-1] and record moves [0] have reached the minimum, time complexity O (n)

If all is reversed, the required number of keyword comparisons [n (n-1)/2] and record move times [3*n* (n-1)/2] reach maximum, time complexity O (n^2)

Basic idea:

1, the first number and the second number comparison, if the first is larger than the second, the exchange position, the second and the third comparison, repeat, the first comparison N-1 times

2, repeat n-i times, each time put the largest number in the last

1  Public classBubblesort2 {3      Public voidSortint[] a)4     {5         inttemp = 0;6          for(inti = a.length-1; i > 0; --i)7         {8              for(intj = 0; J < I; ++j)9             {Ten                 if(A[j + 1] <A[j]) One                 { Atemp =A[j]; -A[J] = a[j + 1]; -A[j + 1] =temp; the                 } -             } -         } -     } +}

Cons: Low efficiency (need to compare N (n-1)/2 times)

Second, choose the sort

If all are sorted, the required number of keyword comparisons [n-1] and record moves [0] have reached the minimum, time complexity O (n)

If all is reversed, the required number of keyword comparisons [n (n-1)/2] and record move times [n] are up to maximum, time complexity O (n^2)

Basic idea:

1, each cycle to find the most value, no hurry to exchange, put to each cycle end and then Exchange

1 voidSelectsort (RecordType r[],intLength/*simple selection of record array r, length is the number of records to be sorted*/2 {3     inttemp;4      for(i=0; i< length-1; i++)//n-1 Tour Sort by5     {6         intIndex=i;//assume that the corresponding array element at index is the smallest7          for(intj=i+1; J < length; J + +)//find the location of the smallest record8             if(R[j].key <R[index].key)9index=J;Ten         if(index!=i)//if the first element in the unordered area is not the smallest element in the unordered area, the interchange One         { Atemp =R[i]; -R[i] =R[index]; -R[index] =temp; the         } -     } -}

Bubble sort and select sort comparison:

The bubble sort method is 22 in turn, and does the exchange, the number of exchanges more.
The selection sort method is to find the maximum value for Each loop, and the maximum value is adjusted to the appropriate position at the end of the loop, and the number of exchanges is less.

Time complexity is the same [O (n^2)], but the transposition operation, select Sort is O (n), bubble sort is O (N^2/2)

Iii. Quick Sort (improvement of bubble sort)

In the best case, the time complexity is O (NLOGN). But in the worst case, O (n^2) may be reached.

Basic idea:

1. First take a number from the series as the base number
2, the partition process, will be larger than the number of the number of the whole to its right, less than or equal to the number of its full on its left
3. Repeat the second step to the left and right intervals until there is only one number for each interval

1  Public Static voidQuickSort (int[] arr) {2Qsort (arr, 0, Arr.length-1);3 }4 Private Static voidQsortint[] arr,intLowintHigh ) {5     if(Low <High ) {6         intPivot=partition (arr, low, high);//divide the array into two parts7Qsort (arr, Low, pivot-1);//recursively sort left sub-array8Qsort (arr, pivot+1, high);//recursively sort right sub-array9     }Ten } One Private Static intPartitionint[] arr,intLowintHigh ) { A     intPivot = Arr[low];//Pivot Record -      while(low<High ) { -          while(LowHigh ; theArr[low]=arr[high];//switch to the left side of the record smaller than the pivot -          while(LowLow ; -Arr[high] = Arr[low];//switch to the right side of the record smaller than the pivot -     } +     //scan complete, pivot in place -Arr[low] =pivot; +     //The position of the pivot is returned. A     returnLow ; at}

Iv. Insert Sort

Basic idea:

By constructing an ordered sequence, for unsorted data, to scan from backward forward in the sorted sequence, locate the corresponding position and insert

1     voidInsertion_sort (intA[],intN)2     {  3         inti,j,tmp; 4          for(i = 1; i < n; i++) {  5TMP =A[i]; 6              for(j = i-1; J >= 0 && a[j] > tmp; j--) {  7A[J+1] =A[j]; 8             }  9A[J+1] =tmp; Ten         }   One}

Various sorting algorithms

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.