Click here to Skip to main content
15,067,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
}
Posted
Updated 28-Nov-17 3:44am
Comments
Richard MacCutchan 28-Nov-17 9:20am
   
Just use simple compare operation to find which number is less than the other. That will work for any value.
Patrice T 28-Nov-17 12:25pm
   
Radix sort imply a special handling.
Radix sort - Wikipedia[^]

1 solution

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

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.
C++
#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;
}

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.
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900