Introduction to Algorithms : A Sorting Algorithm
Print this page
 
       
  • Now, we take a more complex problem called sorting.
  • Problem Definition: Sort given n numbers by non-descending order.
  • There are many sorting algorithm. Insertion sort is a simple algorithm.
  • Insertion Sort: We can assume up to first number is sorted. Then sort up    to two numbers. Next, sort up to three numbers. This process continue till    we sort all n numbers.
  • Consider the following example of five integer:

           79 43 39 58 13 : Up to first number, 79, is sorted.
           43 79 39 58 13 : Sorted up to two numbers.
           39 43 79 58 13 : Sorted up to three numbers.
           39 43 58 79 13 : Sorted up to four numbers.
           13 39 43 58 79 : Sorted all numbers.
  • That is, if first (i-1) numbers are sorted then insert ith number into its correct     position. This can be done by shifting numbers right one number at a time     till a position for ith number is found.
  • That is, shift number at (i-1)th position to ith position, number in (i-2)th position to (i-1)th position, and so on, till we find a correct position for the number in ith     position. This method is depicted in the figure on right side.
   
An Applet Demonstrating Insertion
Sort
 
Prev