Hi Ritesh Yeah, you are right. we do not need to sort. my bad Thank you for explaining clearly
On Thu, Dec 27, 2012 at 4:29 AM, Ritesh Mishra <[email protected]> wrote: > @anurag : there is no need to SORT. as it will increase complexity O(n) to > O(n log n). > here is the correct code.. please look over it and notify me if i'm wrong . > > T.C. = O( n ) > > // ex: "1 4 3 2 0" i'm explaining on behalf of it. > bool permute (int *arr , int N ) > { > if ( N<=1 ) return false; > > for ( int i = N-2 ; i >= 0 ; i-- ) > { > if ( arr[i+1] > arr[i]) // now i points to 1 > { > for ( int j = N-1 ; j > i ; j--) > { > if ( arr[i]< arr[j]) // now j points to 2(just greater > than 1 ) > { > swap(arr[i],arr[j]) ;//this will generate "2 4 3 1 0" > since 4310 are already sorted in reverse > //thus no need to sort again in > inc order rather than reversing > i++ ; j = N-1; > while(i<j) swap(arr[i++],arr[j--]) ; // reversing the > seq 4..0 to 0..4 > return true ; > } > } > } > } > return false ; > } > > > Regards, > > Ritesh Kumar Mishra > Information Technology > Third Year Undergraduate > MNNIT Allahabad > > > On Mon, Dec 24, 2012 at 7:56 PM, marti <[email protected]> wrote: > >> >> I REPEAT, THERE is no need to SORT; >> >> >> http://en.wikipedia.org/wiki/Permutation#Lexicographical%5Forder%5Fgeneration >> >> >> On Friday, December 14, 2012 11:56:16 AM UTC+5:30, tapan rathi wrote: >> >>> For a given number, find the next greatest number which is just greater >>> than previous one and made up of same digits. >> >> -- >> >> >> > > -- > > > -- Regards Anurag Gupta IV Year Computer Engineering Delhi Technological University (Formerly Delhi College of Engineering) --
