user466534
user466534

Reputation:

sort numbers by digit

why this code does not work? it does not show me output

#include <stdlib.h>
#include <iostream>
#include <string.h>
void Sort(int *arr,int length){
    int *iter=arr;
    char buf[12],buf1[12];
     while ((iter++)< (arr+length)){

          if (iter==arr || (strcmp(itoa(*iter,buf,10),itoa(*(iter-1),buf1,10))>=0)){
       iter++;
          }
          else{
              *iter^=*(iter+1);
              *(iter+1)^=*iter;
              *iter^=*(iter+1);
              iter--;
          }

          }


     }

     int main(){

         int a[]={1,2,10,100,19,21,2,4,31};
         int n=sizeof(a)/sizeof(int);
         Sort(a,n);
          for(int i=0;i<n;i++)
            std::cout<<a[i]<<"  ";




          return 0;
     }

please help

Upvotes: 0

Views: 568

Answers (1)

ereOn
ereOn

Reputation: 55726

Here is the output using gcc 4.5.1:

> g++ -o test test.cpp
> test.exe
1  2  10  100  19  21  2  4  31

As you can see, it compiles and runs fine in my place. Whether it works like intended is another matter though.

Are you sure you saved your changes before compiling ? What compiler are you using ?


Moreover, you should better use a std::vector to store the integers and std::sort with a custom comparator object to do the sort.

Upvotes: 3

Related Questions