Krolyxon
Commit 864708a

Branches

Refs →

tros864708a

add radix sort

Author
krolyxon
Date
2023-09-01 11:31 +0530
Parent
f8fc421
Commit
864708a6deec0443097111c6d0ffab47941fdac0

+46 −6

4 files changed

  1. M README.md +3 −3
  2. M src/main.c +4 −2
  3. M src/sort.c +38 −0
  4. M src/sort.h +1 −1
M README.md View
@@ -1,13 +1,13 @@
11 ## WIP WIP WIP
22
33 # TODO
4−- [] Algorithms to be implemented:
4+- [ ] Algorithms to be implemented:
55 - [x] Bubblesort
66 - [x] SelectionSort
77 - [x] InsertionSort
8− - [] Radix Sort
8+ - [x] Radix Sort
99 - [x] Implement Score system
10−- [] Code Refactoring/Cleaning
10+- [ ] Code Refactoring/Cleaning
1111
1212 ## Levels:
1313 1: 5 elems\
M src/main.c View
@@ -8,7 +8,7 @@
88 const int EASY_SCORE_DECREMENT = 10;
99 const int MEDIUM_SCORE_DECREMENT = 20;
1010 const int LOWER = 1;
11−const int UPPER = 3;
11+const int UPPER = 4;
1212
1313 typedef enum Difficulty {
1414 Easy,
@@ -56,7 +56,9 @@ int main(int argc, char *argv[]) {
5656 getarr(size);
5757 selectionsort(list, size);
5858 break;
59− // case 4: radixsort(list); break;
59+ case 4:
60+ radixsort(list, size);
61+ break;
6062 default:
6163 break;
6264 }
M src/sort.c View
@@ -39,3 +39,41 @@ void insertionsort(int a[], int n) {
3939 printf("\n");
4040 }
4141 }
42+
43+int get_max(int a[], int n) {
44+ int max = a[0];
45+ int i;
46+ for (i = 1; i < n; i++)
47+ if (a[i] > max)
48+ max = a[i];
49+ return max;
50+}
51+void radixsort(int a[], int n) {
52+ int bucket[10][10], bucket_cnt[10];
53+ int i, j, k, r, NOP = 0, divisor = 1, lar, pass;
54+ lar = get_max(a, n);
55+ while (lar > 0) {
56+ NOP++;
57+ lar /= 10;
58+ }
59+ for (pass = 0; pass < NOP; pass++) {
60+ for (i = 0; i < 10; i++) {
61+ bucket_cnt[i] = 0;
62+ }
63+ for (i = 0; i < n; i++) {
64+ r = (a[i] / divisor) % 10;
65+ bucket[r][bucket_cnt[r]] = a[i];
66+ bucket_cnt[r] += 1;
67+ }
68+ i = 0;
69+ for (k = 0; k < 10; k++) {
70+ for (j = 0; j < bucket_cnt[k]; j++) {
71+ a[i] = bucket[k][j];
72+ i++;
73+ }
74+ }
75+ divisor *= 10;
76+ printarr(a, n);
77+ printf("\n");
78+ }
79+}
M src/sort.h View
@@ -4,4 +4,4 @@
44 void bubblesort(int a[], int n);
55 void selectionsort(int a[], int n);
66 void insertionsort(int a[], int n);
7−void radixsort(int a[]);
7+void radixsort(int a[], int n);