Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

} Use the pointer and pointer expression to do the bubble sort. To improve the p

ID: 3532925 • Letter: #

Question

}Use the pointer and pointer expression to do the bubble sort. To improve the performance, modify the sort to check at the end of each of pass whether any swaps have been made. If none has been made, then the data must already be in the proper order, so the program should terminate. If swaps have been made, then at least one more pass is needed.

How do I come up with a code to run this program?
}Use the pointer and pointer expression to do the bubble sort. To improve the performance, modify the sort to check at the end of each of pass whether any swaps have been made. If none has been made, then the data must already be in the proper order, so the program should terminate. If swaps have been made, then at least one more pass is needed.

How do I come up with a code to run this program?

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>

int bubblesort(int *,int);

int bubblesort(int *arr,int n)

{

int i,j,temp;

for(i=0;i<(n-1);i++)

for(j=0;j<(n-1-i);j++)

{

if(*(arr+j)>*(arr+(j+1)))

{

temp=*(arr+j);

*(arr+j)=*(arr+(j+1));

*(arr+(j+1))=temp;

}

}

return 0;

}

int main()

{

int *arr,i,n;

printf("Enter no. of terms ");

scanf("%d",&n);

arr=(int *)malloc(n*sizeof(int));

printf("Enter the elements ");

for(i=0;i<n;i++)

scanf("%d",(arr+i));

bubblesort(arr,n);

printf("Sorted elements are ");

for(i=0;i<n;i++)

printf("%d ",*(arr+i));

return 0;

}