Monday, April 14, 2014

Insertion Sort in Python

Here is an implementation of Insertion sort in python.

Here we take the first element compare with zeroth element and swap values if the zeroth element is bigger. Now we take the second element. Compare with first one swap values if first one is bigger. Then again we compare the first value again with zeroth and swap if zeroth value is bigger. Like that
we do till end of the array.

Asymptotic Complexity
SomethingTime ComplexitySpace Complexity
Worst Case PerformanceSwaps: O (n^2)
Comparisons : O(n^2)

Best Case PerformanceSwaps: O (1 )
Comparisons : O(n) 

Average Case PerformanceSwaps: O(n^2)
Comparisons : O(n^2)

Worst Case space Complexity
Total: O (n)
Auxiliary : O(1)