Question
Modify your program so that it prints the array at each step of the algorithm. #include using namespace std; void selectionSort(int [], int); void showArray(int
Modify your program so that it prints the array at each step of the algorithm.
#include
using namespace std;
void selectionSort(int [], int); void showArray(int [], int);
int main() {
const int SIZE = 6; int values[SIZE] = {5, 12, 2, 6, 7, 1};
cout << "The unsorted values are "; showArray(values, SIZE);
selectionSort(values, SIZE);
cout << "The sorted values are "; showArray(values, SIZE); return 0; }
void selectionSort(int array[], int size) {
int startScan, maxIndex, maxValue; for (startScan = 0; startScan < (size - 1); startScan++) { maxIndex = startScan; maxValue = array[startScan]; for(int index = startScan + 1; index < size; index++) {
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; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started