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. 34. When selection sort is applied to an array , how many times is the comparison in the if-then statement performed?
To find out the number of times and order comparison is performed in the execution of the algorithm
From the given using these for comparison analysis.
In the first step is compare elements. This requires one comparison and one comparison Index of comparison in total. Comparisons first step =4
In the second step compare elements
Bartleby provides explanations to thousands of textbook problems written by our experts, many with advanced degrees!Get Started