-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountingSort.cpp
More file actions
38 lines (31 loc) · 806 Bytes
/
CountingSort.cpp
File metadata and controls
38 lines (31 loc) · 806 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
#include<bits/stdc++.h>
using namespace std;
void countingSort(int arr[], int n, int max) {
int i, j;
int B[20], C[100];
for(i=0; i<=max; i++) C[i] = 0;
for(j=1; j<=n; j++) C[arr[j]] = C[arr[j]] + 1;
for(i=1; i<=max; i++) C[i] = C[i] + C[i-1];
for(j=n; j>=1; j--) {
B[C[arr[j]]] = arr[j];
C[arr[j]] = C[arr[j]] - 1;
}
printf("The Sorted array is:\n");
for(i=1; i<=n; i++)
printf("%d ", B[i]);
return;
}
int main()
{
int a[50],n,i,max=0;
printf("Enter number of elements: ");
scanf("%d",&n);
printf("Enter elements:\n");
for(i=1; i<=n; ++i) {
scanf("%d",&a[i]);
if(a[i] > max)
max = a[i];
}
countingSort(a, n, max);
return 0;
}