Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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,

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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases DeMYSTiFieD

Authors: Andy Oppel

2nd Edition

0071747990, 978-0071747998

More Books

Students also viewed these Databases questions