Use the clock function to plot and compare the Ocomplexities of quicksort and bu
ID: 3550504 • Letter: U
Question
Use the clock function to plot and compare the Ocomplexities of quicksort and bubble sort insert in main.
#include <iostream>
#include <ctime>
using namespace std;
void bsort()
{
int array[100], m, i, j, swap;
cout << "Enter the number of elements: ";
cin >> m;
cout << "Enter the integers: ";
for (i = 0; i < m; i++)
cin >> array[i];
for (i = 0; i < (m - 1); i++)
{
for (j = 0; j < m - i - 1; j++)
{
if (array[j]>array[j + 1])
{
swap = array[j];
array[j] = array[j + 1];
array[j + 1] = swap;
}
}
}
cout << "List in ascending order: ";
for (i = 0; i < m; i++)
cout <<" "<<array[i];
}
void quickSort(int arr[], int left, int right)
{
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];
/* partition */
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
}
/* recursion */
if (left < j)
quickSort(arr, left, j);
if (i < right)
quickSort(arr, i, right);
}
int main()
{
}
Explanation / Answer
try this it would work better!!!!!!!!!!!!!!!!!!!!!!
#include <iostream.h>
#include <stdio.h>
#include <cmath.h>
#include <cstdlib.h>
#include <ctime.h>
using namespace stdnt;
int m1[1000], m2[1000], m;
int main() {
cout << "number of elements you want to enter: ";
cin >> m;
cout << "integers you want to enter: ";
for (int i = 0; i < m; i++){
cin >> rand() % INT_MAX;
m2[i] = m1[i];
}
clock_t start1,start2;
double dur;
start1 = clock();
bsort();
dur = (clock()-start1)/(double)CLOCKS_PER_SEC;
cout << "Binary Sort duration is : " << duration << endl;
start2 = clock();
quickSort(array2,0,m-1);
dur = (clock()-start2)/(double)CLOCKS_PER_SEC;
cout << "Quick Sort duration is : " << duration << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.