Merge pull request #497 from ubc1729/master

Indented "counting Sort" and fixed compilation errors
This commit is contained in:
Ashwek Swamy 2019-11-09 23:34:53 +05:30 committed by GitHub
commit 70c11c370a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,19 +1,36 @@
/*
> Counting sort is a sorting technique based on keys between a specific range.
> integer sorting algorithm
> Worst-case performance O(n+k)
> Stabilized by prefix sum array
*/
#include <stdio.h> #include <stdio.h>
#include <string.h>
int main() int main()
{ {
int i, n, l = 0; int i, n, l = 0;
printf("Enter size of array = ");
scanf("%d", &n); scanf("%d", &n);
int a[n]; int a[n];
printf("Enter %d elements in array :\n", n);
for(i = 0; i < n; i++) for(i = 0; i < n; i++)
{ {
scanf("%d", &a[i]); scanf("%d", &a[i]);
if(a[i] > l) if(a[i] > l)
l = a[i]; l = a[i];
} }
int b[l+1]={0};
int b[l + 1];
memset(b, 0, (l + 1) * sizeof(b[0]));
for(i = 0; i < n; i++) for(i = 0; i < n; i++)
b[a[i]]++; //hashing number to array index b[a[i]]++; //hashing number to array index
for(i=0;i<(l+1);i++)
for(i = 0; i < (l + 1); i++) //unstable , stabilized by prefix sum array
{ {
if(b[i] > 0) if(b[i] > 0)
{ {
@ -24,5 +41,6 @@
} }
} }
} }
return 0; return 0;
} }