[英]Radix Sort C++ Assignment
I have a problem with my homework assignment and I do not know where I went wrong with it. 我的家庭作业有问题,我不知道我哪里出错了。 I have to design a function for radix sort with aa buckets and k rounds. 我必须设计一个带有桶和k轮的基数排序功能。 I need to preserve the sequence of the list items in the bucket and therefore I need to keep two points for each bucket - to front and rear. 我需要保留桶中列表项的顺序,因此我需要为每个桶保留两个点 - 前后。 However, when I compile my code and run my test code with 10 numbers that need to be sorted, my output contains only 3 numbers. 但是,当我编译我的代码并运行我需要排序的10个数字的测试代码时,我的输出只包含3个数字。 If its 20 numbers, it prints only 2. Can you help me please? 如果它的20个数字,它只打印2.你能帮我吗? This is my code and thank you for your time. 这是我的代码,谢谢你的时间。 Edit: by leastSigDig i mean Significant Digit i have to change that because its a bad name 编辑:至少SigDig我的意思是重要的数字我必须改变它,因为它的名字不好
#include <cstdlib> // Provides size_t and NULL
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
struct listnode { struct listnode * next;
unsigned long value; } ;
struct listnode *radixsort (struct listnode *data, int a, int k){
struct listnode *front [a], *rear [a], *cursor;
int i=0 , j = 0, leastSigDig,base10num;
if (data == NULL) {return data;}
for (i;i<k;i++){
base10num= pow(a,i);
cursor = data;
for (j=0; j<a; j++){
front [j] = NULL;
rear [j] = NULL;
}
while (cursor != NULL){
leastSigDig = ((cursor->value)/base10num)%a;
if (rear [leastSigDig]!= NULL){
rear[leastSigDig]->next= cursor;
rear [leastSigDig]= cursor;
}
else if (cursor == NULL) {
rear [leastSigDig] = cursor;
}
cursor = cursor->next;
}
//Linking
cursor = NULL;
for (int y=0; y< a-1; y++){
int z= y+1;
if (front [y] == NULL)
continue;
else if (cursor == NULL){
cursor = front [y];
rear [y]->next = front [z];
}
else if (cursor != NULL)
rear [y]->next = front [z];
data = cursor;
}
}
}
return data;
}
int main(void)
{
long i, length=10;
long a = 10; // working with base 10
long k = log10(length*a);
struct listnode *node, *space;
space = (struct listnode *) malloc(length*sizeof(struct listnode));
for( i=0; i< length; i++ ) {
(space + i)->value = 2*((17*i)%length);
(space + i)->next = space + (i+1);
}
(space+(length-1))->next = NULL;
node = space;
struct listnode * temp =node;
cout<<endl<<"List before radixsort\n" <<endl ;
while(temp!=NULL)
{
cout << temp->value << "\t";
temp = temp->next;
}
node = radixsort(node,a,k);
listnode *check = node;
cout << "\n\nList after radixsort \n\n";
while (check)
{
cout << check->value << "\t";
check = check->next;
}
cout << "\n\n";
exit(0);
}
There is at least one problem here: 这里至少有一个问题:
//Linking
int y= 0;
for (int y; y< a-1; y++){
The variable y
in the for
loop shadows the y
in the outer scope. for
循环中的变量y
遮蔽外部范围中的y
。 This means the y
inside the loop is uninitialized and you are off into the weeds. 这意味着循环内部的y
未初始化,您将进入杂草。
You should turn up the compiler warning level and pay attention to what it says. 您应该调高编译器警告级别并注意它所说的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.