forked from raunakkumarsingh/Cpp_Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountingsort.cpp
More file actions
61 lines (61 loc) · 815 Bytes
/
countingsort.cpp
File metadata and controls
61 lines (61 loc) · 815 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
void swap(int *x,int *y)
{
int temp=*x;
*x=*y;
*y=temp;
}
int findMax(int A[],int n)
{
int max=INT32_MIN;
int i;
for(i=0;i<n;i++)
{
if(A[i]>max)
max=A[i];
}
return max;
}
void CountSort(int A[],int n)
{
int i,j,max,*C;
max=findMax(A,n);
C=(int *)malloc(sizeof(int)*(max+1));
for(i=0;i<max+1;i++)
{
C[i]=0;
}
for(i=0;i<n;i++)
{
C[A[i]]++;
}
i=0;j=0;
while(j<max+1)
{
if(C[j]>0)
{
A[i++]=j;
C[j]--;
}
else
j++;
}
}
int main()
{
int n, i;
cout << "Enter the length of the array: ";
cin >> n;
int arr[n];
cout << "Enter the elements of the array: ";
for (i = 0; i < n; i++)
{
cin >> arr[i];
}
CountSort(arr,n);
for(i=0;i<n;i++)
cout<<arr[i]<<" ";
return 0;
}