>

LoeiJe

:D 获取中...

何以解忧?唯有暴富

桶排序

有点小问题的桶排序!!

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
// 桶排序.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

#define MAX_SIZE 10

struct array
{
int value[MAX_SIZE];
int mark[MAX_SIZE];
array()
{
value[MAX_SIZE] = { 0 };
mark[MAX_SIZE] = 0;
}
}tem;
int main()
{
srand(time(0));
int a[MAX_SIZE];
for (int i = 0; i < MAX_SIZE; i++)
{
a[i] = rand() % MAX_SIZE;
}
for (int i = 0; i < MAX_SIZE; i++)
{
cout << a[i] << " ";
}
cout << endl;
for (int i = 0; i < MAX_SIZE; i++)
{
tem.value[a[i]] = a[i];
}
for (int i = 0; i < MAX_SIZE; i++)
{
if (tem.value[a[i]] == a[i])
{
tem.mark[a[i]]++;
}
}
int index = 0;
for (int i = 0; i < MAX_SIZE; i++)
{
while (tem.mark[i]--)
{
a[index++] = tem.value[i];
}
}
for (int i = 0; i < MAX_SIZE; i++)
{
cout << a[i] << " ";
}
cout << endl;
return 0;
}