Insertion Sort Animation with User Input

Algorithm Description

This is a sorting algorithm that in the "worst case" behaves like the more inefficient Bubble Sort and Selection Sort Algorithms, but in many "average cases" performs better. The code for the algorithm is as follows:

	for (i = 1; i < n; i++)
      		Inserted = false;  // boolean variable 
      		j = i;
      		while ((j >= 1) && (Inserted == false)) 
        		if (List[j] < List[j-1])
          			swap(List[j], List[j-1])
          			Inserted = true;

Note : This algorithm sorts in ascending order - i.e., smaller elements move to the front of the list.

Instructions on use of the applet

The user of the applet is advised to read this section in order to make the best use of the applet. If you have a large enough browser window, this page should be scrolled so that the applet itself (including the interaction panel at the bottom of the display part of the applet) is visible along with these instructions. The buttons in the interaction panel are to be used in the following manner:


NOTE : Clicking on the ">>" or "<<" buttons will move the sort forward (or backward) at a VERY FAST pace, which may be unobservable. These are essentially "Fast Forward" or "Rewind" buttons. If you want to see the sort move ahead at a reasonable pace, click on the "Slow" button and then use the slider bar to adjust the delay to a suitable value.

Insertion Sort Animation with User Input

Explanation of operation

This algorithm works by first considering the first element of the list (element 0) to be correctly placed. It may not be, of course. In that case, it proceeds to the next element (element 1) and considers its magnitude vis-a-vis element 0. If the magnitude is smaller, it swaps it with element 0. Now, elements 0 and 1 are sorted, but only with respect to each other. Element 2 is now considered, and is moved to its correct position with respect to elements 0 and 1, i.e., it may stay where it currently is, or it may be placed between what are now elements 0 and 1, or it may be placed before element 0. To do this, elements 2 and 1 may be swapped, resulting in the placement of element 2 between what is currently element 0 and element 1, and then another swap between what are now elements 0 and 1 may be done to place the element before what is now element 0 - if all this swapping is indeed necessary. TRY THIS with the first three elements of the list above (in the applet) and see what happens. This continues over and over again, considering the elements from element 3 through the last element of the list. Each element is sought to placed in its "proper" position vis-a-vis the part of the list you already consider sorted. Note that each element under consideration is swapped with its immediately preceding neighbor as it moves towards the front of the list. The moment the swapping stops, the element's immediately preceding neighbor has lesser magnitude, and so the current element may be considered to be properly placed.

Complexity of the algorithm

In this method there are N-1 insert operations. The j-th insert operation inserts a new element into a sorted array of j elements and hence takes at most j comparisons. Thus the total number of comparisons is

1 + 2 + 3 + . + (N-1) = N*(N-1)/2 = O(N^2)