Answer to Question #187981 in C++ for Rasel Sheikh

Question #187981

Write a program with analysis to sort in descending manner using 'p' number of values and analyze the complexity.(bubble sort)



1
Expert's answer
2021-05-01T17:50:38-0400
#include <iostream>
#include <ctime>
#include <random>
 
using namespace std;
 
int main()
{
	int p;
	cout << "Enter quantity of items: ";
	cin >> p;
	int* arr = new int[p];
	srand(time(0));
	for (int i = 0; i < p; i++)
	{
		arr[i] = rand() % 10000;
	}
	// Since bubble sort uses a loop nested within a loop, the complexity of execution is O^2
	for (int i = 0; i < p; i++)
	{
		for (int j = 0; j < p - 1; j++)
		{
			if (arr[j] < arr[j + 1]) swap(arr[j], arr[j + 1]);
		}
	}
	cout << "Sorted array: ";
	for (int i = 0; i < p; i++)
	{
		cout << arr[i] << " ";
	}
	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