Need help on C++. Tsk 1: Funtins r lsss r k. Initiliz th n rry frm th Vlus.txt M
ID: 3729818 • Letter: N
Question
Need help on C++.
Tsk 1:
Funtins r lsss r k.
Initiliz th n rry frm th Vlus.txt Mstly Srtd Dsnding.
Impmnt Sltin srt t srt th vlus in snding rdr. Lst Grtst
Msur th tim it tks t xut th srt in millisnds.
Pls run th srt 3 tims.
tth Phts f Sur d nd utput
Tsk 2:
Funtins r lsss r k.
Initiliz th n rry frm th Vlus.txt Mstly Srtd Dsnding.
Implmnt Insrtin srt t srt th vlus in snding rdr. Lst Grtst
Msur th tim it tks t xut th srt in millisnds.
Pls run th srt 3 tims.
tth Phts f Sur d nd utput
Tsk 3:
Funtins r lsss r k.
Initiliz th n rry frm th Vlus.txt Mstly Srtd Dsnding.
Implmnt Quik srt t srt th vlus in snding rdr. Lst Grtst
Msur th tim it tks t xut th srt in millisnds.
Pls run th srt 3 tims.
tth Phts f Sur d nd utput
Mostly Sorted Descending 1000 values 1001000.0, 10000.0, 9990.0, 9980.0, 9970.0, 9960.0, 9950., 9940.0, 9930.0, 9920.0, 9910.0, 9900.0, 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, 9170.0, 9160.0, 9150.0, 9140.0, 9130.0, 9120.0, 9110.0, 9100.0, 9090.0, 9080.0, 9070.0, 9060.0, 9050.0, 9040.0, 9030.0, 9020.0, 901000.0, 9000.0, 8990.0, 8990.0, 8970.0, 8960.0, 8950.0, 8940.0, 8930.0, 8920.0, 8910.0, 8900.0, 8890.0, 9880.0, B870.0, 8860.0, 8850.0, 8940.0, 8830.0, 8820.0, 8810.0, 8800.0, 8790.0, 8780.0, 8770.0, 876000.0, 8750.0, 8740.0, 8730.0, 8720.0, 8710.0, 8700.0, B690.0, 8680.0, 8670.0, 8660.0, 8650.0, 8640.0, 8630.0, 8620.0, 8610.0, 8600.0, 8590.0, 8580.0, 8570.0, 8560.0, 8550.0, 8540.0, 8530.0, 8520.0, 851000.0, 8500.0, 8490.0, 8480.0, 8470.0, 8460.0, 8450.0, 8440.0, 8430.0, 8420.0, 8410.0, 8400.0, 8390.0, 8380.0, 8370.0, 8360.0, 8350.0, 8340.0, 8330.0, 8320.0, 8310.0, 8300.0, 8290.0, 8280.0, 8270.0, 826000.0, 8250.0, 8240.0, 8230.0, 8220.0, 8210.0, 8200.0, 8190.0, 8180.0, 8170.0, 8160.0, 8150.0, 8140.0, 8130.0, 8120.0, 8110.0, 8100., 8090.0, 8080.0, 8070.0, 8060.0, 8050.0, 8040.0, 8030.0, 8020.0, 801000.0, 8000.0, 7990.0, 7980.0, 7970.0, 7960.0, 7950.0, 7940.0, 7930.0, 7920.0, 7910.0, 7900.0, 7890.0, 7880.0, 7870.0, 7860.0, 7850.0, 7840.0, 7830.0, 7820.0, 7810.0, 7800.0, 7790.0, 7780.0, 7770.0, 776000.0, 7750.0, 7740.0, 7730.0, 7720.0, 7710.0, 7700.0, 7690.0, 7680., 7670.0, 7660.0, 7650.0, 7640.0, 7630.0, 7620.0, 7610.0, 7600.0, 7590.0, 7580.0, 7570.0, 7560.0, 7550.0, 7540.0, 7530.0, 7520.0, 751000.0 7500.0, 7490.0, 7480.0, 7470.0, 7460.0, 7450.0, 7440.0, 7430.0, 7420.0, 7410.0, 7400.0, 7390.0, 7380.0, 7370.0, 7360.0, 7350.0, 7340.0, 7330.0, 7320.0, 7310.0, 7300.0, 7290.0, 7280.0, 7270.0, 726000.0, 7250.0, 7240.0, 7230.0, 7220.0, 7210.0, 7200.0, 7190.0, 7180.0, 7170.0, 7160.0, 7150.0, 7140.0, 7130.0, 7120.0, 7110.0, 7100.0, 7090.0, 7080.0, 7070.0, 7060.0, 7050.0, 7040.0, 7030.0, 7020.0, 701000.0, 7000.0, 6990.0, 6980.0, 6970.0, 6960.0, 6950.0, 6940.0, 6930.0, 6920.0, 6910.0, 6900.0, 6890.0, 6880.0, 6870.0, 6860.0, 6850.0, 6840.0, 6830.0, 6820.0, 6810.0, 6900.0, 6790.0, 6780.0, 6770.0, 676000.0, 6750.0, 6740.0, 6730.0, 6720.0, 6710.0, 6700.0, 6690.0, 6680.0, 6670.0, 6660.0, 6650.0, 6640.0, 6630.0,Explanation / Answer
Task 1 :
#include <stdlib.h>
#include <time.h>
#include<stdio.h>
void selectionSort(float arr[], int n)
{
int i, j, index;
float temp;
for (i = 0; i < n-1; i++)
{
index = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[index])
index = j;
temp = arr[index];
arr[index]= arr[i];
arr[i] = temp;
}
}
int main()
{
float array[1000];
int i;
clock_t start, stop;
FILE *myFile;
myFile = fopen("values.txt", "r");
for (i=0;i<1000;i++){
fscanf(myFile, "%f", &array[i]);
}
start = clock();
selectionSort(array,1000);
stop = clock();
for (i=0;i<1000;i++){
printf("%f ",array[i]);
}
printf("Time Took for sorting : %f ",
((double)(stop - start)));
}
Task 2 :
#include <stdlib.h>
#include <time.h>
#include<stdio.h>
void insertionSort(float arr[], int n)
{
int i, j;
float value;
for (i = 1; i < n; i++)
{
value = arr[i];
j = i-1;
while (j >= 0 && arr[j] > value)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = value;
}
}
int main()
{
float array[1000];
int i;
clock_t start, stop;
FILE *myFile;
myFile = fopen("values.txt", "r");
for (i=0;i<1000;i++){
fscanf(myFile, "%f", &array[i]);
}
start = clock();
insertionSort(array,1000);
stop = clock();
for (i=0;i<100;i++){
printf("%f ",array[i]);
}
printf("Time Took for sorting : %f ",
((double)(stop - start)));
}
Task 3 :
#include <stdlib.h>
#include <time.h>
#include<stdio.h>
int divide (float arr[], int low, int high)
{
float pivot = arr[high];
int i = (low - 1),j;
float temp;
for ( j = low; j <= high- 1; j++)
{
if (arr[j] <= pivot)
{
i++;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
return (i + 1);
}
void quickSort(float arr[], int low, int high)
{
if (low < high)
{
int pi = divide(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main()
{
float array[1000];
int i;
clock_t start, stop;
FILE *myFile;
myFile = fopen("values.txt", "r");
for (i=0;i<1000;i++){
fscanf(myFile, "%f", &array[i]);
}
start = clock();
quickSort(array,0,999);
stop = clock();
for (i=0;i<1000;i++){
printf("%f ",array[i]);
}
printf("Time Took for sorting : %f ",
((double)(stop - start)));
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.