add radix sort

This commit is contained in:
krolyxon 2023-09-01 11:31:52 +05:30
parent f8fc421e0b
commit 864708a6de
4 changed files with 46 additions and 6 deletions

View File

@ -1,13 +1,13 @@
## WIP WIP WIP
# TODO
- [] Algorithms to be implemented:
- [ ] Algorithms to be implemented:
- [x] Bubblesort
- [x] SelectionSort
- [x] InsertionSort
- [] Radix Sort
- [x] Radix Sort
- [x] Implement Score system
- [] Code Refactoring/Cleaning
- [ ] Code Refactoring/Cleaning
## Levels:
1: 5 elems\

View File

@ -8,7 +8,7 @@
const int EASY_SCORE_DECREMENT = 10;
const int MEDIUM_SCORE_DECREMENT = 20;
const int LOWER = 1;
const int UPPER = 3;
const int UPPER = 4;
typedef enum Difficulty {
Easy,
@ -56,7 +56,9 @@ int main(int argc, char *argv[]) {
getarr(size);
selectionsort(list, size);
break;
// case 4: radixsort(list); break;
case 4:
radixsort(list, size);
break;
default:
break;
}

View File

@ -39,3 +39,41 @@ void insertionsort(int a[], int n) {
printf("\n");
}
}
int get_max(int a[], int n) {
int max = a[0];
int i;
for (i = 1; i < n; i++)
if (a[i] > max)
max = a[i];
return max;
}
void radixsort(int a[], int n) {
int bucket[10][10], bucket_cnt[10];
int i, j, k, r, NOP = 0, divisor = 1, lar, pass;
lar = get_max(a, n);
while (lar > 0) {
NOP++;
lar /= 10;
}
for (pass = 0; pass < NOP; pass++) {
for (i = 0; i < 10; i++) {
bucket_cnt[i] = 0;
}
for (i = 0; i < n; i++) {
r = (a[i] / divisor) % 10;
bucket[r][bucket_cnt[r]] = a[i];
bucket_cnt[r] += 1;
}
i = 0;
for (k = 0; k < 10; k++) {
for (j = 0; j < bucket_cnt[k]; j++) {
a[i] = bucket[k][j];
i++;
}
}
divisor *= 10;
printarr(a, n);
printf("\n");
}
}

View File

@ -4,4 +4,4 @@
void bubblesort(int a[], int n);
void selectionsort(int a[], int n);
void insertionsort(int a[], int n);
void radixsort(int a[]);
void radixsort(int a[], int n);