不能排序超过2位数的基数排序C ++ [英] Cant sort more than 2 digit numbers radix sort C++

查看:73
本文介绍了不能排序超过2位数的基数排序C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该程序可以对0-9个数字进行排序,但在上述10个数字时无法正确排序



我的尝试:



the program can sort 0-9 numbers but cant sort properly when 10 above is involve

What I have tried:

#include <iostream>
#include <math.h>
using namespace std;


int main() {
 int sa,temp1=0,largestnum=0,digits=0,digitvalue,temp2=0,divs=1,locator,reset,switcher=0,nset=1,singledigit;
 int value,testvalue,c1=0,c2=0,c3=0,c4=0,c5=0,c6=0,c7=0,c8=0,c9=0,c0=0,s1=0,s2=0,s3=0,s4=0,s5=0,s6=0,s7=0,s8=0,s9=0,s0=0;
   cout<<"Enter size of array: ";cin>>sa;
    int array2[sa]={};
    int array3[sa]={};
    int q0[sa]={};
    int q1[sa]={};
    int q2[sa]={};
    int q3[sa]={};
    int q4[sa]={};
    int q5[sa]={};
    int q6[sa]={};
    int q7[sa]={};
    int q8[sa]={};
    int q9[sa]={};
   for(int i=0;i<sa;i++){
    cout<<"Enter value of array "<<i<<":";cin>>temp1;
    array2[i]=temp1;
    array3[i]=0;
   }
    cout<<"The values of the array are :";
    for(int i=0;i<sa;i++){

    cout<<array2[i]<<"|";
    }
    for(int i=0;i<sa;i++){
        if(i==0){largestnum=array2[i];}
        else{temp1=array2[i];
                if(temp1>largestnum){largestnum=temp1;}
        }
    }
    while(largestnum>9){
            largestnum=largestnum/10;
            digits=digits+1;
    }
    digits=digits+1;



    for(int i=1;digits>0;i*10){
            divs=i;

           for(int placement=0;placement<sa;placement++)
            {
                testvalue=array2[placement];
                if(testvalue<10){
                                    if(i==1){digitvalue=testvalue;}
                                    else{digitvalue=0;}
                }
                else if(testvalue>=10){digitvalue=(testvalue/divs)%10;}

                if(digitvalue==0){q0[c0]=testvalue;c0=c0+1;}
                else if(digitvalue==1){q1[c1]=testvalue;c1=c1+1;}
                else if(digitvalue==2){q2[c2]=testvalue;c2=c2+1;}
                else if(digitvalue==3){q3[c3]=testvalue;c3=c3+1;}
                else if(digitvalue==4){q4[c4]=testvalue;c4=c4+1;}
                else if(digitvalue==5){q5[c5]=testvalue;c5=c5+1;}
                else if(digitvalue==6){q6[c6]=testvalue;c6=c6+1;}
                else if(digitvalue==7){q7[c7]=testvalue;c7=c7+1;}
                else if(digitvalue==8){q8[c8]=testvalue;c8=c8+1;}
                else if(digitvalue==9){q9[c9]=testvalue;c9=c9+1;}
            }
            for(int show=0;show<sa;show++)
                {
                  if(c0>=1){array3[show]=q0[s0];c0=c0-1;q0[s0]=0;s0=s0+1;}
                  else if(c1>=1){array3[show]=q1[s1];c1=c1-1;q1[s1]=0;s1=s1+1;}
                  else if(c2>=1){array3[show]=q2[s2];c2=c2-1;q2[s2]=0;s2+=s2+1;}
                  else if(c3>=1){array3[show]=q3[s3];c3=c3-1;q3[s3]=0;s3+=s3+1;}
                  else if(c4>=1){array3[show]=q4[s4];c4=c4-1;q4[s4]=0;s4+=s4+1;}
                  else if(c5>=1){array3[show]=q5[s5];c5=c5-1;q5[s5]=0;s5+=s5+1;}
                  else if(c6>=1){array3[show]=q6[s6];c6=c6-1;q6[s6]=0;s6+=s6+1;}
                  else if(c7>=1){array3[show]=q7[s7];c7=c7-1;q7[s7]=0;s7+=s7+1;}
                  else if(c8>=1){array3[show]=q8[s8];c8=c8-1;q8[s8]=0;s8+=s8+1;}
                  else if(c9>=1){array3[show]=q9[s9];c9=c9-1;q9[s9]=0;s9+=s9+1;}
                }
                c1=0;c2=0;c3=0;c4=0;c5=0;c6=0;c7=0;c8=0;c9=0;c0=0;
                s1=0,s2=0;s3=0;s4=0;s5=0;s6=0;s7=0;s8=0;s9=0;s0=0;
                cout<<"\n"<<"Set "<<nset<<"\n";
                nset=nset+1;
                for(int output=0;output<sa;output++){
                cout<<array3[output]<<"|";
                array2[output]=array3[output];

                }

                digits=digits-1;
    }




   return 0;
}

推荐答案

Quote:

该程序可以排序0-9个数字,但当涉及10个以上时无法正确排序

the program can sort 0-9 numbers but cant sort properly when 10 above is involve



显示输入和输出失败的示例是个好主意,它有助于帮助者了解出了什么问题。



对于简单的基数排序,你的代码看起来很复杂,甚至很难看出发生了什么。据说基数排序需要2个数组副本,而不是12个副本。

基数排序 - 维基百科 [ ^ ]

我想你需要进一步研究数组的概念。

-----

学会正确缩进你的代码,它显示它的结构,它有助于阅读和理解。不要将内容打包在一行代码中,这样会使读取更加困难而没有任何优势。


It is a good idea to show an example of input and output that fail, it help helpers to understand what is going wrong.

Your code looks way complicated for a simple radix sort, it is even difficult to see what is going on. It is said that the radix sort need 2 copies of the array, not twelve copies.
Radix sort - Wikipedia[^]
I guess you need to study further the concept of array.
-----
Learn to indent properly your code, it show its structure and it helps reading and understanding. Do not pack things in a single line of code, it makes it more difficult to read without any advantage.

#include <iostream>
#include <math.h>
using namespace std;


int main() {
    int sa,temp1=0,largestnum=0,digits=0,digitvalue,temp2=0,divs=1,locator,reset,switcher=0,nset=1,singledigit;
    int value,testvalue,c1=0,c2=0,c3=0,c4=0,c5=0,c6=0,c7=0,c8=0,c9=0,c0=0,s1=0,s2=0,s3=0,s4=0,s5=0,s6=0,s7=0,s8=0,s9=0,s0=0;
    cout<<"Enter size of array: ";
    cin>>sa;

    int array2[sa]={};
    int array3[sa]={};
    int q0[sa]={};
    int q1[sa]={};
    int q2[sa]={};
    int q3[sa]={};
    int q4[sa]={};
    int q5[sa]={};
    int q6[sa]={};
    int q7[sa]={};
    int q8[sa]={};
    int q9[sa]={};
    for(int i=0;i<sa;i++){
        cout<<"Enter value of array "<<i<<":";
        cin>>temp1;
        array2[i]=temp1;
        array3[i]=0;
    }
    cout<<"The values of the array are :";
    for(int i=0;i<sa;i++){
        cout<<array2[i]<<"|";
    }
    for(int i=0;i<sa;i++){
        if(i==0){
            largestnum=array2[i];
        }
        else{
            temp1=array2[i];
            if(temp1>largestnum){
                largestnum=temp1;
            }
        }
    }
    while(largestnum>9){
        largestnum=largestnum/10;
        digits=digits+1;
    }
    digits=digits+1;

    for(int i=1;digits>0;i*10){
        divs=i;

        for(int placement=0;placement<sa;placement++)
        {
            testvalue=array2[placement];
            if(testvalue<10){
                if(i==1){
                    digitvalue=testvalue;
                }
                else{
                    digitvalue=0;
                }
            }
            else if(testvalue>=10){
                digitvalue=(testvalue/divs)%10;
            }

            if(digitvalue==0){
                q0[c0]=testvalue;
                c0=c0+1;
            }
            else if(digitvalue==1){
                q1[c1]=testvalue;
                c1=c1+1;
            }
            else if(digitvalue==2){
                q2[c2]=testvalue;
                c2=c2+1;
            }
            else if(digitvalue==3){
                q3[c3]=testvalue;
                c3=c3+1;
            }
            else if(digitvalue==4){
                q4[c4]=testvalue;
                c4=c4+1;
            }
            else if(digitvalue==5){
                q5[c5]=testvalue;
                c5=c5+1;
            }
            else if(digitvalue==6){
                q6[c6]=testvalue;
                c6=c6+1;
            }
            else if(digitvalue==7){
                q7[c7]=testvalue;
                c7=c7+1;
            }
            else if(digitvalue==8){
                q8[c8]=testvalue;
                c8=c8+1;
            }
            else if(digitvalue==9){
                q9[c9]=testvalue;
                c9=c9+1;
            }
        }
        for(int show=0;show<sa;show++)
        {
            if(c0>=1){
                array3[show]=q0[s0];
                c0=c0-1;
                q0[s0]=0;
                s0=s0+1;
            }
            else if(c1>=1){
                array3[show]=q1[s1];
                c1=c1-1;
                q1[s1]=0;
                s1=s1+1;
            }
            else if(c2>=1){
                array3[show]=q2[s2];
                c2=c2-1;
                q2[s2]=0;
                s2+=s2+1;
            }
            else if(c3>=1){
                array3[show]=q3[s3];
                c3=c3-1;
                q3[s3]=0;
                s3+=s3+1;
            }
            else if(c4>=1){
                array3[show]=q4[s4];
                c4=c4-1;
                q4[s4]=0;
                s4+=s4+1;
            }
            else if(c5>=1){
                array3[show]=q5[s5];
                c5=c5-1;
                q5[s5]=0;
                s5+=s5+1;
            }
            else if(c6>=1){
                array3[show]=q6[s6];
                c6=c6-1;
                q6[s6]=0;
                s6+=s6+1;
            }
            else if(c7>=1){
                array3[show]=q7[s7];
                c7=c7-1;
                q7[s7]=0;
                s7+=s7+1;
            }
            else if(c8>=1){
                array3[show]=q8[s8];
                c8=c8-1;
                q8[s8]=0;
                s8+=s8+1;
            }
            else if(c9>=1){
                array3[show]=q9[s9];
                c9=c9-1;
                q9[s9]=0;
                s9+=s9+1;
            }
        }
        c1=0;c2=0;c3=0;c4=0;c5=0;c6=0;c7=0;c8=0;c9=0;c0=0;
        s1=0,s2=0;s3=0;s4=0;s5=0;s6=0;s7=0;s8=0;s9=0;s0=0;
        cout<<"\n"<<"Set "<<nset<<"\n";
        nset=nset+1;
        for(int output=0;output<sa;output++){
            cout<<array3[output]<<"|";
            array2[output]=array3[output];
        }
        digits=digits-1;
    }
    return 0;
}



专业程序员的编辑器具有此功能,其他功能包括括号匹配和语法高亮。

Notepad++主页 [ ^ ]

ultraedit [ ^ ]

-----

有一个工具可以让你要查看代码正在执行的操作,其名称为调试程序。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
-----
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于不能排序超过2位数的基数排序C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆