@amol : algo seems to work but will fail if size of arr increases.
int arr[]={21,2,7,9,20,15,3,5,0,17,11,12,13,14,1,8,10,4,19,6,18,16};
it will fail for above case.
if we u run outer loop one more time then it will work fine. So there
should be some relation b/w number of elements and minimum number number of
outer loop we need to run.On Mon, Jul 2, 2012 at 4:08 PM, Amol Sharma <[email protected]> wrote: > @atul: > the logic for the O(n) counting is same as for the count sort. > > you haven't write the swap function correctly, > > here is correct code for swap function -- > > > temp=arr[i]; > arr[i]=arr[arr[i]]; > arr[temp]=temp; > > please replace this in your code and it works fine, hope now it's clear. > > > -- > > > Amol Sharma > Final Year Student > Computer Science and Engineering > MNNIT Allahabad > > <http://gplus.to/amolsharma99> > <http://twitter.com/amolsharma99><http://in.linkedin.com/pub/amol-sharma/21/79b/507><http://www.simplyamol.blogspot.com/><http://facebook.com/amolsharma99> > > > > > > > On Mon, Jul 2, 2012 at 3:30 PM, atul anand <[email protected]>wrote: > >> yes it is same and it not working . Apart for this you had provided code >> for sorting O(n) time and not the idea/algo. >> please explain the algorithm , how you are sorting it within O(n) time >> and O(1) space complexity. >> >> On Mon, Jul 2, 2012 at 3:18 PM, Amol Sharma <[email protected]>wrote: >> >>> i don't see any change in the code you posted...other than expanding >>> swap function.... >>> >>> i believe in discussing the idea/algo....not the code.. >>> >>> -- >>> >>> >>> Amol Sharma >>> Final Year Student >>> Computer Science and Engineering >>> MNNIT Allahabad >>> >>> <http://gplus.to/amolsharma99> >>> <http://twitter.com/amolsharma99><http://in.linkedin.com/pub/amol-sharma/21/79b/507><http://www.simplyamol.blogspot.com/><http://facebook.com/amolsharma99> >>> >>> >>> >>> >>> >>> >>> On Mon, Jul 2, 2012 at 12:59 PM, atul anand <[email protected]>wrote: >>> >>>> @amol : >>>> >>>> #include<stdio.h> >>>> >>>> int main() >>>> { >>>> >>>> int arr[]={5,0,1,2,6,7,8,3,4,9}; >>>> int i,j,n,temp; >>>> n=sizeof(arr)/sizeof(arr[0]); >>>> >>>> for(j=0;j<=1;j++) >>>> { >>>> for(i=0;i<n;i++) >>>> { >>>> if(arr[i]!=i){ >>>> temp=arr[i]; >>>> arr[i]=arr[arr[i]]; >>>> arr[arr[i]]=temp; >>>> >>>> } >>>> >>>> } >>>> } >>>> >>>> for(i=0;i<n;i++) >>>> printf("%d ",arr[i]); >>>> printf("\n"); >>>> return 1; >>>> >>>> } >>>> >>>> >>>> >>>> -- >>>> 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. >>>> >>> >>> -- >>> 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. >>> >> >> -- >> 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. >> > > -- > 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. > -- 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.
