Summary of sorting algorithms

Source: Internet
Author: User

believe that the sorting algorithm everyone is more familiar with, this is my personal experience and implementation, link https://github.com/yxiao1994/Sort. Here I'm mainly talking about some implementation details.

For fast sorting, be sure not to forget the terminating condition, otherwise there will be unlimited recursion causing the program to crash, the code is as follows:

void Sort::quicksort (vector<int int int r) {     if(p<r) {// must pay attention to the judgment here, otherwise it will be infinite recursion down, and here is less than, not less than equal        int q = PARTITION (VEC, p, R); // Q is the element        in the array q-p+1 large 1 );         1 , R);}    }

For the merge sort, mainly the implementation of the merging process, it is necessary to pay attention to setting Sentinel, which can be found in the introduction of algorithms. The code is as follows:

voidSort::merge (vector<int> & VEC,intPintQintR) {//merge process for merge sort, see Introduction to Algorithms    intN1 = Q-p +1; intN2 = R-Q; int*l =New int[N1 +1]; int*r =New int[N2 +1]; L[N1]= MAX;//Set SentinelR[N2] = MAX;//Set Sentinel     for(inti =0; I < N1; i++) L[i]= Vec[p +i];  for(intj =0; J < N2; J + +) R[j]= vec[j + q +1];  for(inti =0, j =0, k = p; K <= R; k++) {        if(L[i] <=R[j]) {Vec[k]=L[i]; I++; }        Else{Vec[k]=R[j]; J++; }    }    Delete[]l; Delete[]r;}

Here are some of the test results, which I'm testing in general (i.e., random arrays) and special cases (ordered arrays). The array size I set is 10000.

(1) Comparison of sorting performance under general conditions

Sorting algorithms Bubble sort Insert Sort Quick Sort Random Version Quick Sort Merge sort
Time required (unit: s) 57.516 18.538 0.083 0.087 0.075

As you can see, the time complexity O (nlog N) version and the time Complexity O (N2) algorithm are indeed not a level.

(2) Sorting performance comparison of sorted arrays

Sorting algorithms Bubble sort Insert Sort Quick Sort Random Version Quick Sort Merge sort
Time required (unit: s) 32.65 0.009 38.309 21.641 0.07

As you can see, in an ordered array, the performance of the fast sorting algorithm will go to O (N2), because now each partition is divided into an array of n-1 elements and an array of 1 elements, and the complexity of the partition process is O (n), and the following formula is obtained:

T (n) =t (n-1) +o (n)

This makes it easy to see why the performance of the fast sorting algorithm drops to the O (N2) level. Using a random version of the quick sorting effect will be better, but according to the test effect is better and does not solve the problem, I do not understand, I hope you enlighten. The algorithm of merge sort is still stable, it is worth noting that the time complexity of inserting sort is already O (n), so it is faster than merge sort.

There is also a sort artifact that can be found in the programming Zhu Ji Nanxiong, but with more restrictive conditions, the need for data in the array is not duplicated, and the input data is limited to a smaller range. This problem is more in the interview, for large file sorting is very suitable, for example, 1 million phone numbers to sort.

Probably summed up so much, the first time to write a blog, write bad Hope forgive me.

Summary of 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.