   Chapter 11.3, Problem 31ES ### Discrete Mathematics With Applicat...

5th Edition
EPP + 1 other
ISBN: 9781337694193

#### Solutions

Chapter
Section ### Discrete Mathematics With Applicat...

5th Edition
EPP + 1 other
ISBN: 9781337694193
Textbook Problem
1 views

# Exercises 28—35 refer to selection sort, which is another algorithm to arrange the items in an array in ascending order. Algorithm 11.3.2 Selection Sort (Given an array a [ 1 ] ,   a [ 2 ] ,   a [ 3 ] , … ,   a [ n ] , this algorithm selects the smallest element and places it in the first position. then selects the second smallest element and places it in the second position, and so forth, until the entire array is sorted. In general, for each k = 1 to n − 1 , the kth step of the algorithm selects the index of the array item will, minimum value from among a [ k + 1 ] ,   a [ k + 2 ] ,   a [ k + 3 ] ,   … ,   a [ n ] . Once this index is found, the value of the corresponding array item is interchanged with the value of a [ k ] unless the index already equals k. At the end of execution the array elements are in order.] Input: n [a positive integer a [ 1 ] ,   a [ 2 ] ,   a [ 3 ] , … ,   a [ n ] [an array of data items capable of being ordered] Algorithm Body: for k : = 1 to n − 1 I n d e x O f M i n : = k for i : = k + 1 to n if ( a [ i ] < a [ I n d e x o f M i n ] ) then I n d e x O f M i n : = i next i if IndexOfMin ≠ k then T e m p : = a [ k ] a [ k ] : = a [ I n d e x O f M i n ] a [ I n d e x O f M i n ] : = T e m p next k Output: a [ 1 ] ,   a [ 2 ] ,   a [ 3 ] , … ,   a [ n ] [in ascending order]The action of selection sort can be represented pictorially as follows: a [ 1 ]   a [ 2 ] ⋯ a [ k ] ↑ a [ k + 1 ] ⋯ a [ n ] kth step: Find the index of the array element with minimum value from among a [ k + 1 ] ,   … ,   a [ n ] . If the value of this array element is less than the value of a [ k ] . then its value and the value of a [ k ] are interchanged. 31. Construct a trace table showing the action of selection sort on the array of exercise 29.

To determine

To construct the trace table for showing the action of the selection sort on an array.

Explanation

Given information:

Reference to the Exercise 29 is −

Construct a table showing the result of each step when selection sort is applied to the array a=6, a=4, a=5, a=8 and a=1.

Calculation:

The following is the table that shows the interchanges that occurs when the selection sort is applied to the array.

 a[n] Stored value a 6 a 4 a 5 a 8 a 1

In the given selection sort algorithm, when the for-next loop is iterated for the first iteration, the variable k=1 and the IndexOfMin is assigned value of k which is 1. Now the next-inner for loop runs for the values ranging from k+1 to n i.e. from 2 to n.

Inside the for loop the if else condition is tested as follows-

1. for i=2, if-then-else statement is tested, since a<a{4<6}, therefore the IndexOfMin changes to 2.
2. for i=3, if-then-else statement is tested, since a<a{4<5}, therefore the IndexOfMin remains the same.
3. for i=4, if-then-else statement is tested, since a<a{4<8}, therefore the IndexOfMin remains the same

### Still sussing out bartleby?

Check out a sample textbook solution.

See a sample solution

#### The Solution to Your Study Problems

Bartleby provides explanations to thousands of textbook problems written by our experts, many with advanced degrees!

Get Started 