-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path96.cpp
61 lines (38 loc) · 773 Bytes
/
96.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
// Count Sort
#include<iostream>
using namespace std;
void countSort(int arr[],int n){
int k=arr[0];
for (int i = 0; i < n; i++)
{
k=max(k,arr[i]);
}
int count[10]={0};
// counting the no. of elements in the given array
for (int i = 0; i < n; i++)
{
count[arr[i]]++;
}
for (int i = 1; i <= k; i++)
{
count[i]+=count[i-1];
}
int output[n];
for (int i = n-1; i >=0; i--)
{
output[--count[arr[i]]]=arr[i];
}
for (int i = 0; i < n; i++)
{
arr[i]=output[i];
}
}
int main(){
int arr[]={1,3,2,3,4,1,6,4,3};
countSort(arr,9);
for (int i = 0; i < 9; i++)
{
cout<<arr[i]<<" ";
}
return 0;
}