-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbucketSort.cpp
More file actions
64 lines (52 loc) · 1021 Bytes
/
bucketSort.cpp
File metadata and controls
64 lines (52 loc) · 1021 Bytes
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
61
62
63
64
#include <iostream>
#include<cstdlib>
#include <ctime>
using namespace std;
const int SIZE = 32;
const int NUM_BUCKETS = 100;
void print(int* input)
{
for(int i = 1; i <= SIZE; i++)
cout << input[i] << " ";
cout << endl;
}
void bucketSort(int* input)
{
int comp = 0;
int count[NUM_BUCKETS+1] = { 0 };
for(int i = 1; i <= SIZE; i++)
{
count[input[i]]++;
comp++;
}
int curIndex = 1;
for(int i = 1; i <= NUM_BUCKETS; i++)
{
int curCount = count[i];
comp++;
for(int j = 0; j < curCount; j++)
{
input[curIndex] = i;
curIndex++;
comp++;
}
}
cout << "Count: " << comp << endl;
}
int main()
{
srand(time(NULL));
cout << "The array consists of " << SIZE << " elements: " << endl;
int input[SIZE+1] = { 0 };
for(int i = 1; i <= SIZE; i++)
{
int j = rand() % 100+1;
input[i] = j;
}
cout << "Input: " << endl;
print(input);
bucketSort(input);
cout << "Output: " << endl;
print(input);
return 0;
}