# TUTORIAL-3 - TUTORIAL-3 INSERTION SORT: It sequentially...

This preview shows pages 1–2. Sign up to view the full content.

TUTORIAL-3 INSERTION SORT: It sequentially processes a list of records. Each record is inserted in turn at the correct position. Best case complexity is O(n) and it occurs when the input array is already sorted. Because in this case always the left side element is smaller than the right side element (since the array is sorted) the inner while loop of the insertion sort algorithm (page 47 of course book) will not execute any time. So only due to outer for loop the algorithm becomes linear ie O(n) (suppose n is the size of the array). Worst case complexity is O(n 2 ) and it occurs when the input array is reversely sorted. Because in this case always the left side element is greater than the right side element the inner while loop will execute every time. So considering both outer for loop and inner while loop the algorithm becomes quadratic ie O(n 2 ). Average case complexity is O(n 2 ) and it occurs when the input array is randomly sorted. In this case the inner while loop will execute randomly still making the algorithm O(n 2 ) complexity. Insertion sort algorithm is simpler and better for very small amount of data. Since the over all time complexity of this algorithm is O(n 2 ), for large number of input data this algorithm is not good for practical use. Inversion:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 03/15/2010 for the course STATISTIC 472 taught by Professor Amjad during the Spring '08 term at Yarmouk University.

### Page1 / 4

TUTORIAL-3 - TUTORIAL-3 INSERTION SORT: It sequentially...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online