-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountingSort.c
57 lines (51 loc) · 985 Bytes
/
CountingSort.c
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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define SIZE 11
int *CountingSort(int *arr,int n,int k)
{
int *B;
B=(int *)malloc(sizeof(int)*SIZE);
int C[k];
int i;
for(i=0;i<k;i++)
C[i]=0;
for(i=1;i<n;i++)
C[arr[i]]++;
for(i=1;i<k;i++)
C[i]+=C[i-1];
for(i=n-1;i>=1;i--)
{
B[C[arr[i]]]=arr[i];
C[arr[i]]=C[arr[i]]-1;
}
return B;
}
void Display(int *arr,int n)
{
int i;
for(i=1;i<n;i++)
{
printf("%d ",arr[i]);
}
printf("\n");
}
void main()
{
int *arr;
arr=(int *)malloc(sizeof(int)*SIZE);
int i;
printf("Enter the range of array elements\n");
int range;
scanf("%d",&range);
printf("Enter ten array elements\n");
for(i=1;i<=SIZE;i++)
scanf("%d",&arr[i]);
printf("The initial array is \n");
Display(arr,SIZE);
printf("Sorting the array using Counting Sort\n");
int *X=CountingSort(arr,SIZE,range);
printf("The sorted array is\n");
Display(X,SIZE);
getch();
}