Last active
July 11, 2017 09:35
-
-
Save continue98/801b69d8958450049888c49a73608670 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if defined RADIX_SORT_INC | |
#endinput | |
#endif | |
#define RADIX_SORT_INC | |
#if !defined MAX_SIZE_ARRAY_SORT | |
#define MAX_SIZE_ARRAY_SORT 1000 | |
#endif | |
stock GetMaxValue(arr[], size = sizeof(arr), &index=-1) | |
{ | |
new max_arr = arr[0]; | |
for (new i = 1; i < size; i++) | |
{ | |
if (arr[i] > max_arr) | |
{ | |
max_arr = arr[i]; | |
index = i; | |
} | |
} | |
return max_arr; | |
} | |
stock CountSort(arr[], size = sizeof(arr), exp) | |
{ | |
new output[MAX_SIZE_ARRAY_SORT]; // êîñòûëü, èç íå âîçìîæíîñòè dynamic array | |
new i, count[10]; | |
for (i = 0; i < size; i++) | |
count[(arr[i] / exp) %10] ++; | |
for (i = 1; i < 10; i++) | |
count[i] += count[i - 1]; | |
for (i = size - 1; i >= 0; i--) | |
{ | |
output[count[(arr[i] / exp) % 10] - 1] = arr[i]; | |
count[(arr[i] / exp) % 10]--; | |
} | |
for (i = 0; i < size; i++) | |
arr[i] = output[i]; | |
} | |
stock RadixSort(arr[], size = sizeof(arr)) | |
{ | |
new max_val = GetMaxValue(arr, size); | |
for (new exp = 1; max_val / exp > 0; exp *= 10) | |
CountSort(arr, size, exp); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment