"Data Structure" non-comparative sorting algorithm (implement count sort and base sort)

Source: Internet
Author: User
Tags assert

Count sort

1, the algorithm thought:

The counting sort is the deformation of the direct addressing method. By opening up a certain amount of space, the number of occurrences of the same data is counted and then written back to the original sequence.

2. Steps:

1) Find the largest and smallest data in the sequence, and determine the amount of space to open.

2) Open up space, use open space to store the number of each data.

3) write the ordered sequence back to the original sequence.

The specific implementation is as follows:

Void countsort (int *arr, int size) { assert (arr);  int min = arr[0] ; int max = arr[0]; int num = 0; for  (int i =  0; i < size; ++i)//Find maximum and minimum number  {  if  (arr[i] < min)   {   min = arr[i];  }  if  (arr[i] >  Max)   {   max = arr[i];  } } num = max -  min + 1;//Open Space size  int *count = new int[num]; memset (count,  0, sizeof (int) *num);//Initialize count for  (int i = 0; i < size;  ++i)  {  count[arr[i] - min]++;//Direct addressing  } int index = 0;  for  (int i = 0; i < num; ++i)  {  while  (count[i ]--)   {   arr[index] = i + min;//writes back to    index++; &nbsp in the original sequence;}  }}

Advantages and Disadvantages

Advantage: When sorting integers within a certain range, it has a complexity of 0 (N+k) (where K is the range of integers), faster than any comparison sorting algorithm

Disadvantage: The cardinal order needs to open up the corresponding size space, K large time space utilization is not high, therefore adapts to the data dense sequence. If the data is dense and not duplicated, we can use bitmaps.

Base sort

Cardinality sorting is a typical sort of assignment class, and assigning class ordering is the implementation of sorting by allocating and collecting two basic operations. The base sort is sorted by repeated allocation and collection operations, and there are two sorting methods for the cardinal sort, namely "low priority" and "high priority". Here I analyze the "low priority" sort method.

1, the algorithm thought:

When sorting, the records are initially sorted by the lowest bit value, which is then further sorted by the lower value. And so on, from low to high, each trip is based on the previous trip, according to the keyword key of one of the records of all sorts, until the highest bit, so that the whole process of cardinal sorting.

2. Steps:

1) It is necessary to find the maximum number of digits due to the end of the highest position.

2) Open space to store a sequence of sorted sequences. using the quick transpose idea of the matrix, the count array is used to store the number of the same number of bits in the sort, and the start array is used to record each starting position, and each element is quickly positioned.

3) write back to the original sequence.

4) Repeat the above steps until the highest level is reached.

Void radixsort (int *arr, int size) { assert (arr);  int *count = new  int[10];//per digit between 0~9  int *start = new int[10]; int *tmp =  new int[size];//Store the sequence  int maxradix = getmaxradix (arr, size) after each trip;//maximum number of digits   int radix = 1; for  (int k = 1; k <= maxradix; + +k)  {  memset (count, 0, sizeof (int) * 10);//Initialize Count  memset (start,  0, sizeof (int) * 10);  for  (int i = 0; i < size ;  ++i)//count stores the same number of digits   {   count[(Arr[i] / radix)  % 10 ]++;  }  start[0] = 0;  for  (int i = 1; i  < 10; ++i)//start Storage data Start position   {   start[i] = start[i -  1] + count[i - 1];  }  for  (int i = 0; i <  Size; ++i)//Quick Location   {   int num =  (arr[i] / radix)  %  10;   tmp[start[num]++] = arr[i];  }  memcpy (Arr, tmp,  sizeof (int) *size);//write-back   radix *= 10; } delete[] tmp;//note release tmp}

● Stability Analysis

Stability means that the relative position of the same data in the original sequence does not change after sorting. Insert Sort, bubble sort, merge sort, count sort and cardinal sort are stable; Quick Sort, hill sort, heap sort, and select sort are unstable.

● Analysis of Complexity

1, space complexity: fast sorting, merge sorting, counting sorting and cardinal sort all need to open up space, insert sort, hill sort, select sort, heap sort, bubble sort all need not, space complexity is O (1).

2. Complexity of Time:

1) Insert sort, hill sort, select sort, bubble sort all O (n^2), low efficiency. The choice of sorting efficiency is lowest, in the best case time complexity or O (n^2), bubble sort and insert sort comparison, the insertion sort is better (eg:0 2 1 3 4 5 6 7 8 9; the insertion sort is done once, and the bubble sort takes two times, and the bubble sort is expensive, and the insertion is more time-reducing after optimization (Hill sort).

2) heap Sort, merge sort, and quick sort are all O (N*LG (n)).

The worst-case scenario is usually seen, but the fast sort (there are more optimizations) has almost no worst case, with a time complexity of O (N*LG (n)).

One drawback of heap sorting is that it can only be sorted on an array, there are limitations (data-dense) for both cardinality sorting and counting sorting, the spatial complexity of the merge sort is O (n), and the quick Ordering is O (LG (n)), which can be sorted best by quick sorting.

Dry

Merge sort exists within sort and outer sort. Out-of-order refers to the ability to sort outside of memory (on-disk) data, for large data files, not directly loaded into memory for sorting, you can take the file into small files, load small files into memory to sort, and then rewrite the ordered data, By reordering the two ordered data files, you can arrange large data files. According to the above ideas can be the implementation of file compression, interested can try it yourself.

This article is from the "Scen" blog, make sure to keep this source http://10741357.blog.51cto.com/10731357/1782275

"Data Structure" non-comparative sorting algorithm (implement count sort and base sort)

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.