# Is there a more efficient way?

 Andrew Cana · April 29, 2015 I'm a begginer in the ways of C++ (as I like to call them xD) and I've sat around one day to come up with an algorithm that sorts the digits of a number so that it creates the biggest number possible and the smallest number possible. I'll leave bellow how I've done it, but I want to know if there's a more efficient way, how would you have done it?  Thanks in advance.``#include using namespace std;long int a;void descending (long int x){int i,v[10];for(i=0;i0){    v[x%10]++;    x/=10;}for(i=9;i>=0;i--){    while(v[i]>0)    {x=x*10+i;    v[i]--;    }}cout``

## Replies

 Mukul Chauhan · April 30, 2015 Hey Andrew, i just wanna let you know that there are more than 8 type of sorting procedures ( atleast that i know hehe ).Talking about efficiency, QuickSort is the most efficient way of sorting.Don't be scared of this long code :p there are alot of comments to explain what is happening Code :#include  void quickSort(int a[], int first, int last);int pivot(int a[], int first, int last);void swap(int& a, int& b);void swapNoTemp(int& a, int& b);void print(int array[], const int& N); using namespace std; int main(){    int test[] = { 7, -13, 1, 3, 10, 5, 2, 4 };    int N = sizeof(test)/sizeof(int);     cout << "Size of test array :"  << N << endl;     cout << "Before sorting : " << endl;    print(test, N);     quickSort(test, 0, N-1);     cout << endl << endl << "After sorting : " << endl;    print(test, N);         return 0;} /** * Quicksort. * @param a - The array to be sorted. * @param first - The start of the sequence to be sorted. * @param last - The end of the sequence to be sorted.*/void quickSort( int a[], int first, int last ) {    int pivotElement;     if(first < last)    {        pivotElement = pivot(a, first, last);        quickSort(a, first, pivotElement-1);        quickSort(a, pivotElement+1, last);    }} /** * Find and return the index of pivot element. * @param a - The array. * @param first - The start of the sequence. * @param last - The end of the sequence. * @return - the pivot element*/int pivot(int a[], int first, int last) {    int  p = first;    int pivotElement = a[first];     for(int i = first+1 ; i <= last ; i++)    {        /* If you want to sort the list in the other order, change "<=" to ">" */        if(a <= pivotElement)        {            p++;            swap(a, a[p]);        }    }     swap(a[p], a[first]);     return p;}  /** * Swap the parameters. * @param a - The first parameter. * @param b - The second parameter.*/void swap(int& a, int& b){    int temp = a;    a = b;    b = temp;} /** * Swap the parameters without a temp variable. * Warning! Prone to overflow/underflow. * @param a - The first parameter. * @param b - The second parameter.*/void swapNoTemp(int& a, int& b){    a -= b;    b += a;// b gets the original value of a    a = (b - a);// a gets the original value of b} /** * Print an array. * @param a - The array. * @param N - The size of the array.*/void print(int a[], const int& N){    for(int i = 0 ; i < N ; i++)        cout << "array[" << i << "] = " << a << endl;}
• 1

## C++

129,893 followers