Common sorting algorithms

Source: Internet
Author: User

data structure is a part of the important content is the sorting algorithm, after sorting can be used fast binary find, sorting algorithm, the algorithm of the measurement standard mainly, 1) stability , 2) time complexity for best/Worst case, 3) The spatial complexity of the best/worst case scenario. Give a summary.

The following is the introduction of a common sorting algorithm, and its performance analysis, the sorting method is the length of $n $ of the sequence L is ordered, the elements are $ (l_0,l_1,.. L_{N-1}) $.

1. Insert Sort

1) Direct insert sort stable, preferably $O (n) $, Worst $O (n^2) $, average $O (n^2) $, Space $O (1) $

Direct Insert sort Select a number from the sequence to be sorted, select a location to insert it into the ordered list, and repeat the process until the sort is complete.

Show the direct insert execution process, began to assume that the first $0$ elements ordered, for the first $i =1...n-1$ elements, from the $i -1$ forward search, find the insertion position, and then move the record, find the appropriate location to insert, can be seen $n -1$ the insertion, if the array is ordered only $n -1$ The second comparison can be, without moving, the complexity shown is $O (n) $, if the array is reversed, you need to $\frac{(n-1) (n-1+1)}{2}$ comparison and movement, the complexity of $O (n^2) $, so the best time complexity $O (n) $, the worst time Inter-complexity $O (n^2) $, with an average time complexity of $O (n^2) $. Because the spatial complexity is $O (1) $, the algorithm is stable because it is sorted in situ.

2) Binary Insert sort

Because the ordering process in the front is already orderly, so the ordered sequence can be binary lookup, relatively direct insertion, reducing the number of comparisons, but because the insertion position after the drop to move the elements of the sequence, so the time complexity is still $O (n^2) $.

To insert a sort code directly:

 Public voidInsert_sort (int[] nums) {        if(Nums = =NULL|| Nums.length <2)return; //i = 1-n-1         for(inti =1; i < nums.length; i + +){            if(Nums[i] >= nums[i-1])Continue; intPivot = Nums[i], j = i1;//Pending Insertion             while(J >=0&& Nums[j] >pivot) nums[j+1] = nums[j--]; Nums[j+1] = pivot;//J < 0 for inserting into the pinch face        }    }
View Code

As for binary insert sort, you need to find the insertion position with binary search . If the same element, in order to ensure its stability, then find the last of the same key, for example, now the sequence is [1,1,1,1,1,3,4,1], we want to insert the last 1, in order to maintain its stability , we need to find the last 1, this with the same element of the Binary search has the following code:

 Public intBinary_search (int[] Nums,intPosintkey) {        if(Nums = =NULL)return-1; intLow =0; intHigh = Nums.length-1;  while(Low <=High ) {            intMID = low + (high-low)/2;//Prevent overflow            if(Nums[mid] = =key) {                                if(POS = =-1){                    if(Mid >0&& nums[mid-1] = = Nums[mid]) high = mid-1; Else returnmid; }Else if(POS = =1){                    if(Mid < nums.length-1&& nums[mid+1] = = Nums[mid]) Low = mid +1; Else returnmid; }Else returnMid//POS = = 0                            }Else if(Nums[mid]>key) {High = mid-1 ;} Else{Low = mid +1;} }        returnHigh//Insert the element after high}
View Code

2. Hill sort

Common 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.