sorting - Print the permutations of integer array in non increasing order -


given array of intergers. how program print permutations of numbers in array. output should sorted in non-increasing order. example array { 12, 4, 66, 8, 9}, output should be:

9866412  9866124  9846612  ....  ....  1246689 

#include<algorithm> #include<iostream> #include<stdio.h> #include<math.h> using namespace std; int totaldigits=0; int x[10000000]; int countx=0; int finddigits(int num) {     int counter=0;     while(num!=0)     {         num=num/10;         counter++;     }     return counter; } int arrdigits(int *a,int size) {     int count=0;     for(int i=0;i<=size;i++)     {     count+=finddigits(a[i]);     }     return count; } int findval(int n) { totaldigits-=finddigits(n); return(pow(10,totaldigits)*n); } void findnum(int *a,int size) {     x[countx]=0;     int n=0;     for(int i=0;i<=size;i++)     {     n+=findval(a[i]);     }     x[countx]=n;     countx++; } void swap(int *a,int *b) { int *temp; *temp=*a; *a=*b; *b=*temp; } void permute(int *arr,int start,int end) { if(start==end) { totaldigits=arrdigits(arr,end); findnum(arr,end); } else {     for(int j=start;j<=end;j++)     {         swap(arr[start],arr[j]);         permute(arr,start+1,end);         swap(arr[start],arr[j]);  //backtrack     } } } int main() { int a[]={12,4,66,8,9}; totaldigits=arrdigits(a,4); permute(a,0,4); sort(x,x+countx); for(int i=countx-1;i>=0;i--) fprintf(stdout,"%ld\n",x[i]); system("pause"); return 0; } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -