Re-write the sort program so that it orders integers from largest to smallest (d
ID: 3592584 • Letter: R
Question
Re-write the sort program so that it orders integers from largest to smallest (descending order) rather than smallest to largest (ascending order).
// This program uses the selection sort algorithm to sort an
// array in ascending order.
#include <iostream>
using namespace std;
// Function prototypes
void selectionSort(int [], int);
void showArray(int [], int);
int main()
{
// Define an array with unsorted values
const int SIZE = 6;
int values[SIZE] = {5, 7, 2, 8, 9, 1};
// Display the values.
cout << "The unsorted values are ";
showArray(values, SIZE);
// Sort the values.
selectionSort(values, SIZE);
// Display the values again.
cout << "The sorted values are ";
showArray(values, SIZE);
return 0;
}
//**************************************************************
// Definition of function selectionSort. *
// This function performs an ascending order selection sort on *
// array. size is the number of elements in the array. *
//**************************************************************
void selectionSort(int array[], int size)
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = array[startScan];
for(int index = startScan + 1; index < size; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
//**************************************************************
// Definition of function showArray. *
// This function displays the contents of array. size is the *
// number of elements. *
//**************************************************************
void showArray(int array[], int size)
{
for (int count = 0; count < size; count++)
cout << array[count] << " ";
cout << endl;
}
Explanation / Answer
#include <iostream>
using namespace std;
void selectionSort(int [], int);
void showArray(int [], int);
int main()
{
// Define an array with unsorted values
const int SIZE = 6;
int values[SIZE] = {5, 7, 2, 8, 9, 1};
// Display the values.
cout << "The unsorted values are ";
showArray(values, SIZE);
// Sort the values.
selectionSort(values, SIZE);
// Display the values again.
cout << "The sorted values are ";
showArray(values, SIZE);
return 0;
}
void selectionSort(int array[], int size)
{
/* just renamed the variables as for the purpose */
int startScan, maxIndex, maxValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
maxIndex = startScan;
maxValue = array[startScan];
for(int index = startScan + 1; index < size; index++)
{
/* I have applied the change here */
if (array[index] > maxValue)
{
maxValue = array[index];
maxIndex = index;
}
}
array[maxIndex] = array[startScan];
array[startScan] = maxValue;
}
}
void showArray(int array[], int size)
{
for (int count = 0; count < size; count++)
cout << array[count] << " ";
cout << endl;
}
/* it is same as your code but a very minor change , please observer that*/
/* if any queries please comment*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.