Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

code in C++ The exchange sort is another basic sort algorithm that repeatedly scans a vector and swaps elements until the list is placed in

code in C++

The exchange sort is another basic sort algorithm that repeatedly scans a vector and swaps elements until the list is placed in ascending order. The algorithm exchanges a pair of elements when a smaller element is out of order. The example below explains how the algorithm works:

image text in transcribed

For this programming assignment you will implement the exchange sort. Exchange sort must be a template function. The list to be sorted must be implemented as a vector. You must use member function resize in your solution. To test your program, use the following lists:

9, 16, 3, 8, 2, 12, 6 and 15, 34, 18, 6, 55, 78, 2

You output should look as follows:

Enter 7 integer values: 9 16 3 8 2 12 6

The vector as input is: 9 16 3 8 2 12 6

The sorted vector is: 2 3 6 8 9 12 16

Enter 7 integer values: 15 34 18 6 55 78 2

The vector as input is: 15 34 18 6 55 78 2

The sorted vector is: 2 6 15 18 34 55 78

Press any key to continue

Pass 0: Consider the full list 8, 3,6, 2 .The entry at index 0 is compared with each other entry in the vector at index 1, 2, and 3. For each comparison, if the larger element is at index 0, the two entries are exchanged. After all the comparisons, the smallest element is stored at index 0 Pass 0: Compare vIO] with v[1],v[2], v[3] Initial List Resulting List Action Exchange NoExchange Exchange Consider the sublist 8, 6, 3. With the smallest element already located at index 0, only entries in the vector from index 1 to the end are considered The entry at index 1 is compared with the other entries at index 2 and 3 For each comparison, if the larger element is at index 1, the two entries are exchanged. After all the comparisons, the smallest element in the new list is stored at index 1 Pass 1: Pass 1: Compare v[1] with vI2], v3] Initial List Resulting List Action Exchange Exchange Pass 2: Consider the sublist 8,6. With the two smallest elements already located at index 0 and 1, only entries in the list from index 2 to the end are consid- ered. The entry at index 2 is compared with the only other element in the vector, at index 3. After the comparison, the smallest element in the new list is stored at index 2. The resulting vector is ordered. Pass 2: Compare v[2] with v[3] Initial List Action Resulting List Exchange

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions