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

IN C++ Bubble Sort Sorting a list of numbers is an important computing problem t

ID: 3688853 • Letter: I

Question

IN C++

Bubble Sort Sorting a list of numbers is an important computing problem that has been extensively studied. One of the simplest methods is known as bubble sort. Given a list of numbers, bubble sort sorts the list by passing over the list multiple times and moving at least one element into the correct position in each pass. Therefore, a list of N elements is guaranteed to be sorted after at most N-l passes. Consider the following list. 3, 5, 2, 8, 9, 1 The basic idea of bubble sort is to start at the beginning of the list and compare the first two numbers in the list and swap them if the first one is larger than the second (assuming you wish to sort from low to high). Next, the second and third numbers are compared and swapped if necessary, then the third and fourth, fourth and fifth, and so on until the entire list has been examined. After the first pass over the list, the largest value will always end up in the last position, as shown below. 3, 2, 5, 8, 1, 9 The name bubble sort refers to the fact that large numbers "bubble up" to the top of the list as the algorithm runs. We repeat the process, "bubbling" the larger values up in the list on each pass. Note, however, that for the second pass we don't need to examine the last value, because it's guaranteed to be the largest. After the second pass, the last two values need not be examined, and so on. The process ends when an entire pass through the list results in no values being swapped. Write a program that will implement the Bubble Sort algorithm. For this problem, you need to do the following: Declare an integer array named list that contains 50 values. Using a loop, initialize list with the values 100,99,98,... (in decreasing order) Construct a void function named b sort that implements the Bubble Sort algorithm, as described above. Your function should accept two arguments: the integer array to be sorted and the number of elements in the array. Call the b sort function to sort the list in increasing order. Print out the elements of list, 5 per line as follows: Example:

Explanation / Answer

// C++ program for implementation of Bubble sort
#include <iostream>

using namespace std;

void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

// A function to implement bubble sort
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)

// Last i elements are already in place   
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}

/* Function to print an array */
void printArray(int arr[], int size)
{
for (int i=0; i < size; i++){
if(i%5==0 && i>0)
cout<<endl;

cout<< arr[i] << " ";   
}
cout<<endl;
}

// Driver program to test above functions
int main()
{
int arr[50];
int i=0, n=50;

for(i=0;i<50;++i){
arr[i]=100-i;
}

bubbleSort(arr, n);

cout<< "Sorted array: "<<endl;

printArray(arr, n);

return 0;
}