Preface
Non-Comparative sorting, no comparison, exchange, and sorting in linear time. Disadvantage: There are many space requirements, not in-situ sorting. A typical space is used for time.
Count sorting
Counting sorting utilizes one feature: In an array that has sorted the order (for example, from small to large), if the element I is X, there must be one element in the array that is less than or equal to X. Counting sorting requires a Space Price: N + max_num.
Sort buckets
Two steps are involved in sorting buckets:DistributionElement to each bucket; OrderReclaimElement in the bucket. The pseudocode is as follows:
Bucket_sort ()
- N
← Length [A]
- ForI= 1NDo
- InsertA[I] Into list
B[Na[I]
- ForI= 0N-1 do
- Sort listBWith insertion sort
- Concatenate the Lists B [0], B [1],... B [n-1] together in order.
The Code Implementation of bucket_sort is to directly traverse count to determine whether it is greater than 1 and reclaim elements. counting_sort converts the Count content
After sorting the original array, the number of elements before the element also needs to be traversed once.
# Include <iostream> <br/> # include <algorithm> <br/> # include <limits> <br/> # include <vector> <br/> # include <ctime> <br/> # include <cassert> </P> <p> using namespace STD; </P> <p> void bucketsort (int * array, int length) <br/>{< br/> const int max = 256; </P> <p> int bucket [Max]; </P> <p> for (unsigned int I (0); I <Max; I ++) bucket [I] = 0; </P> <p> for (unsigned int I = 0; I <length; I ++) <br/> bucket [array [I] ++; </P> <p> (Unsigned I = 0, idx = 0; I <Max; I ++) <br/> for (unsigned int K = bucket [I]; k> 0; k --) // reclaim elements in each bucket <br/> array [idx ++] = I; <br/>}</P> <p>/** sort array values, store the results in DST array <br/> @ Param arr point to source array <br/> @ Param DST point to destinate array <br/> @ Param length the array size. <br/> @ note need O (N + max_num) Assistant space. space ==> time <br/> **/<br/> void counting_sort (int * Arr, int * DST, int length) <br/>{< br/> const int max_num = 255; <br/> int count [max_num]; </P> <p> for (int I (0); I <max_num; I ++) Count [I] = 0; </P> <p> for (int I (0); I <length; I ++) <br/> count [arr [I] ++; </P> <p> for (int I (1); I <max_num; I ++) <br/> count [I] + = count [I-1]; </P> <p> for (INT I = length-1; I> = 0; I --) <br/> {<br/> DST [count [arr [I]-1] = arr [I]; // You must subtract one <br/> count [arr [I] --; <br/>}</P> <p> Int main () <br/> {<br/> # define P_2 </P> <p> # ifdef P_1 <br/> {<br/> int myints [] = {10, 20, 30, 5, 15 }; <br/> int length = sizeof (myints)/sizeof (myints [0]); <br/> int * result = new int [length]; </P> <p> counting_sort (myints, result, length); </P> <p> for (int I (0); I <length; I ++) cout <result [I] <"; <br/> cout <Endl; </P> <p> Delete result; Result = 0; <br/>}< br/> # endif // P_1 </P> <p> # ifdef P_2 <br/>{< br/> Co NST int test_num = 10000000; // 1 million <br/> srand (INT) time (0 )); </P> <p> cout <"test number" <"/T:/T" <test_num <STD: Endl; </P> <p> int * testptr = new int [test_num]; <br/> int * testptr1 = new int [test_num]; <br/> int * testptr2 = new int [test_num]; </P> <p> for (INT idx = 0; idx <test_num; idx ++) <br/> testptr2 [idx] = testptr1 [idx] = testptr [idx] = rand () % 255; </P> <p> clock_t T = clock (); <br/> // sort <Br/> // keep the testptr1, change the testptr. <br/> counting_sort (testptr1, testptr, test_num); <br/> cout <"count_sort" <"/T:/T" <clock () -T <STD: Endl; </P> <p> T = clock (); <br/> bucketsort (testptr1, test_num ); <br/> cout <"bucketsort" <"/T:/T" <clock ()-T <STD: Endl; </P> <p> T = clock (); <br/> STD: Sort (testptr2, testptr2 + test_num); <br/> cout <"STD :: sort "<"/T:/T "<clo CK ()-T <STD: Endl; </P> <p> // test the result validity <br/> for (INT idx = 0; idx <test_num; idx ++) <br/> If (testptr [idx]! = Testptr1 [idx] | testptr1 [idx]! = Testptr2 [idx]) <br/> cout <"wrong"; </P> <p> Delete [] testptr; testptr = 0; <br/> Delete [] testptr1; testptr1 = 0; <br/> Delete [] testptr2; testptr2 = 0; <br/>}< br/> # endif // P_2 </P> <p> # ifdef P_3 <br/> # endif // P_3 </P> <p> system ("pause "); </P> <p> return 0; <br/>}
Test results:
Test number: 10000000 <br/> count_sort: 112 <br/> bucketsort: 70 <br/> STD: sort: 4646
The advantages of counting and bucket sorting time are needless to say, but these non-Comparative sorting algorithms have higher requirements on space and are also related to the distribution of sorting data.The above test result is based on the assumption that all random data is between [0-255 ].. The more scattered the data is, the more complex the space is.
Postscript
The bucket sorting method is especially suitable for sorting string arrays, because acⅱ characters are both located in the range of 0,255. For more general bucket sorting, you can use a linked list. If you have time, continue ~