T
Tarique
Hello.
I have tried to implement Radix Sort. Can anyone please help
me fix the bug.For the time being i assume all my data(to be sorted)
are of 2 digits.
I ran the code under a debugger.And i see that all the data are properly
hashed to their positions.The error seems to be the retrieval and
rearrangement of data into the array.
Here is the code :
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define NKEYS (sizeof x/sizeof x[0])
struct node{
int data;
struct node *next;
};
struct node* get_node(int n)
{
struct node *z;
z = malloc(sizeof *z);
if(z != NULL)
{
z->data = n;
z->next = NULL;
return z;
}
return NULL;
}
void radixsort(int *x,int n)
{
int i = 0 , j ,k ;
struct node *z;
struct node *p;
struct node *hashtable[10];
struct node *hashend[10];
for(i = 0; i < 10; i++)
{
hashtable = (z = get_node(i));
hashend = (z = get_node(i));
}
for( k = 1; k < 3 ; k++)
{
for(i = 0; i < n; i++)
{
j = (x/(int)pow(10,k-1)) % 10;
z = get_node(x); /*Did not check return value*/
if( hashtable[j]->next == NULL ) {
hashtable[j]->next = z;
hashend[j]->next = z;
}
else {
hashend[j]->next->next = z;
hashend[j]->next = z;
}
}
/*This seems to be the bugged part */
for(j = 0; i < n ; j++)
{
p = hashtable[j];
while( p->next != NULL )
{
x[i++] = p->data;
p = p->next;
}
}
}
return ;
}
int main(void)
{
int i;
int x[] = {34,87,84,23,99};
radixsort(x,NKEYS);
for(i = 0;i < NKEYS; i++)
printf("%d ",x);
return 0;
}
Thank You
I have tried to implement Radix Sort. Can anyone please help
me fix the bug.For the time being i assume all my data(to be sorted)
are of 2 digits.
I ran the code under a debugger.And i see that all the data are properly
hashed to their positions.The error seems to be the retrieval and
rearrangement of data into the array.
Here is the code :
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define NKEYS (sizeof x/sizeof x[0])
struct node{
int data;
struct node *next;
};
struct node* get_node(int n)
{
struct node *z;
z = malloc(sizeof *z);
if(z != NULL)
{
z->data = n;
z->next = NULL;
return z;
}
return NULL;
}
void radixsort(int *x,int n)
{
int i = 0 , j ,k ;
struct node *z;
struct node *p;
struct node *hashtable[10];
struct node *hashend[10];
for(i = 0; i < 10; i++)
{
hashtable = (z = get_node(i));
hashend = (z = get_node(i));
}
for( k = 1; k < 3 ; k++)
{
for(i = 0; i < n; i++)
{
j = (x/(int)pow(10,k-1)) % 10;
z = get_node(x); /*Did not check return value*/
if( hashtable[j]->next == NULL ) {
hashtable[j]->next = z;
hashend[j]->next = z;
}
else {
hashend[j]->next->next = z;
hashend[j]->next = z;
}
}
/*This seems to be the bugged part */
for(j = 0; i < n ; j++)
{
p = hashtable[j];
while( p->next != NULL )
{
x[i++] = p->data;
p = p->next;
}
}
}
return ;
}
int main(void)
{
int i;
int x[] = {34,87,84,23,99};
radixsort(x,NKEYS);
for(i = 0;i < NKEYS; i++)
printf("%d ",x);
return 0;
}
Thank You