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. 31. Construct a trace table showing the action of selection sort on the array of exercise 29.
To construct the trace table for showing the action of the selection sort on an array.
Reference to the Exercise 29 is −
Construct a table showing the result of each step when selection sort is applied to the array
The following is the table that shows the interchanges that occurs when the selection sort is applied to the array.
In the given selection sort algorithm, when the for-next loop is iterated for the first iteration, the variable and the IndexOfMin is assigned value of which is . Now the next-inner for loop runs for the values ranging from to i.e. from to .
Inside the for loop the if else condition is tested as follows-
Bartleby provides explanations to thousands of textbook problems written by our experts, many with advanced degrees!Get Started