1- the provided quick sort algorithm, and 2- your bubble sort algorithm. Plot th
ID: 3550431 • Letter: 1
Question
1- the provided quick sort algorithm, and
2- your bubble sort algorithm.
Plot the time-results of at least nine samples: starting fron 1 million elements, to nine millions elements arrays
Plotting must be done by the program: using the clock() library function as has been described in class.
hints:
1- it is easier to put the times horizontally (x-axis), and the sample sizes (n) down (y-axis)
2- the two curves are to be using (.) and (*) for distinction between the two algorithms.
MOST OF THE CODE IS DONE I JUST NEED HELP WITH PLOTTING USING THE CLOCK FUNCTION.
#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
ry 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.