Counting sorting condition: the elements of the array to be sorted must be in a certain range, for example, 1 ~ 100. Before sorting, we must know the range of array elements. Idea: as its name implies, it is counted using an array. Step: 1. Use an array to count [].The elements of the sorted array arr [] are marked as the subscript of the array count [].If there are two identical numbers in the array arr [], the value is in count [] ++. For example:Count [arr [I] ++. 2. traverse the array again, count [], count [I] + = count [I-1] + count [I-2] +... + count [0], in orderRecord the position of the element corresponding to this subscript in the array. 3. Start sorting. We use an array to record sorted [] elements in the sorting order. 4. From step 2, we can clearly know that the element corresponding to the subscript in count [] excludes the number of all elements ).But it must be reduced by one (for example, the array subscript corresponding to the fifth digit should be 4). 5. The specific expression is: count [arr [I] --; // The subscript in sorted [] minus one sorted [count [arr [I] = a [I]; // The position of count [arr [I] in sorted []. equal to a [I], it is the value of the corresponding element. 6. After traversing the length of the array in step 5, you can get the sorted array sorted []. Time Complexity: O (N + K) code: show you my code!
Public class CountSort {static int [] countSort (int [] a, int range/* range of array elements */) {int count [] = new int [range]; for (int I = 0; I <. length; I ++) {count [a [I] ++;} for (int I = 1; I <count. length; I ++) {count [I] + = count [I-1];} int sortArr [] = new int [. length]; for (int I = 0; I <sortArr. length; I ++) {count [a [I] --; sortArr [count [a [I] = a [I];} return sortArr ;} public static void main (String [] args) {int [] a = {,}; int [] sortArr = countSort (, 100); for (int I = 0; I <sortArr. length; I ++) {System. out. print (sortArr [I] + "");}}}Bucket sorting condition: similar to counting sorting, you must know the range of elements to be sorted (for example, 0 ~ N ). Ideas: 1. We can refer
The range of elements is divided into K ranges.(Each range corresponds to a bucket) (K = N/constant ). 2. Then we can assign the elements of the array to be sorted to the corresponding bucket. 3. Sort the buckets (sort by insert ). (If the data in each bucket is the same or the bucket range is 1, the data is degraded to count sorting ). 4. Sort the buckets in order. Time Complexity: O (N + K), worst: (N ^ 2) that is, it is all in one bucket (the insertion sorting time complexity of the bucket is N ^ 2), and the space complexity is (N * K ). Applicable: Applicable to big data sorting, awesome, haha! CODE: see click to open the link.
# Include
# Include
# Include
Using namespace std;/* initial arr */void InitialArr (double * arr, int n) {srand (unsigned) time (NULL); for (int I = 0; I
< n; i++){cout<< 0 && temp bucket[flag][j - 1]; --j){bucket[flag][j]="bucket[flag][j-1];}bucket[flag][j]" =temp;} 所有数据重新链接 int k="0;for" ; 10 i++){for j="0" j< count[i];j++){arr[k]="bucket[i][j];k++;}}for" i<10 ;i++){delete bucket[i];bucket[i]="NULL;}delete" []bucket;bucket="NULL;}void" main(){double *arr="new" double[10];initialarr(arr, 10);bucketsort(arr, 10);printarr(arr,10);delete [] arr;}< pre>
The base sorting condition is relatively loose: it can make a relatively irregular range, but try not to include negative numbers. Otherwise, you have to consider it separately. Idea: 1. We can compare each element by comparing the largest bit. In this way, the sorted elements can be obtained. 2. For example, the following elements are available: 9, 78,345, 34, 34,345. A: sort the digits by 23, 78, and 9. B: 9 (note: 10 digits of 9 are 0), 23, 34,345, 78.C: sort hundreds of BITs: 9, 23, 34, 78,345.3, and count each time. Time Complexity: O (K * N) CODE: Wikipedia: Base sorting
# Include
# Define MAX 20 # define SHOWPASS # define BASE 10 void print (int * a, int n) {int I; for (I = 0; I <n; I ++) {printf ("% d \ t", a [I]) ;}} void radixsort (int * a, int n) {int I, B [MAX], m = a [0], exp = 1; // find the maximum number of digits for (I = 1; I <n; I ++) {if (a [I]> m) {m = a [I] ;}} while (m/exp> 0) {int bucket [BASE] = {0 }; // sort the counts for (I = 0; I <n; I ++) {bucket [(a [I]/exp) % BASE] ++ ;} for (I = 1; I <BASE; I ++) {bucket [I] + = bucket [I-1];} for (I = n-1; I> = 0; I --) {B [-- bucket [(a [I]/exp) % BASE] = a [I] ;}for (I = 0; I <n; I ++) {a [I] = B [I];} // counting sorting end exp * = BASE; // enter the next digit # ifdef SHOWPASS printf ("\ nPASS :"); print (a, n); # endif} int main () {int arr [MAX]; int I, n; printf ("Enter total elements (n <= % d): ", MAX); scanf (" % d ", & n); n = n <MAX? N: MAX; printf ("Enter % d Elements:", n); for (I = 0; I <n; I ++) {scanf ("% d ", & arr [I]);} printf ("\ nARRAY:"); print (& arr [0], n); radixsort (& arr [0], n ); printf ("\ nSORTED:"); print (& arr [0], n); printf ("\ n"); return 0 ;}