Saturday, July 17, 2010

Insertion Sort Analysis

Algorithm Analysis

Consider the entry i.
There are i possible positions :
  • not moving at all,
  • moving 1 position,
  • ...
  • moving (n-1) positions.
All the above occur with equal probability. Hence:
  • Probability of not moving at all is 1/i.
  • Probability of moving is (i-1)/i
Since all of the (i-1) possible positions are equally likely, the average number of iterations are:
1+2+.... + (i-1) = (i-1)i = _i_
(i-1) 2(i-1) 2



Insertion Sort

Insertion Sort works on the fundamental effort to turn the given the unsorted list into a ordered list. So before describing the Insertion Sort itself, we'll look a bit at the fundamental of the Ordered List.

The Concept - Ordered List

Ordered List is a data-structure which is defined as a list in which every entry contains a key, such that the keys are in order.
Thus if an entry i comes before entry j in the list, then the key of entry i is less than or equal to the key of entry j. Thus the entries i and j are ordered in the list.

Ordered Insertion forms the fundamental idea behind the Insertion Sort. Insertion mechanism into the Ordered List may be termed as Ordered Insertion. The whole idea revolves around the effort to convert the unordered list into the ordered list. The elements can be inserted in the ordered list by Ordered Insertion.

Given an Ordered List L, if we wish to insert a new entry e, then the following steps need to be followed:
  1. Find the correct Location in the list L where the entry e belongs. Thus Location(e) in L will be such that, all elements of L before Location(e) will be less that e and all elements of L after Location(e) will be greater than e.
  2. Insert the entry at Location(e) in the list L.



Sorting Algorithms

Over the next few posts, I'll review several sorting algorithms. Some of these algorithms are:
  1. Insertion Sort.
  2. Selection Sort.
  3. Shell Sort.
  4. Divide and Conquer Sorting.
  5. Quick Sort.

Although Knuth describes some 25 sorting algorithms, we will proceed only linearly to know some of those depending on their academic merit and/or problem specific utility.

The Random Diary

My effort to maintain a log of my random readings !