tros864708a
add radix sort
+46 −6
4 files changed
- M README.md +3 −3
- M src/main.c +4 −2
- M src/sort.c +38 −0
- M src/sort.h +1 −1
| @@ -1,13 +1,13 @@ | |||
| 1 | 1 | ## WIP WIP WIP | |
| 2 | 2 | ||
| 3 | 3 | # TODO | |
| 4 | − | - [] Algorithms to be implemented: | |
| 4 | + | - [ ] Algorithms to be implemented: | |
| 5 | 5 | - [x] Bubblesort | |
| 6 | 6 | - [x] SelectionSort | |
| 7 | 7 | - [x] InsertionSort | |
| 8 | − | - [] Radix Sort | |
| 8 | + | - [x] Radix Sort | |
| 9 | 9 | - [x] Implement Score system | |
| 10 | − | - [] Code Refactoring/Cleaning | |
| 10 | + | - [ ] Code Refactoring/Cleaning | |
| 11 | 11 | ||
| 12 | 12 | ## Levels: | |
| 13 | 13 | 1: 5 elems\ | |
| @@ -8,7 +8,7 @@ | |||
| 8 | 8 | const int EASY_SCORE_DECREMENT = 10; | |
| 9 | 9 | const int MEDIUM_SCORE_DECREMENT = 20; | |
| 10 | 10 | const int LOWER = 1; | |
| 11 | − | const int UPPER = 3; | |
| 11 | + | const int UPPER = 4; | |
| 12 | 12 | ||
| 13 | 13 | typedef enum Difficulty { | |
| 14 | 14 | Easy, | |
| @@ -56,7 +56,9 @@ int main(int argc, char *argv[]) { | |||
| 56 | 56 | getarr(size); | |
| 57 | 57 | selectionsort(list, size); | |
| 58 | 58 | break; | |
| 59 | − | // case 4: radixsort(list); break; | |
| 59 | + | case 4: | |
| 60 | + | radixsort(list, size); | |
| 61 | + | break; | |
| 60 | 62 | default: | |
| 61 | 63 | break; | |
| 62 | 64 | } | |
| @@ -39,3 +39,41 @@ void insertionsort(int a[], int n) { | |||
| 39 | 39 | printf("\n"); | |
| 40 | 40 | } | |
| 41 | 41 | } | |
| 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 | + | } | |
| @@ -4,4 +4,4 @@ | |||
| 4 | 4 | void bubblesort(int a[], int n); | |
| 5 | 5 | void selectionsort(int a[], int n); | |
| 6 | 6 | void insertionsort(int a[], int n); | |
| 7 | − | void radixsort(int a[]); | |
| 7 | + | void radixsort(int a[], int n); | |