-
Notifications
You must be signed in to change notification settings - Fork 2
/
countingsort.cpp
172 lines (130 loc) · 3.32 KB
/
countingsort.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
Dong Lin
CSCI 115 - 1 PM Lab
Lab 5 - Counting Sort
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
using namespace std;
const int SIZE = 100000;
void countingSort(int *inArr, int size);
void display(int *arr, int size);
void initDisplay(int* arr, int size);
int main()
{
int* input = new int[SIZE];
//int input[] = { 2,0,1,0 };
//int size = sizeof(input) / sizeof(input[0]);
//initDisplay(input, size);
srand(time(NULL));
for (int i = 0; i < SIZE; i++)
{
input[i] = rand() % (SIZE);
}
//display(input, SIZE);
clock_t t = clock();
countingSort(input, SIZE);
t = clock() - t;
//display(input, SIZE);
cout << "Time to sort " << SIZE << " integers using counting sort: "
<< (float) t / CLOCKS_PER_SEC << " seconds." << endl;
delete[] input;
return 0;
}
void countingSort(int *inArr, int size)
{
int max = *max_element(inArr, inArr + size);
int* outArr = new int[size];
int* auxArr = new int[max + 1];
for (int i = 0; i <= max; i++)
{
auxArr[i] = 0;
}
for (int i = 0; i < size; i++)
{
auxArr[inArr[i]]++;
}
for (int i = 1; i <= max; i++)
{
auxArr[i] += auxArr[i - 1];
}
for (int i = size - 1; i >= 0; i--)
{
outArr[--auxArr[inArr[i]]] = inArr[i];
}
for (int i = 0; i < size; i++)
{
inArr[i] = outArr[i];
}
delete[] outArr;
delete[] auxArr;
}
void display(int *arr, int size)
{
cout << "[ ";
for (int i = 0; i < size; i++)
{
cout << arr[i] << ' ';
}
cout << ']';
cout << endl;
}
void initDisplay(int* arr, int size)
{
vector<int> tmpVec(arr, arr + size);
cout << "Input Array = ";
display(arr, size);
cout << "Expected Output = ";
sort(tmpVec.begin(), tmpVec.end());
cout << "[ ";
for (vector<int>::iterator itr = tmpVec.begin(); itr != tmpVec.end(); itr++)
{
cout << *itr << ' ';
}
cout << "]" << endl;
cout << "Initial Auxiliary array= ";
int max = *max_element(arr, arr + size);
int auxSize = max + 1;
int *tmpAux = new int[auxSize];
for (int i = 0; i < auxSize; i++)
{
tmpAux[i] = 0;
}
display(tmpAux, auxSize);
cout << "Auxiliary array after adding the count of each element= ";
for (int i = 0; i < size; i++)
{
tmpAux[arr[i]]++;
}
display(tmpAux, auxSize);
cout << "Auxiliary array after cumulation= ";
for (int i = 1; i < auxSize; i++)
{
tmpAux[i] += tmpAux[i - 1];
}
display(tmpAux, auxSize);
cout << "\nBuilding output array:" << endl;
int* outArr = new int[size];
vector<char> cVec(size, 'x');
for (int i = size - 1; i >= 0; i--)
{
static int count = 1;
cout << "Iteration " << count++ << ':' << endl;
cVec[tmpAux[arr[i]] - 1] = '0' + arr[i];
outArr[--tmpAux[arr[i]]] = arr[i];
cout << "Output array= ";
cout << "[ ";
for (vector<char>::iterator itr = cVec.begin(); itr != cVec.end(); itr++)
{
cout << *itr << ' ';
}
cout << "]" << endl;
cout << "Auxiliary array=";
display(tmpAux, auxSize);
}
delete[] tmpAux;
delete[] outArr;
cout << endl;
}