help on C++ H/W Please use comments Task 1: Functions or classes are ok. Create
ID: 3729751 • Letter: H
Question
help on C++ H/W
Please use comments
Task 1:
Functions or classes are ok.
Create an array that holds 1000 random floats between 1-1000.
Implement Selection sort to sort the values in ascending order. Least Greatest
Measure the time it takes to execute the sort in milliseconds.
Please run the sort 3 times.
Attach Photos of Source Code and Output
Task 2:
Functions or classes are ok.
Create an array that holds 1000 random floats between 1-1000.
Implement Insertion sort to sort the values in ascending order. Least Greatest
Measure the time it takes to execute the sort in milliseconds.
Please run the sort 3 times.
Attach Photos of Source Code and Output
Task 3:
Functions or classes are ok.
Create an array that holds 1000 random floats between 1-1000.
Implement Quick Sort to sort the values in ascending order. Least Greatest
Measure the time it takes to execute the sort in milliseconds.
Please run the sort 3 times.
Attach Photos of Source Code and Output
Task 4:
Functions or classes are ok.
Initialize the an array from the Values.txt Mostly Sorted Descending.
Implement Selection sort to sort the values in ascending order. Least Greatest
Measure the time it takes to execute the sort in milliseconds.
Please run the sort 3 times.
Attach Photos of Source Code and Output
Task 5:
Functions or classes are ok.
Initialize the an array from the Values.txt Mostly Sorted Descending.
Implement Insertion sort to sort the values in ascending order. Least Greatest
Measure the time it takes to execute the sort in milliseconds.
Please run the sort 3 times.
Attach Photos of Source Code and Output
Task 6:
Functions or classes are ok.
Initialize the an array from the Values.txt Mostly Sorted Descending.
Implement Quick sort to sort the values in ascending order. Least Greatest
Measure the time it takes to execute the sort in milliseconds.
Please run the sort 3 times.
Attach Photos of Source Code and Output
Task 7:
Functions or classes are ok.
Initialize the an array from the Values.txt Mostly Sorted Ascending.
Implement Selection sort to sort the values in ascending order. Least Greatest
Measure the time it takes to execute the sort in milliseconds.
Please run the sort 3 times.
Attach Photos of Source Code and Output
Task 8:
Functions or classes are ok.
Initialize the an array from the Values.txt Mostly Sorted Ascending
Implement Insertion sort to sort the values in ascending order. Least Greatest
Measure the time it takes to execute the sort in milliseconds.
Please run the sort 3 times.
Attach Photos of Source Code and Output2
Task 9:
Functions or classes are ok.
Initialize the an array from the Values.txt Mostly Sorted Ascending.
Implement Quick sort to sort the values in ascending order. Least Greatest
Measure the time it takes to execute the sort in milliseconds.
Please run the sort 3 times.
Attach Photos of Source Code and Output
Mostly Sorted Descending 1000 values 1001000.0, 10000.0, 9990.0, 9980.0, 9970.0, 9960.0, 9950.0, 9940.0, 9930.0, 9920.0, 9910.0, 9900., 9890.0, 9880.0, 9870.0, 9860.0, 9850.0 9840.0, 9830.0, 9820.0, 9810.0, 9800.0, 9790.0, 9780.0, 9770.0, 976000.0, 9750.0, 9740.0, 9730.0, 9720.0, 9710.0, 9700.0, 9690.0, 9680.0 9670.0, 9660.0, 9650.0, 9640.0, 9630.0, 9620.0, 9610.0, 9600.0, 9590.0, 9580.0, 9570.0, 9560.0, 9550.0, 9540.0, 9530.0, 9520.0, 951000.0, 9500.0, 9490.0, 9480.0, 9470.0, 9460.0, 9450.0, 9440.0, 9430.0, 9420.0, 9410.0, 9400.0, 9390.0, 9380.0, 9370.0, 9360.0, 9350.0, 9340.0, 9330.0, 9320.0, 9310.0, 9300.0, 9290.0, 9280.0, 9270.0, 926000.0 9250.0, 9240.0, 9230.0, 9220.0, 9210.0, 9200.0, 9190.0, 9180.0,Explanation / Answer
As per Chegg guidelines, we are allowed to answer only the first 2 tasks.
Task 1:
SOURCE CODE:
#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h>
#include <chrono>
#include <unistd.h>
using namespace std;
void swap(float *xp, float *yp)
{
float temp = *xp;
*xp = *yp;
*yp = temp;
}
int main()
{
float arr[1000];
for(int i=0;i<1000;i++)
arr[i] = 1 + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(1000)));
int i, j,n=1000;
for(int k=0;k<3;k++)
{
auto begin = chrono::steady_clock::now();
int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first element
swap(&arr[min_idx], &arr[i]);
}
auto end = chrono::steady_clock::now();
cout << "Elapsed time in milliseconds : "
<< chrono::duration_cast<chrono::milliseconds>(end - begin).count()
<< " ms" << endl;
}
for(i=0;i<1000;i++)
cout<<arr[i]<<endl;
}
OUTPUT:
Task 2:
SOURCE CODE:
#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h>
#include <chrono>
using namespace std;
int main()
{
float arr[1000];
for(int i=0;i<1000;i++)
arr[i] = 1 + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(1000)));
int i, j,n=1000;
float key;
for(int k=0;k<3;k++)
{
auto begin = chrono::steady_clock::now();
clock_t t;
t = clock();
for (i = 1; i < n; i++)
{
key = arr[i];
j = i-1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
auto end = chrono::steady_clock::now();
cout << "Elapsed time in milliseconds : "
<< chrono::duration_cast<chrono::milliseconds>(end - begin).count()
<< " ms" << endl;
}
for(i=0;i<1000;i++)
cout<<arr[i]<<endl;
}
OUTPUT:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.