Data structures-Various ' sort algorithm ' implementations (bottom)

Source: Internet
Author: User

In the previous blog, the main is to achieve a variety of sorting algorithms, and for some algorithms to optimize the processing, the following main discussion of the non-comparative sorting algorithm (counting sorting, radix sorting), and the performance of a variety of sorting algorithms, time complexity, space complexity, advantages and disadvantages, as well as the applicable scenarios to do a summary analysis.


1. Counting sort

main idea: mainly need statistic number, use direct addressing method, statistic maximum number and minimum number, open two number difference space size, for duplicate data, use count to count, Time complexity O (n+ range number), Space complexity O (range number) The counting sort is suitable for the data dense situation, when the data is dense and does not have the duplicate data, may use the ' bitmap ' directly, can save the space


Void countsort (int* a, size_t size) {     assert (a);      int max = a[0];     int min = a[0];      int count = 0;      for  (size_ T i = 0; i < size; ++i)       // Find the maximum and minimum numbers in an array      {          if   (a[i] < min)           {                min = a[i];           }           if  (A[i] > max)           {                max = a[i];           }     }          int*  tmp = new int[max - min + 1];    //opens up storage space and initializes      memset (tmp, 0, sizeof (int) * (max - min + 1));      for  (size_t i = 0; i < max - min  + 1; ++i)    //Direct addressing method      {           int num = a[i] - min;           tmp[num]++;     }     for   (size_t i = 0; i < size;)     //writes the sorted order in the A array       {          for  (size_t j = 0; j < &NBSP;MAX&NBSP;-&NBSP;MIN&NBSP;+&NBSP;1;&NBSP;++J)            {               count =  tmp[j];               while  ( count--)     //need to write multiple times for duplicate data                 {                     a[i] = j + min;                     i++;                }           }       }     delete[] tmp;}


2. Base sorting

Main idea: Similar to the idea of ' quick transpose ', open two arrays count and Start,count are used to count the number of data in single-digit 0~9, start is used to begin the statistical data starting position (starting at 0, position of the next bit of data = where the previous data started + The previous total number of data), and open a size space to hold each sort, below is the low cardinal sort, starting from the single digit, and then sort 10 bits, and then hundreds, until the highest level of the largest data, sort end.


Int getmaxradix (int* a, size_t size)     //find the maximum number of bits of data {      int index = 1;   //Data Minimum One      int max  = 10;     for  (size_t i = 0; i <  Size; ++i)      {           while  (A[i] >= max)       //data greater than 1 bits            {                index++;                max = max * 10;          }      }     return index;} Void lsdsort (int* a, size_t size) { &Nbsp;   assert (a);      int index = getmaxradix (a,  size);    //the number of bits      int count[10] = { 0&nbsp the maximum data;} ;     //record the number of occurrences of the data      int start[10] = { 0  };   //where to start recording data      int radex = 1;      int* bucket = new int[size];              for  (int k = 1; k <= index; ++k )     //from everyone to the highest ranking      {           memset (count, 0, sizeof (int) * 10)     //count 0 before each sort           //count (number of data for each 0~9)            for  (size_t i = 0; i < size; ++i)            {                int num =  (A[i] / radex)  % 10;      //take digit                count[num ]++;          }                     //where to start recording data            start[0] = 0;           int j = 1;          while   (J&NBSP;&LT;&NBSP;10)           {                start[j] = start[j - 1] + count[j -  1];               j++;           }           for  (size_t i = 0; i < size; ++i)    // Put data in a bucket in order           {                int num =  (A[i] / radex)  % 10;                bucket[start[num]++] = a[i];          }           radex *= 10;           memcpy (A,  bucket, sizeof (int) * size);     }          delete[] bucket;}


3. Sorting algorithm Summary

(1) Performance analysis of various sorting algorithms

650) this.width=650; "title=" Untitled. png "src=" http://s3.51cto.com/wyfs02/M01/80/76/ Wkiom1dciihywa01aabinr6tukk426.png "alt=" Wkiom1dciihywa01aabinr6tukk426.png "/>

Where: R is the number of data ranges


Stability Analysis:

        Stability: refers to the data that needs to be sorted if there is the same data element, before the sort, After the relative position is unchanged, that is, when sorting {1,3,5 , 7,2,5 , 6}, sorted after ' 5 ' in ' 5 ' Span style= "line-height:0px;" > before, instead of exchanging with each other.

among the kinds of sorts introduced, inserting sort, bubble sort, merge sort, count sort is stable. Quick Sort, hill sort, select Sort, heap sort are unstable


Complexity of space:

in the sorting algorithm, the quick sort (which requires recursion), the merge sort, the Count sort, and the cardinal sort all require additional space to sort. The rest of the sorting algorithm does not need any space.


Complexity of Time:

O (n^2):

Insert Sort, bubble sort, select Sort is the space complexity of O (n^2), sorting efficiency is basically relatively low, the choice of sorting is the most bad, because in the most cases, also need to n^2 time complexity, relatively speaking, the insertion and bubbling can be better, in the case of optimization, can reduce the sequencing time. But when the amount of data is large, the time cost of bubbling is higher.


O (N*LGN):

The average performance is O (N*LGN) algorithm: fast sorting, merge sorting, heap sorting algorithm, fast sorting through a variety of optimization algorithms ( three-digit method, the interval is small when using the direct insertion algorithm , "The previous blog detailed introduction of the optimization of fast sorting" has been relatively more efficient.

Merge sort is also called the outer sort, the outside sort actually refers to the ability to sort the data outside the memory (on disk), the file of big data cannot be loaded directly into memory to sort, we can take the file into small file, load the small file into memory to sort, then rewrite the ordered data, By reordering the two ordered data files, you can arrange large data files. The reader can go on with the experiment, and this will not be explained in detail here.

The efficiency of heap sequencing is still quite high, but there is a fatal drawback to heap sequencing, which is that it is only possible to sort the arrays, because the index of the array is required to determine the location of the data in the heap. So The heap sort can only be sorted on an array .



This article is from the "unintentional persistent" blog, please be sure to keep this source http://10740590.blog.51cto.com/10730590/1782091

Data structures-Various ' sort algorithm ' implementations (bottom)

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.