TELE INSERTION SORT The insertion sort is a simple sorting algorithm, but it is usually not the most efficient. To sort a list with ท elements, the insertion sort begins with the second element. The insertion sort compares this second element with the fi
element and inserts it before the first element if it does not exceed the first element and” after the first element if it exceeds the first element. At this point, the first two elements are in the correct order. The third element is then compared with the first element, and if it is larger than the first element, it is compared with the second element: it is inserted into the correct position among the first three elements.
In general, in the j th step of the insertion sort, the j th element of the list is inserted into the correct position in the; list of the previously sorted j — 1 elements. To insert the j th element in the list, a linear search technique is used (see Exercise 43): the j th element is successively compared with the already sorted j — 1 elements at the start of the list until the first element that is not less than this element is found or until it has been compared with all j — 1 elements; the j th element is inserted in the correct position so that the first j elements are sorted. The algorithm continues until the last element is placed in the correct position relative to the already sorted list of the first n — 1 elements. The insertion sort is described in pseudo code in Algorithm5.