Count Sort:
#define _CRT_SECURE_NO_WARNINGS 1#include<iostream>using namespace std; #include < assert.h> #include <vector>void print (vector<int> a) { for (Int i = 0; i < a.size (); i++) { cout << a[i] << " "; } cout << endl;} Void countsort (Vector<int>& a) { int max = a[0]; int min = a[0]; //finds the maximum and minimum values of the sequence, opening up max-min+ 1 Space-size count array for (Int i = 1; i < a.size (); i++) { if (Max<a[i]) max = a[i]; if (Min>a[i]) min = a[i]; } int* count = new int[max - min + 1]; memset (count, 0, (max - min + 1) * sizeof (int));//Initialize the array /* to count the array A to sort, the element I of the A array is placed at the i-min of the Count array, Not I. Because: If the sequence is 1000 2000 3000, the opened count starts at subscript 0 and puts 1000 at the 1000-1000=0 of Count */ for (Int i = 0; i < a.size (); i++) { count[a[i]-min]++; } / /Put the count array back to get, i+min on behalf of the restore subscript int j = 0; for ( int i = 0; i < max - min + 1; i++) { while (count[i]>0)//At this time the number repeats n times, then take that number back n times { a[j++] = i + min; count[i]--; } } }void testcountsort () { vector<int> a = { 12, 34, 12222, 4568, 26, 1, 16, 10, 2, 4, 4, 93, 7, 5, 2, 4 }; countsort (a); print (a); }int main () { testcountsort (); system ("Pause"); return 0;}
Base sort:
#define _CRT_SECURE_NO_WARNINGS 1#include<iostream>using namespace std; #include < Assert.h>void print (int* a,int size) { for (int i = 0; i < size; i++) { cout << a[i] << " "; } cout << endl;} Int maxradix (int* a, int size) { int radix = 10; int count = 1; int i = 0; for (int i = 0; i<size; i++) { while (A[i] > radix) { radix *= 10; count++; } } return count;} Void _partradix (int* a, int size,int divisor) { int count[ 10] = { 0 }; //handles array count, counts the number of occurrences of each, ten, and every digit of the data for (int i = 0; i < size; i++) { int num = a[i] / divisor; count[num % 10]++; } // Working with array start, counting the starting position of each element int start[10] = { 0 }; for (int i = 1; i < 10; i++) &NBSP;&NBSP;&NBSP;&NBsp { start[i] = start[i - 1] + count [i - 1]; } //iterates over array A, placing these elements in the computed position of TMP int* tmp = new int[size]; for (int i = 0; i < size; i++) { int num = a[i] / divisor; tmp[start[num % 10]++] = a[i];//if the bit has a repeating number, then the Gaga coordinates are placed at the back of the starting position } //back to bits or 10 bits or hundreds of sorted arrays, starting with the next bit of sort for (int i = 0; i < size; i++) { a[i] = tmp[i]; }}void radixsort (int* a, int size) { assert(a); for (Int i = 1; i <= maxradix (a, size); i++) { int divisor = 1;// Get divisor, easy to get data digit, 10 bit, hundred ...... int k = i; while (--k) { divisor *= 10; } _partradix (a, size, divisor); }}void testradixsort () { int a[] = { 12, 34, 12222, 4568, 26, 1, 16, 10, 2, 4, 4, 93, 7, 5, 2, 4 }; radixsort (A, sizeof (a) / sizeof ( A[0]); print (A,sizeof (a)/sizeof (a[0])); Int main () { testradixsort (); system ("Pause"); return 0;}
This article is from "Han Jing's Blog", please make sure to keep this source http://10740184.blog.51cto.com/10730184/1782077
"Data structure" algorithm implementation of non-comparative sorting (including counting sort, counting sort)