-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20[课设].基数排序.cpp
60 lines (55 loc) · 905 Bytes
/
20[课设].基数排序.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <queue>
#include <stdio.h>
#include <time.h>
#define BUCKET_NUM 20
using namespace::std;
queue<int> bucket[BUCKET_NUM];
void RadixSort(int* nums, int n, int maxLength, int radix)
{
//±éÀúÍ°
int B = 1;
for (int i = 0; i < maxLength; i++)
{
//±éÀúÊý×é
for (int j = 0; j < n; j++)
{
int temp = nums[j]/B;
bucket[temp % radix].push(nums[j]);
}
int k = 0;
for (int j(0); j < radix; j++)
{
while (!bucket[j].empty())
{
nums[k++] = bucket[j].front();
bucket[j].pop();
}
}
B *= radix;
}
return;
}
void Print(int* s, int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", s[i]);
}
printf("\n");
}
int main()
{
int n;
int nums[100];
printf("Please input n\n");
scanf_s("%d", &n);
int i;
for (i = 0; i < n; i++)
{
scanf_s("%d", nums + i);
}
RadixSort(nums, n, 3, 10);
for (i = 0; i < n; i++)
printf("%d ", nums[i]);
}