this will be so because when in your case j=MAX-1, then ptr[j+1] will try to access ptr[MAX] which is not existing..so segmentation fault occurs
On Fri, Jun 22, 2012 at 3:35 PM, vindhya chhabra <[email protected]>wrote: > @deepika-ur program was running only for 1 iteration because of ur > condition in the inner loop(i.e. j<MAX-i should be either j<MAX-1 or > j<MAX-i-1) > > #define MAX 10 > void sort(int a[],int *ptr[]) > { > //where p[i]=&a[i] ; > int i,j,*x ; > for(i=0;i<MAX;i++) > { > printf("iterations %d",i); > for(j=0;j<MAX-1;j++) > > { > if((*(ptr[j]))> (*(ptr[j+1]))) //this means A[j] > A[j+1] > { > x = ptr[j+1]; > ptr[j+1] = ptr[j]; > ptr[j] = x; > } > } > }//end of outer loop > for(i=0;i<MAX;i++){ > printf("%d",*ptr[i]); > } > }//end of function > main(){ > int *p[MAX],i,ar[MAX]; > printf("enter the array"); > for(i=0;i<MAX;i++){ > scanf("%d",&ar[i]); > p[i]=&ar[i]; > } > sort(ar,p); > getch(); > } > > > On Fri, Jun 22, 2012 at 2:13 PM, deepikaanand <[email protected]>wrote: > >> //To sort an array of integers by not moving the element itself ..we >> can only use array of pointers...to adjust the pointers >> I have used bubble sort....but it only runs for the first pass when i >> = 0 ..but not for further values of i >> >> void sort(int A[]) >> { >> int i ; >> //int **a = &ptr[0]; >> for(i=0;i<MAX-1;i++) >> { >> cout<<"\n case number = "<<i; >> for(int j = 0;j<MAX-i;j++) >> { >> if((*(ptr[j]))> (*(ptr[j+1]))) //this means A[j] > A[j+1] >> { >> int *a = ptr[j+1]; >> ptr[j+1] = ptr[j]; >> ptr[j] = a; >> display(); >> >> } >> } >> }//end of outer loop >> }//end of function >> >> >> full code :---- >> http://ideone.com/IeSRQ >> >> -- >> 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. >> >> > > > -- > Vindhya Chhabra > > > > -- Vindhya Chhabra -- 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.
