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 , 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 to , the kth step of the algorithm selects the index of the array item will, minimum value from among . Once this index is found, the value of the corresponding array item is interchanged with the value of unless the index already equals k. At the end of execution the array elements are in order.] Input: n [a positive integer [an array of data items capable of being ordered] Algorithm Body: for to
for to n
if then next i if then
next k Output: [in ascending order]The action of selection sort can be represented pictorially as follows:
kth step: Find the index of the array element with minimum value from among . If the value of this array element is less than the value of . then its value and the value of are interchanged.
33. When selection sort is applied to the array of exercise 29. how many times is the comparison in the if-then statement performed?
To find the no of total comparison between and using algorithm segment.
From the given
Initially given 5 elements and 5 is assigned to n. Initially set k to 1,Index of Min to and i to .Here, .
First step to compare the second element to a Index of min which is less than first element 6 ,so need to assign the 2 as the Index of min and then i is increased by 1.This requires one comparison a Index of min
Bartleby provides explanations to thousands of textbook problems written by our experts, many with advanced degrees!Get Started