Selection Sort Method


Selection Sort method is a simple sorting method. It works fine for smaller number of elements in the list.
Selection sort is an example of Internal Sort.
In order to sort N elements using selection sort technique we required to perform N-1 PASS.
During First PASS entire array is searched from first element to find the smallest element. When smallest element is found, it is interchanged with the first element in the array. Thus the element with the smallest value is placed at first position in the array.
During Second PASS array is searched from second element to find the second smallest element. When this element is found, it is interchanged with the second element in the array. Thus the element with the second smallest value is placed at second position in the array.
During each successive PASS the smallest element is placed at its proper position and this process continues until all the elements in the array are arranged in ascending order.
During first pass we required N-1 comparisons. During second pass we required N-2 comparisons and during Ith pass we required N-I comparisons.
Thus the order of comparisons is proportional to N2 i.e. O(N2)

     

Example of Selection Sort

  Pass1 Pass2 Pass3 Pass4 Pass5 Pass6 Pass7
5
-2
-2
-2
-2
-2
-2
-2
2
2
0
0
0
0
0
0
35
35
35
2
2
2
2
2
9
9
9
9
3
3
3
3
3
3
3
3
9
5
5
5
-2
5
5
5
5
9
9
9
52
52
52
52
52
52
52
35
0
0
2
35
35
35
35
52
  Indicates elements to be compare during each PASS.
  Indicates elements which are already sorted.

Download Projects


Download Programs