(45 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
<metadesc>Complexities of basic sorting algorithms. Minimum no. of swaps required for sorting algorithms  </metadesc>
  
{|class="wikitable" style="text-align: center; color: black;"
+
{{alert|Please don't by heart this table. This is just to check your understanding and you should know how these complexities came|alert-danger}}
! align="left"| Algorithm
+
{|class="wikitable" style="color: black; text-align: center; " width="85%"
 +
!align = "center" |Algorithm
 
! Worst Case
 
! Worst Case
 
! Average Case
 
! Average Case
Line 8: Line 10:
 
! Max. no. of swaps
 
! Max. no. of swaps
 
|-
 
|-
|Bubble
+
! align = "center"|[http://en.wikipedia.org/wiki/Bubble_sort Bubble]
|$\theta(n^{2})$
+
! align = "center"|$\Theta(n^{2})$
|$\theta(n^{2})$
+
! align = "center"|$\Theta(n^{2})$
|$\theta(n^{2})$
+
! align = "center"|$\Theta(n)$
|$\theta(n^{2})$
+
! align = "center"|0
|$\theta(n^{2})$
+
! align = "center" |$\Theta(n^{2})$
 
|-
 
|-
|Selection
+
! align = "center"|[http://en.wikipedia.org/wiki/Selection_sort Selection]
|$\theta(n^{2})$
+
! align = "center"|$\Theta(n^{2})$
|$\theta(n^{2})$
+
! align = "center"|$\Theta(n^{2})$
|$\theta(n^{2})$
+
! align = "center"|$\Theta(n^{2})$
|0 (when array is already sorted)
+
! align = "center"|0  
|$\theta(n^{2})$
+
! align = "center"|$\Theta(n)$
 
|-
 
|-
|Insertion
+
! align = "center"|[http://en.wikipedia.org/wiki/Insertion_sort Insertion]
|$\theta(n^{2})$
+
! align = "center"|$\Theta(n^{2})$
|$\theta(n^{2})$
+
! align = "center"|$\Theta(n^{2})$
|$\theta(n)$ (when array is sorted)
+
! align = "center"|$\Theta(n)$  
|0 (when array is already sorted)
+
! align = "center"|0  
|$\theta(n^{2})$
+
! align = "center"|$\Theta(n^{2})$
 
|-
 
|-
|Quick
+
! align = "center"|[http://en.wikipedia.org/wiki/Quick_sort Quick]
|$\theta(n^{2})$ (when array is sorted, and pivot taken from one end)
+
! align = "center"|$\Theta(n^{2})$  
|$\theta(nlgn)$
+
! align = "center"|$\Theta(n\lg n)$
|$\theta(nlgn)$
+
! align = "center"|$\Theta(n\lg n)$
|0 (when array is already sorted)
+
! align = "center"|0  
|$\theta(n^{2})$
+
! align = "center"|$\Theta(n^{2})$
 
|-
 
|-
|Merge
+
! align = "center"|[http://en.wikipedia.org/wiki/Merge_sort Merge]
|$\theta(nlgn)$
+
! align = "center"|$\Theta(n\lg n)$
|$\theta(nlgn)$
+
! align = "center"|$\Theta(n\lg n)$
|$\theta(nlgn)$
+
! align = "center"|$\Theta(n\lg n)$
|Not in place sorting
+
! align = "center"|Is not in-place sorting
|Not in place sorting
+
! align = "center"|Is not in-place sorting
 
|-
 
|-
|Heap
+
! align = "center"|[http://en.wikipedia.org/wiki/Heap_sort Heap]
|$\theta(nlgn)$
+
! align = "center"|$\Theta(n\lg n)$
|$\theta(nlgn)$
+
! align = "center"|$\Theta(n\lg n)$
|$\theta(nlgn)$
+
! align = "center"|$\Theta(n\lg n)$
|$\theta(nlgn)$
+
! align = "center"|$O(n\lg n)$
|$\bigo(nlgn)$
+
! align = "center"|$\Theta(n\lg n)$
 
|}
 
|}
  
 +
The best case for insertion sort happens when the input array is already sorted. Each new element will then be put at the end of the array and the complexity will be $\Theta(n)$.
  
 +
Interestingly, the worst case for quick sort also happens when the input array is sorted (either ascending or descending). This happens only if we choose the pivot as either the first element or the last element. In either of these case, we split the array across the pivot with n-1 elements on one array and just 1 element on the other array, which makes the complexity go to $\Theta(n^2)$
  
<div class="fb-like"  data-layout="standard" data-action="like" data-show-faces="false" data-share="true"></div>
+
===In-Place Sorting===
 +
When sorting happens within the same array, its called in-place sorting. All the above sorting algorithms except merge sort are in-place sorting. In merge sort, we use an auxiliary array for merging two sub-arrays and hence its not in-place sorting.
  
 +
===Stable sorting===
 +
This matters only for arrays with some element(s) repeated. If $A[x] = A[y]$, in the original array such that $x<y$, then in the sorted array, $A[x]$ must be placed at position $u$ and $A[y]$ must be placed at position $v$, such that $u<v$. i.e.; the relative positions of same value elements must be same in the sorted array as in the input array, for the sorting algorithm to be stable. In most cases stability depends on the way in which algorithm is implemented. Still, its not possible to implement heap sort as a stable sort.
  
<div class="fb-share-button"  data-type="button_count"></div>
+
{{Template:FBD}}
  
 +
[[Category: Algorithms & Data Structures Notes]]
  
 
+
[[Category: Compact Notes for Reference of Understanding]]
<disqus/>
 
[[Category: CS Notes]]
 
[[Category: Algorithms & Data Structures]]
 

Latest revision as of 15:20, 15 February 2016


Heads Up! Please don't by heart this table. This is just to check your understanding and you should know how these complexities came
Algorithm Worst Case Average Case Best Case Min. no. of swaps Max. no. of swaps
Bubble $\Theta(n^{2})$ $\Theta(n^{2})$ $\Theta(n)$ 0 $\Theta(n^{2})$
Selection $\Theta(n^{2})$ $\Theta(n^{2})$ $\Theta(n^{2})$ 0 $\Theta(n)$
Insertion $\Theta(n^{2})$ $\Theta(n^{2})$ $\Theta(n)$ 0 $\Theta(n^{2})$
Quick $\Theta(n^{2})$ $\Theta(n\lg n)$ $\Theta(n\lg n)$ 0 $\Theta(n^{2})$
Merge $\Theta(n\lg n)$ $\Theta(n\lg n)$ $\Theta(n\lg n)$ Is not in-place sorting Is not in-place sorting
Heap $\Theta(n\lg n)$ $\Theta(n\lg n)$ $\Theta(n\lg n)$ $O(n\lg n)$ $\Theta(n\lg n)$

The best case for insertion sort happens when the input array is already sorted. Each new element will then be put at the end of the array and the complexity will be $\Theta(n)$.

Interestingly, the worst case for quick sort also happens when the input array is sorted (either ascending or descending). This happens only if we choose the pivot as either the first element or the last element. In either of these case, we split the array across the pivot with n-1 elements on one array and just 1 element on the other array, which makes the complexity go to $\Theta(n^2)$

In-Place Sorting

When sorting happens within the same array, its called in-place sorting. All the above sorting algorithms except merge sort are in-place sorting. In merge sort, we use an auxiliary array for merging two sub-arrays and hence its not in-place sorting.

Stable sorting

This matters only for arrays with some element(s) repeated. If $A[x] = A[y]$, in the original array such that $x<y$, then in the sorted array, $A[x]$ must be placed at position $u$ and $A[y]$ must be placed at position $v$, such that $u<v$. i.e.; the relative positions of same value elements must be same in the sorted array as in the input array, for the sorting algorithm to be stable. In most cases stability depends on the way in which algorithm is implemented. Still, its not possible to implement heap sort as a stable sort.




blog comments powered by Disqus
Algorithm Worst Case Average Case Best Case Min. no. of swaps Max. no. of swaps
Bubble $\theta(n^{2})$ $\theta(n^{2})$ $\theta(n^{2})$ $\theta(n^{2})$ $\theta(n^{2})$
Selection $\theta(n^{2})$ $\theta(n^{2})$ $\theta(n^{2})$ 0 (when array is already sorted) $\theta(n^{2})$
Insertion $\theta(n^{2})$ $\theta(n^{2})$ $\theta(n)$ (when array is sorted) 0 (when array is already sorted) $\theta(n^{2})$
Quick $\theta(n^{2})$ (when array is sorted, and pivot taken from one end) $\theta(nlgn)$ $\theta(nlgn)$ 0 (when array is already sorted) $\theta(n^{2})$
Merge $\theta(nlgn)$ $\theta(nlgn)$ $\theta(nlgn)$ Not in place sorting Not in place sorting
Heap $\theta(nlgn)$ $\theta(nlgn)$ $\theta(nlgn)$ $\theta(nlgn)$ $\bigo(nlgn)$




blog comments powered by Disqus