Chapter 8 "base sorting Radix-sort" for linear time sorting (Exercise 8.3-1)

Source: Internet
Author: User

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 ;}

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.