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

The bubble sort is another technique for sorting an array. A bubble sort com- pa

ID: 3807560 • Letter: T

Question

The bubble sort is another technique for sorting an array. A bubble sort com- pares adjacent array elements and exchanges their values if they are out of order. In this way, the smaller values “bubble” to the top of the array (toward element 0), while the larger values sink to the bottom of the array. After the first pass of a bubble sort, the last array element is in the correct position; after the second pass the last two elements are correct, and so on. Thus, after each pass, the unsorted portion of the array contains one less element. Write and test a function that implements this sorting method.

Please write program in C and add comments.

Explanation / Answer

#include <stdio.h>
void bubbleSort(int intArray[],int n) {

/*
* In bubble sort, we basically traverse the array from first
* to array_length - 1 position and compare the element with the next one.
* Element is swapped with the next element if the next element is greater.
*
* Bubble sort steps are as follows.
*
* 1. Compare array[0] & array[1]
* 2. If array[0] > array [1] swap it.
* 3. Compare array[1] & array[2]
* 4. If array[1] > array[2] swap it.
* ...
* 5. Compare array[n-1] & array[n]
* 6. if [n-1] > array[n] then swap it.
*
* After this step we will have largest element at the last index.
*
* Repeat the same steps for array[1] to array[n-1]
*
*/
int i,j;
int temp = 0;

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

if(intArray[j-1] > intArray[j]){
//swap the elements!
temp = intArray[j-1];
intArray[j-1] = intArray[j];
intArray[j] = temp;
}

}
}

}
int main()
{
int array[] = {5,4,7,8,9,6,2,1,3,10,4};
int n = 10;
int i;
  
printf("Array elements before sorting ");
for(i=0; i<n; i++) {
printf("%d ", array[i]);
}
printf(" ");
bubbleSort(array, n);
  
printf("Array elements after sorting ");
for(i=0; i<n; i++) {
printf("%d ", array[i]);
}
printf(" ");

return 0;
}

Output:

sh-4.2$ gcc -o main *.c                                                                                                                                                                                                                                                

sh-4.2$ main                                                                                                                                                                                                                                                           

Array elements before sorting                                                                                                                                                                                                                                          

5 4 7 8 9 6 2 1 3 10                                                                                                                                                                                                                                                   

Array elements after sorting                                                                                                                                                                                                                                           

1 2 3 4 5 6 7 8 9 10

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote