Extend the following code to implement bubble sort. Bubble sort is an inefficien
ID: 3903971 • Letter: E
Question
Extend the following code to implement bubble sort. Bubble sort is an inefficient but simple to implement sorting algorithm. It steps through the array one element at a time and compares each pair of adjacent elements. The algorithm swaps the pair if they are in the wrong order. This is repeated until the array is sorted. int element_cmp (void* a, void* b); void bubble_sort (void* elements, size_t n_elements, size_ t size_element, int cmp) (void*, void)) element, 1nt(*cmp) (void* Output the contents of the sorted list afterwards to verify the elements have been sorted.Explanation / Answer
/* Program of sorting using bubble sort */
#include <stdio.h>
#define MAX 20
main()
{
int arr[MAX],i,j,k,temp,n,xchanges;
printf("Enter the number of elements : ");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&arr[i]);
}
printf("Unsorted list is : ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf(" ");
/* Bubble sort*/
for (i = 0; i < n-1 ; i++)
{
xchanges=0;
for (j = 0; j <n-1-i; j++)
{
if (arr[j] > arr[j+1]) //comparison
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
xchanges++;
}/*End of if*/
}/*End of inner for loop*/
if(xchanges==0) /*If list is sorted*/
break;
printf("After Pass %d elements are : ",i+1);
for (k = 0; k < n; k++)
printf("%d ", arr[k]);
printf(" ");
}/*End of outer for loop*/
printf("Sorted list is : ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf(" ");
}/*End of main()*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.