LSD Radix Sort,两个实现
自己用单链表实现如下:
1: #include <stdio.h>
2: #include <stdlib.h>
3: #include <math.h>
4:
5: /*Radix Sort*/
6: #define MAX_COUNT 100
7: #define BASE 10
8: typedef struct node snode;
9: typedef struct node* ptrtonode;
10: struct node
11: {
12: int value;
13: ptrtonode next;
14: };
15: ptrtonode create_newnode();
16: void make_empty(ptrtonode pnode);
17: void append_node(ptrtonode header,ptrtonode element);
18: void append_value(ptrtonode header,int value);
19: void print(int *array,int length);
20: void print_list(ptrtonode header);
21: void clear_list(ptrtonode header);
22:
23: ptrtonode create_newnode()
24: {
25: ptrtonode pnode=(ptrtonode)malloc(sizeof(snode));
26: make_empty(pnode);
27: return pnode;
28: }
29:
30: void make_empty(ptrtonode pnode)
31: {
32: pnode->value=0;
33: pnode->next=NULL;
34: }
35:
36: void append_node(ptrtonode header,ptrtonode element)
37: {
38: ptrtonode position=header;
39: while(position->next!=NULL)
40: {
41: position=position->next;
42: }
43: position->next=element;
44: }
45:
46: void append_value(ptrtonode header,int value)
47: {
48: ptrtonode element=create_newnode();
49: element->value=value;
50: element->next=NULL;
51: append_node(header,element);
52: }
53:
54: void print(int *array,int length)
55: {
56: int i=0;
57: for(i=0;i<length;i++)
58: {
59: printf("%d,",array[i]);
60: }
61: printf("\n");
62: }
63:
64: void print_list(ptrtonode header)
65: {
66: ptrtonode position=header->next;
67: while(position!=NULL)
68: {
69: printf("%d,",position->value);
70: position=position->next;
71: }
72: printf("\n");
73: }
74:
75: void clear_list(ptrtonode header)
76: {
77: ptrtonode position=header->next;
78: ptrtonode temp;
79: while(position!=NULL)
80: {
81: temp=position->next;
82: free(position);
83: position=temp;
84: }
85: header->next=NULL;
86: }
87:
88: int main()
89: {
90: int numbers[MAX_COUNT];
91: int results[MAX_COUNT];
92: int i=0;
93: int maxValue=0;
94: //init random numbers to sort
95: for(i=0;i<MAX_COUNT;i++)
96: {
97: numbers[i]=rand();
98: results[i]=numbers[i];
99: if(numbers[i]>maxValue)
100: {
101: maxValue=numbers[i];
102: }
103: }
104: printf("the random numbers:\n");
105: print(numbers,MAX_COUNT);
106:
107: //get the digit
108: i=maxValue;
109: int digit=0;
110: while(i!=0)
111: {
112: i=i/BASE;
113: digit++;
114: }
115: printf("the digit is %d\n",digit);
116:
117: //init bucket
118: ptrtonode buckets[BASE];
119: for(i=0;i<BASE;i++)
120: {
121: buckets[i]=create_newnode();
122: }
123:
124: int j,k,m,digitValue;
125: for(j=0;j<digit;j++)
126: {
127: if(j==0)
128: {
129: k=1;
130: }
131: else
132: {
133: k*=10;
134: }
135:
136: for(i=0;i<MAX_COUNT;i++)
137: {
138: digitValue=(results[i]/k)%BASE;
139: append_value(buckets[digitValue],results[i]);
140: }
141: m=0;
142: for(i=0;i<BASE;i++)
143: {
144: ptrtonode position=buckets[i];
145: position=position->next;
146: while(position!=NULL)
147: {
148: results[m]=position->value;
149: position=position->next;
150: m++;
151: }
152: }
153:
154: for(i=0;i<BASE;i++)
155: {
156: clear_list(buckets[i]);
157: }
158: }
159:
160: print(results,MAX_COUNT);
161: return 0;
162: }
我们也可以纯数组实现,
这里给出的是wiki(http://en.wikipedia.org/wiki/Radix_sort)给出的用数组实现的版本:
1: #include <stdio.h>
2: #define MAX 5
3: #define SHOWPASS
4: void print(int *a, int n)
5: {
6: int i;
7: for (i = 0; i < n; i++)
8: printf("%d\t", a[i]);
9: }
10:
11: void radixsort(int *a, int n)
12: {
13: int i, b[MAX], m = 0, exp = 1;
14: for (i = 0; i < n; i++)
15: {
16: if (a[i] > m)
17: m = a[i];
18: }
19:
20: while (m / exp > 0)
21: {
22: int bucket[10] =
23: {
24: 0
25: };
26: for (i = 0; i < n; i++)
27: bucket[a[i] / exp % 10]++;
28: for (i = 1; i < 10; i++)
29: bucket[i] += bucket[i - 1];
30: //Arrived here, the bucket array will store the index position of each element
31: for (i = n - 1; i >= 0; i--)
32: b[--bucket[a[i] / exp % 10]] = a[i];
33: for (i = 0; i < n; i++)
34: a[i] = b[i];
35: exp *= 10;
36:
37: #ifdef SHOWPASS
38: printf("\nPASS : ");
39: print(a, n);
40: #endif
41: }
42: }
43:
44:
45: int main()
46: {
47: int arr[MAX];
48: int i, n;
49:
50:
51: n=5;
52: printf("Enter %d Elements : ", n);
53: for (i = 0; i < n; i++)
54: arr[i]=rand();
55:
56:
57: printf("\nARRAY : ");
58: print(&arr[0], n);
59:
60: radixsort(&arr[0], n);
61:
62: printf("\nSORTED : ");
63: print(&arr[0], n);
64: printf("\n");
65:
66: return 0;
67: }
显然算法二在性能上优于我自己实现的前一个,当然我实现的主要目的是为了学习单链表结构。
不论是哪一种实现,其核心思想是不变的,这在算法导论中定义的再精辟再清楚不过了:
RADIX_SORT(A,d)
1. for i=1 to d
2. do use a stable sort to sort array A on digit i.