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

Bubble Sort Algorithm<?xml:namespace prefix = o ns = \"urn:schemas-microsoft-com

ID: 3539923 • Letter: B

Question

Bubble Sort Algorithm<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

In this lab you have to write a bubble sort algorithm to sort a given integer list. There are lots of tutorial online on how to write a bubble sort. Go through those tutorials if you are still unsure how it works. Start to write the code when you are ready. To test your program, you may use your own data or use the number file from lab 10.

Sample Input

3 1 2 5 4

Sample output

1 2 3 4 5

Hint:

You may want to use two for loops, one nested inside another. The outer loop will iterate n times (where n is the size of the integer list) and the inner loop will iterate n-1 times. Inside the inner loop you will start from the left at index 0. On each iteration, you have to compare two adjacent indices and swap them if the left one is greater than the right.

For swapping two adjacent index values, you have to take a temporary variable and use it in the following way:

1.       int temp = data[index]; // copy the index value into temp var

2.       data[index] = data[index+1]; // overwrite the index value with (index+1) value

3.       data[index + 1] = temp; // replace the (index+1) value with temp, which actually was index value.

Let%u2019s say, index = 1, data[1] =10 and data[2] = 9. Now using the above swapping codes, we will get:

1.       temp = 10

2.       data[1] = 9;

3.       data[2] = 10;

Explanation / Answer

#include using namespace std; void BubbleSort(int array[],int n) { int i,j; for(i=0;i
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