+ 2
Can u help me. Plis give me an example abt radix sort and merge sort program
2 Réponses
+ 13
void radix_sort(int a[], int length)
{
queue<int> bin[10];
int i;
for (int radix=1; radix <= 1e+9; radix *= 10)
{
for (i = 0; i < length; i++)
bin[a[i] / radix % 10].push(a[i]);
i = 0;
for (int next = 0; next < 10; next++)
while (!bin[next].empty()) {
a[i++] = bin[next].front();
bin[next].pop();
}
}}
+ 11
void merge_sort(int a[], int length)
{
if (length > 1)
{
merge_sort(a, length/2);
merge_sort(a + length/2, length - length/2);
int *sorted = new int[length];
int left = 0, right = length / 2;
int index = 0, bytes = sizeof(a[0]);
while (left < length / 2 && right < length)
sorted[index++] = a[left] < a[right] ?
a[left++] : a[right++];
if (left == length / 2)
memcpy(sorted + index, a + right,
(length - right) * bytes);
else
memcpy(sorted + index, a + left,
(length / 2 - left) * bytes);
memcpy(a, sorted, length * bytes);
delete [] sorted;}}