There are several other sorting algorithms that exhibit the O (N 2 ) behaviour of selection sort. Of these, one of the most important is insertion sort, which operates as follows. You go through each...


There are several other sorting algorithms that exhibit the O (N2
) behaviour of selection sort. Of these, one of the most important is insertion sort, which operates as follows. You go through each element in the vector in turn, as with the selection sort algorithm. At each step in the process, however, the goal is not to find the smallest remaining value and switch it into its correct position, but rather to ensure that the values considered so far are correctly ordered with respect to each other. Although those values may shift as more elements are processed, they form an ordered sequence in and of themselves. For example, if you consider again the data used in the sorting examples from this chapter, the first cycle of the insertion sort algorithm requires no work, because a vector of one element is always sorted:


are in order with respect to each other—looking for the position where 37 belongs. As you go, you need to shift each of the larger elements one position to the right, which eventually makes room for the value you’re trying to insert. In this case, the 56 gets shifted by one position, and the 37 winds up in position 1. Thus, the configuration after the third cycle looks like this:


After each cycle, the initial portion of the vector is always sorted, which implies that cycling through all the positions in this way will sort the entire vector. The insertion sort algorithm is important in practice because it runs in linear time if the vector is already more or less in the correct order. It therefore makes sense to use insertion sort to restore order to a large vector in which only a few elements are out of sequence. Write an implementation of sort that uses the insertion sort algorithm. Construct an informal argument to show that the worst-case behaviour of insertion sort is O (N2).

Nov 19, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here