Question #265539

Sort the following list using the bubble sort algorithm. Show the list after each iteration of the outer

for loop.

26, 45, 17, 65, 33, 55, 12, 18


Expert's answer

#include<iostream>
using namespace std;

const int N = 8;
void SwapInt(int* x, int* y);
void BubbleSortAndPrint(int arr[N]);

int main()
{
	int arr[N] = { 26,45,17,65,33,55,12,18};
	BubbleSortAndPrint(arr);
}

void SwapInt(int* x, int* y)
{
	int temp = *x;
	*x = *y;
	*y = temp;
}

void BubbleSortAndPrint(int arr[N])
{
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N - i-1; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				SwapInt(&arr[j], &arr[j + 1]);
			}			
		}
		for (int k = 0; k < N; k++)
		{
			cout << arr[k] << " ";
		}
		cout << endl;
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS