for getting O(n), Counting sort will work but limitation is that you must know max element possible in the array.
-Regards Amit Agarwal blog.amitagrwal.com On Fri, Oct 8, 2010 at 5:41 AM, Anand <[email protected]> wrote: > Is there O(n) solution available for it? > > > On Tue, Sep 28, 2010 at 7:19 AM, Nishant Agarwal < > [email protected]> wrote: > >> #include<stdio.h> >> #include<stdlib.h> >> int main() >> { >> int a[20],i,n,max,t,j,k; >> printf("Enter the no. of elements\n"); >> scanf("%d",&n); >> for(i=0;i<n;i++) >> scanf("%d",&a[i]); >> for(i=0;i<n-1;i++) >> { >> j=n-1; >> max=0; >> k=i; >> while(i<j) >> { >> if(a[j]<a[i]&&a[j]>=max) >> { >> max=a[j]; >> k=j; >> j--; >> } >> else >> j--; >> } >> t=a[i]; >> a[i]=a[k]; >> a[k]=t; >> } >> for(i=0;i<n;i++) >> printf("%d\t",a[i]); >> return 0; >> >> } >> >> On Tue, Sep 28, 2010 at 3:43 AM, Chi <[email protected]> wrote: >> >>> Move-To-The-Front. >>> >>> On Sep 27, 11:58 pm, Anand <[email protected]> wrote: >>> > Given an array of integers, for each index i, you have to swap the >>> value at >>> > i with the first value smaller than A[ i ] that comes after index i >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Algorithm Geeks" group. >>> To post to this group, send email to [email protected]. >>> To unsubscribe from this group, send email to >>> [email protected]<algogeeks%[email protected]> >>> . >>> For more options, visit this group at >>> http://groups.google.com/group/algogeeks?hl=en. >>> >>> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]<algogeeks%[email protected]> >> . >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]<algogeeks%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
