Tuesday, 12 August 2014

Selection Sort

Complexity :

Best case: O(n^2)
average case:O(n^2)
Worst Case: O(n^2)

Technique :

  1. we first select the ith(for the first iteration i=0) element of the array and assume it to be the smallest 
  2. next we traverse from the i+1 position to the end of the array using the smallest function to find the position of the smallest element and we return its position .
  3. then the current value and the smallest value are compared and swapped.
  4. this entire process continues till the array is completely sorted. 

**Basically what happens is , in every iteration the smallest element element in the array is placed in its proper position.

Source code:

Output :

**the code given above can be sufficiently tweaked . i have purposely made it a bit more lengthy for clarity sake .

No comments:

Post a Comment