c++ need to do a selection sort (selection sort descending) the data text for de
ID: 3729858 • Letter: C
Question
c++
need to do a selection sort (selection sort descending)
the data text for descending is:
Can you change the code to descending from ascending on my code?
Following, this is the code I get for the selection sort ascending.
#include <iostream>
#include <stdlib.h>
#include <chrono>
#include <string>
#include <fstream>
#include <sstream>
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;
}
Explanation / Answer
#include <iostream>
#include <stdlib.h>
#include <chrono>
#include <string>
#include <fstream>
#include <sstream>
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 = n-1; i > 0; i--)
{
// Find the minimum element in unsorted array
min_idx = 0;
for (j = 1; j <= i; 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<arraySize; 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 "<<endl;
printArray(array,arraySize);
// printArray(array,arraySize);
return 0;
}
//I have modified your selection sort to from ascending to descending. Let me know if you have any queries.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.