Answer to Question #265539 in C++ for Usama

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


1
Expert's answer
2021-11-14T09:21:04-0500
#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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS