-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountsort.cpp
More file actions
70 lines (63 loc) · 1.4 KB
/
Copy pathcountsort.cpp
File metadata and controls
70 lines (63 loc) · 1.4 KB
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
62
63
64
65
66
67
68
/*#include <iostream>
using namespace std;
void Counting_sort(int [], int, int);
int main()
{
int n,k = 0, A[15];
cout << "Enter the number of input : ";
cin >> n;
cout << "\nEnter the elements to be sorted :\n";
for ( int i = 1; i <= n; i++)
{
cin >> A[i];
if(A[i] > k)
{
k = A[i];
}
}
Counting_sort(A, k, n);
}
void Counting_sort(int A[], int k, int n)
{
int i, j;
int B[15], C[100];
for(i = 0; i <= k; i++)
C[i] = 0;
for(j =1; j <= n; j++)
C[A[j]] = C[A[j]] + 1;
for(i = 1; i <= k; i++)
C[i] = C[i] + C[i-1];
for(j = n; j >= 1; j--)
{
B[C[A[j]]] = A[j];
C[A[j]] = C[A[j]] - 1;
}
cout << "\nThe Sorted array is : ";
for(i = 1; i <= n; i++)
cout << B[i] << " " ;
}*/
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
using namespace std;
void countSort(int arr[],int n,int k){
int result[100];
int count[100];
for(int i=0;i<k;i++)
count[i]=0;
for(int i=0;i<n;i++)
++count[arr[i]];
for(int i=1;i<k;i++)
count[i]=count[i-1];
for(int i=n-1;i>=0;i--){
result[count[arr[i]]]=arr[i];
--count[arr[i]];
}
for(int i=0;i<n;i++)
cout<<result[i]<<" ";
}
int main(){
int arr[]={10,10,3,3,3,3,2,1,1,7};
countSort(arr,10,10);
}