F
Foodbank
Hi everyone. I'm having trouble with this radix sorting program. I've
gotten some of it coded except for the actual sorting
The book I'm
teaching myself with (Data Structures Using C and C++) just doesn't
explain things good at all and the tutorials I've viewed don't really
explain least significant digit first sorting or show examples, which
would be most helpful. I've commented the section where I know that
the iteration of the least significant digit sorting goes. Any input
is greatly appreciated.
Thanks,
James
gotten some of it coded except for the actual sorting
teaching myself with (Data Structures Using C and C++) just doesn't
explain things good at all and the tutorials I've viewed don't really
explain least significant digit first sorting or show examples, which
would be most helpful. I've commented the section where I know that
the iteration of the least significant digit sorting goes. Any input
is greatly appreciated.
Thanks,
James
Code:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
int check_order(unsigned int *, int);
void lsd_radix_sort(unsigned int *, int);
main(int argc, char *argv[]) {
int i, nvals = 10000;
unsigned int *array;
if(argc > 1)
nvals = atoi(argv[1]);
array = malloc(nvals * sizeof(unsigned int));
for(i = 0; i < nvals; i++)
array[i] = random();
lsd_radix_sort(array, nvals);
if(i = check_order(array, nvals))
printf("%d misorderings\n", i);
else
printf("array is in order\n");
}
int check_order(unsigned int *ip, int n) {
int i, nrev = 0;
for(i = 1; i < n; i++)
if(ip[i-1] > ip[i])
nrev++;
return(nrev);
}
#define BITS 8 /* specify the radix by number of bits */
#define RADIX (1<<BITS)
#define DIGITS (32/BITS)
void lsd_radix_sort(unsigned int *array, int n) {
int i, j, k, d, shift;
int *bin[RADIX], n_in_bin[RADIX], size_bin[RADIX];
unsigned int v;
//least significant digit first radix sort
}