As you have seen in lecture it is often necessary to sort an
As you have seen in lecture, it is often necessary to sort an array in order to makesearching faster. In this part of the lab, you will implement selection sort. The selectionsort algorithm consists of traversing the collection of unsorted elements to find themaximum (or minimum) value so far, and to swap it with the last one that is unsorted, andthen to repeat the process. There are multiple ways to implement the selection sortalgorithm, in addition to deciding whether to sort the array in an ascending versusdescending order. More specifically, you may choose to: Sort the array in an ascending versus descending order, Traverse the array from beginning to end, or vice versa, and Search for the minimum versus the maximum for the traverse. The first part of this program, which reads an array from the user, is exactly the same aswhat you have done in the previous parts of this lab and lab#2. Specifically, you should askthe user to enter the size of the array, by outputting: “Enter the size of the array: ” to the console. If the user enters an incorrect size, you should output the error message “ERROR: you entered an incorrect value for the array size!” and exit the program. If the input is a valid size for the array, ask the user to enter thedata, by outputting “Enter the numbers in the array, separated by a space, and press enter: ” Let the user enter the numbers of the array and store them into a C++ array. Up to now,this should be exactly the same code as Lab#2. Now, sort the array using a selection sort algorithm of your choice. Once the array issorted, output first to the console if the output is sorted in ascending or descending order(depending on what you decided to use): “This is the sorted array in order:” and then output the array in a newline. Also, write if you chose the max or min for thetraverse: “The algorithm selected the for the traverse ofthe array.”
Example runs (input is in italic and bold):
Enter the size of the array: 5 Enter the numbers in the array, separated by a space, and press enter: 4 6 8 2 5
This is the sorted array in descending order: 8 6 5 4 2
The algorithm selected the maximum for the traverse of the array.
Solution
Solution for problem(C++)
Program:-
#include<iostream>
using namespace std;
int main()
{
int size,array[100],i,j,swap;
cout<<\"Enter the size of the array : \"<<endl;
cin>>size;
if(size>0) //if size is valid then only proceed.
{
cout<<\"Enter values into array\"<<endl;
for(i=0;i<size;i++)
{
cin>>array[i]; //reading numbers into array.
}
for(i = 0; i < size; i++)
{
for(j = i+ 1; j < size; j++)
{
if(array[j] > array[i]) //Array sorting in decending order.
{
int swap = array[i];
array[i] = array[j];
array[j] = swap;
}
}
}
cout<<\"This is the sorted array in descending order:\";
for(i=0;i<size;i++)
cout<<\" \"<<array[i];
cout<<\"\ The algorithm selected the maximum for the traverse of the array.\";
}
else
{
cout<<\"Error:You entered incorrect value of array size\";
}
return 0;
}
Output :-
Enter the size of the array :
5
Enter values into array
4 6 8 2 5
This is the sorted array in descending order: 8 6 5 4 2
The algorithm selected the maximum for the traverse of the array.
Note :- If you want print accending order just replace \'>\' with \'<\' in if condition.
Thank you!

