   Chapter 11.3, Problem 32ES ### 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.32. When selection sort is applied to the array of exercise 28, how many times is the comparison in the if-then statement performed?

To determine

To find the no of total comparison between a[j] and x using algorithm segment.

Explanation

From the given problem

a =5a=3a=4a=6a=2

Initially given 5 elements and 5 is assigned to n. Initially set k to 1, Index of Min to k=1 and i to k+1=1+1=2 .Here, n=5,k=1,Indexofmin=1,i=2.

First step to compare j0 the second element to a[Index of min ]=a=5 which is less than first element 5 ,so need to assign the 2 as the Index of min and then i is increased by 1.This requires one comparisona [ Index of min ]>a[i].

Further compare 3rd element to a [ Index of min  ]=a=3

### 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 