Skip to content

Commit da7bf77

Browse files
committed
counting sort added for dynamic range
1 parent 81bd337 commit da7bf77

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ TODO: question14.c
1111
- duplicate elimination from an array
1212
- union, intersection and difference of two arrays
1313
- bucket sort
14+
- radix sort
15+
- for m to m+k range too
1416

1517

1618
### General Questions

arrays/question7.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
COUNTING SORT
3+
numbers to be sorted should be in a range eg (1-5) or (m to m+k)
4+
5+
6+
Time complexity
7+
O(n+k) k = range of numbers n= number of elemnts in input
8+
9+
Space complexity = size of DS
10+
O(k)
11+
*/
12+
13+
#include <stdio.h>
14+
#include <stdlib.h>
15+
#include <math.h>
16+
17+
18+
void count_sort(int a[], int b[], int range, int min, int length){
19+
int size = range+1;
20+
int c[size];
21+
int i,j;
22+
23+
for(i=0;i<size;i++){
24+
c[i]=0;//initializing all elms in c to zero
25+
}
26+
for(i=0;i<length;i++){
27+
c[a[i]-min]++;
28+
}
29+
for(i=1; i<size;i++){
30+
c[i]=c[i-1]+c[i];
31+
}
32+
for(j=length-1;j>=0;j--){
33+
b[c[a[j]-min]-1]=a[j]; //-1 is done to match with 0 index
34+
c[a[j]-min]--;
35+
}
36+
//printing the array
37+
for(i=0; i<length;i++){
38+
printf("%d ", b[i]);
39+
}
40+
}
41+
42+
int main(){
43+
int a[] = {5,6,8,5,9,5};
44+
int length = sizeof(a)/sizeof(a[0]);
45+
int b[length];
46+
int range = 9-5; //max number
47+
int min = 5; //min number
48+
count_sort(a,b,range,min,length);
49+
}

arrays/question8.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
RADIX SORT
3+
numbers to be sorted should be in a range eg (1-5) or (m to m+k)
4+
5+
6+
Time complexity
7+
O(n+k) k = range of numbers n= number of elemnts in input
8+
9+
Space complexity = size of DS
10+
O(k)
11+
*/
12+
13+
#include <stdio.h>
14+
#include <stdlib.h>
15+
#include <math.h>
16+
17+
18+
19+
int main(){
20+
int a[] = {1,1,4,4,2,5,6,8,3};
21+
22+
}

0 commit comments

Comments
 (0)