-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounting-sort.cpp
57 lines (54 loc) · 1.47 KB
/
counting-sort.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
#include <iostream>
#include <algorithm>
using namespace std;
//function to find the max element from the array
int getMax(int a[], int size)
{
int max = a[1];
for (int i = 2; i <= size; i++)
{
if (a[i] > max)
max = a[i];
}
return max;
}
void countSort(int *a, int size);
int main()
{
cout << "Enter the length of array" << endl;
int n;
cin >> n;
int *a = new int(n + 1);
// Getting elements of array
cout << "Enter the elements of array" << endl;
for (int i = 1; i <= n; i++)
cin >> a[i];
cout << "Original array: \n";
for (int i = 1; i <= n; i++)
cout << a[i] << " ";
countSort(a, n);
cout << "\nSorted array: \n";
for (int i = 1; i <= n; i++)
cout << a[i] << " ";
delete (a);
return 0;
}
void countSort(int *a, int size)
{
int output[size + 1];
int max = getMax(a, size);
int count[max + 1]; //create count array (max+1 number of elements)
for (int i = 0; i <= max; i++)
count[i] = 0; //initialize count array to all zero
for (int i = 1; i <= size; i++)
count[a[i]]++; //increase number count in count array.
for (int i = 1; i <= max; i++)
count[i] += count[i - 1]; //find cumulative frequency
for (int i = size; i >= 1; i--)
{
output[count[a[i]]] = a[i];
count[a[i]] -= 1; //decrease count for same numbers
}
for (int i = 1; i <= size; i++)
a[i] = output[i]; //store output array to main array
}