Sort a [17] [4] = {"", "cow", "dog", "sea", "rug", "row", "Mob ", "box", "tab", "bar", "ear", "tar", "dig", "big", "tea", "now ", "Fox.
"Base sorting" can be seen as a condition for "counting sorting". Generally, it is difficult to sort decimals by base, and the efficiency is not as good as counting sorting, however, if n long integers or strings with the length of B, you can use R (r <= B) to divide them into B/R blocks, and then sort them by count. The time complexity is O (B/R) (N + 2 ^ r), where O (N + 2 ^ r) is the time for sorting each count, b/R return count sorting. Although base sorting looks better than the average condition of quick sorting, its constant factor is much greater than that of quick sorting. Although it performs less times, however, each time it takes a lot of time, and the base sorting is not in-situ sorting and requires additional memory, which is worse than in-situ quick sorting to save memory.
# Include <string. h> # include <time. h> # define buffer_size 10 void countingsort (char (* A) [4], int Len, int index, int K) {char B [Len + 1] [4]; int C [k]; int I = 0; for (I = 0; I <K; I ++) {C [I] = 0 ;}for (I = 1; I <= Len; I ++) {C [A [I] [Index]-'a'] ++;} for (I = 1; I <K; I ++) {C [I] + = C [I-1];} for (I = Len; I> 0; I --) {strcpy (B [C [A [I] [Index]-'a'], a [I]); c [A [I] [Index]-'a'] --;} for (I = 1; I <= Len; I ++) {strcpy (A [I], B [I]) ;}} void radixsort (char (* A) [4], int Len, int D, int K) {int I = 0; for (I = D-1; I> = 0; I --) {countingsort (A, Len, I, K) ;}} int main () {int I = 0; char A [17] [4] = {"", "cow", "dog", "sea", "rug", "row", "Mob ", "box", "tab", "bar", "ear", "tar", "dig", "big", "tea", "now ", "Fox"}; printf ("array to be sorted: \ n"); for (I = 1; I <= 16; I ++) {printf ("% s ", A [I]);} radixsort (A, 16,3, 26); printf ("base sorting for Arrays: \ n"); for (I = 1; I <= 16; I ++) {printf ("% s", a [I]);} system ("pause"); Return 0 ;}