Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ Quick sort (mostly sorted ascending) Quicksort (mostly sorted ascending) no

ID: 3729854 • Letter: C

Question

c++

Quick sort (mostly sorted ascending)

Quicksort (mostly sorted ascending)

no insertion sort or selection sort, please!

Can you change the code to quicksort basion on my code?

Following, this is the code I get for the selection sort ascending.

#include

#include

#include

#include

#include

#include

using namespace std;

using namespace std::chrono;

//chrono for time check

//swap usign by selection sort

void swap(float *xp, float *yp)

{

float temp = *xp;

*xp = *yp;

*yp = temp;

}

void selectionSort(float arr[], int n)

{

//clock to check time

auto start = high_resolution_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 stop = high_resolution_clock::now();

auto duration = duration_cast(stop - start);

cout << "Time taken by Selection sort is : " << duration.count() << " microseconds" << endl;

}

/* Function to print an array */

void printArray(float arr[],int size)

{

int i;

for (i=0; i < size; i++)

printf("%.2f ", arr[i]);

printf(" ");

}

int readFileAndStore(float arr[]){

//load data from Rannum.txt file

ifstream infile;

string line;//for read line

int counter=-1;

//open the file

infile.open ("data.txt"); //name of file here. plz mention Complete path if file is not at root

if (infile.is_open()) //if file opened

{

while( getline(infile, line,',') ) { //get row from text file

stringstream ss(line);

counter++; //increase counter

ss>>arr[counter];

}

infile.close(); //close file

}

else //if file not found show the below message

{

cout << "Sorry, we could not find the equation file." << endl;

}

//return counter that is number of lines

return counter;

}

int main()

{

//initialize array of 1000

int arraySize = 1000;

float array[arraySize];

//set all to zero

for(int i=0;i

array[i] =0;

}

//read array from file

int readSize = readFileAndStore(array);

//set array size to read size

arraySize = readSize+1;

printArray(array,arraySize);

selectionSort(array,arraySize);

selectionSort(array,arraySize);

selectionSort(array,arraySize);

cout<<"Array after sorting "<

printArray(array,arraySize);

// printArray(array,arraySize);

return 0;

}

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 Measure the time it takes to execute the sort in milliseconds. Please run the sort 3 times. Greatest Attach Photos of Source Code and Output

Explanation / Answer

#include <chrono>//include chrono to deal with date and time

#include <fstream>//include fstream to use ifstream class to read from files

#include <bits/stdc++.h>// to count words in a string using stringstream.

using namespace std;

using namespace std::chrono;

void quicksort(float a[], int l, int u)

{

//clock to check time

auto start = std::chrono::high_resolution_clock::now();

int p,temp;

int i,j;

if(l<u)

{

p=a[l];

i=l;

j=u;

while(i<j)

{

while(a[i] <= p && i<j )

i++;

while(a[j]>p && i<=j )

j--;

if(i<=j)

{

temp=a[i];

a[i]=a[j];

a[j]=temp;}

}

temp=a[j];

a[j]=a[l];

a[l]=temp;

cout <<" ";

for(i=0;i<10;i++)

cout <<a[i]<<" ";

quicksort(a,l,j-1);

quicksort(a,j+1,u);

auto stop = std::chrono::high_resolution_clock::now();

auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);

cout << "Time taken by Selection sort is : " << duration.count() << " microseconds" << endl;

}

}

/* Function to print an array */

void printArray(float arr[],int size)

{

int i;

for (i=0; i < size; i++)

printf("%.2f ", arr[i]);

printf(" ");

}

int readFileAndStore(float arr[]){

//load data from Rannum.txt file

ifstream infile;

string line;//for read line

int counter=-1;

//open the file

infile.open ("data.txt"); //name of file here. plz mention Complete path if file is not at root

if (infile.is_open()) //if file opened

{

while( getline(infile, line,',') ) { //get row from text file

stringstream ss(line);

counter++; //increase counter

ss>>arr[counter];

}

infile.close(); //close file

}

else //if file not found show the below message

{

cout << "Sorry, we could not find the equation file." << endl;

}

//return counter that is number of lines

return counter;

}

int main()

{

//initialize array of 1000

int arraySize = 1000;

float array[arraySize];

//set all to zero

for(int i=0;i<arraySize;i++){

array[i] =0;

}

//read array from file

int readSize = readFileAndStore(array);

//set array size to read size

if(readSize !=-1){

arraySize = readSize+1;

printArray(array,arraySize);

quicksort(array,0,arraySize);

quicksort(array,0,arraySize);

quicksort(array,0,arraySize);

cout<<"Array after sorting "<< endl;

printArray(array,arraySize);

}

// printArray(array,arraySize);

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote