Write a program with analysis to sort in descending manner using 'p' number of values and analyze the complexity.(bubble sort)
#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;
}
Comments
Leave a comment