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

Can anyone help me with this? The bubble sort algorithm is a very simple but ine

ID: 3716307 • Letter: C

Question

Can anyone help me with this?

The bubble sort algorithm is a very simple but inefficient sorting method.

The following example illustrates its method.

Assume that the array a has five elements and its values are 5, 3, 4, 9 and 2 in order.

We want to use the bubble sort algorithm to sort it from small to large. The calculation process is as follows

Round count

Array content

explanation

0

5   3   9   4   2

Compare 5 and 3, the order is wrong, so reverse the order.

1

3   5   9   4   2

Compare 5 and 9, the order is correct, no need to reverse.

2

3   5   9   4   2

Compare 9 and 4, the order is wrong, so reverse the order.

3

3   5   4   9   2

Compare 9 and 2, the order is wrong, so reverse the order.

After completing this swap, the largest 1 number has been moved to the correct position.

4

3   5   4   2   9

Compare 3 and 5, the order is correct, no need to reverse.

5

3   5   4   2   9

Compare 5 and 4, the order is wrong, so reverse the order.

6

3   4   5   2   9

Compare 5 and 2, the order is wrong, so reverse the order.

After completing this swap, the largest 2 number has been moved to the correct position.

7

3   4   2   5   9

Compare 3 and 4, the order is wrong, so reverse the order.

8

3   4   2   5   9

Compare 4 and 2, the order is wrong, so reverse the order.

After completing this swap, the largest 3 number has been moved to the correct position.

9

3   2   4   5   9

Compare 3 and 2, the order is wrong, so reverse the order.

2   3   4   5   9

Sorting completed

Try writing a function void Bubble (int a[ ], int size). Where “a” is the array of integers to be sorted, and “size” is the number of integers in the array.

The feather of this function is to use the bubble sort algorithm to sort all odds from smallest to largest

Claim:

1. This work may not use global variables.

2. The paid contribution must be a complete, executable program.

That is, there must be an appropriate main() function. Bubble() must be called in the main() function to display its function.

Round count

Array content

explanation

0

5   3   9   4   2

Compare 5 and 3, the order is wrong, so reverse the order.

1

3   5   9   4   2

Compare 5 and 9, the order is correct, no need to reverse.

2

3   5   9   4   2

Compare 9 and 4, the order is wrong, so reverse the order.

3

3   5   4   9   2

Compare 9 and 2, the order is wrong, so reverse the order.

After completing this swap, the largest 1 number has been moved to the correct position.

4

3   5   4   2   9

Compare 3 and 5, the order is correct, no need to reverse.

5

3   5   4   2   9

Compare 5 and 4, the order is wrong, so reverse the order.

6

3   4   5   2   9

Compare 5 and 2, the order is wrong, so reverse the order.

After completing this swap, the largest 2 number has been moved to the correct position.

7

3   4   2   5   9

Compare 3 and 4, the order is wrong, so reverse the order.

8

3   4   2   5   9

Compare 4 and 2, the order is wrong, so reverse the order.

After completing this swap, the largest 3 number has been moved to the correct position.

9

3   2   4   5   9

Compare 3 and 2, the order is wrong, so reverse the order.

2   3   4   5   9

Sorting completed

Explanation / Answer

#include <stdio.h>

void bubble_sort(int [], int);

int main()
{
int arr[100], n, loop, d, swap;

printf("Enter number of elements ");
scanf("%d", &n);

printf("Enter %d integers ", n);

for (loop = 0; loop < n; loop++)
scanf("%d", &arr[loop]);

bubble_sort(arr, n);

printf("Sorted list in ascending order: ");

for (loop = 0 ; loop < n ; loop++ )
printf("%d ", array[loop]);

return 0;
}

void bubble_sort(int list[], int size)
{
int loop1, loop2, temp;

for (loop1 = 0 ; loop1 < (size - 1 ); loop1++)
{
for (loop2 = 0 ; loop2 < size - loop1 - 1; loop2++)
{
if (list[loop2] > list[loop2+1])
{
temp = list[loop2];
list[loop2] = list[loop2+1];
list[loop2+1] = temp;
}
}
}
}

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