Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#include #include / * Program sorts an array of integers using a selection sort. The general algorithm repeatedly finds the smallest number in the array
#include
#include
Program sorts an array of integers using a selection sort.
The general algorithm repeatedly finds the smallest number
in the array and places it at the front of the list.
using namespace std;
int findsmallindex int startindex, int numbers ;
void swapvalues int index int index int numbers ;
int main
array of numbers
int numbers ;
int startindex; current starting spot for search
int smallindex; index of the smallest number in the array
int index; index used for print the array values
startindex ;
continue finding the smallest value and placing it
at the front of the list
while startindex
smallindex findsmallindex startindex, numbers;
swapvalues smallindex, startindex, numbers;
startindex;
cout
The sorted array is:
;
for index ; index ; index
cout numbers index;
cout
;
return ;
int findsmallindex int startindex, int numbers
int smallindex, smallest index to be returned
index; current index being viewed
smallindex startindex;
for index startindex ; index ; index
if numbers index numbers smallindex
smallindex index;
return smallindex;
void swapvalues int index int index int numbers
int swapper;
swapper numbers index;
numbers index numbers index;
numbers index swapper;
What value would findsmallindex return for the following array?
What is the point of the assignment smallindex startindex; at the beginning of findsmallindex?
In the control for the for loop in findsmallindex, index does not start at zero as it does in many of the loops that we have written. It starts at different places, different times that it is called. Why is this?
In the while loop in main, startindex only goes up to startindex Explain why the loop does not need to run when startindex equals the last index in the array
In swapvalues, swapper is declared as an int type. Why?
The program is hard coded to only work with arrays of numbers. Add a constant called size before main that is assigned the number of elements to be sorted. Use this size constant in the rest of the program so that the sort will work for any number of values. Run your program with size equal to add more numbers to the array in its declaration list
A simple change to the program will sort the numbers from largest to smallest. Change your program so that it does this. Do not print the sorted values in reverse order! Actually change the sort.
Write the change that you would make to the code here.
Submit :
The answers to the above questions
cpp version of the code.
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