-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCountingSort.cpp
More file actions
35 lines (31 loc) · 859 Bytes
/
CountingSort.cpp
File metadata and controls
35 lines (31 loc) · 859 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
#include <stdio.h>
#include <algorithm>
#include <bits/stdc++.h>
#define N 9
using namespace std;
void counting(int a[])
{
int count[9]={0};
int out[9]={0};
for(int i=0; i<9; i++)
++count[a[i]];
//So that we know how many max spaces we need for a number(incase of repitions)
//we reserve space according
//Other numbers don't matter because they add nothing to the sum. So no space for them.
for(int i=1; i<9; i++)
count[i]+=count[i-1];
for(int i=0; i<9; i++)
{
//because the first number will have count of 1 and we need to
//make it 0 in order to correctly fit inside sorted array.
out[count[a[i]]-1]=a[i];
--count[a[i]];
}
for(int i=0; i<9; i++)
cout << out[i] << " ";
}
int main()
{
int a[]={1,4,2,5,8,5,2,2,6};
counting(a);
}