Saturday, 18 April 2015

Selection Sort Algorithm

Selection Sort Algorithm:-

Like Bubble sort algorithm, Selection sort is also used to sort the given array elements but in Selection sort the smallest number from given array elements reaches its correct position or index in first pass. n-1 comparisons are made in the first pass, n-2 in second pass and so on until the given array is unsorted.
Total no. of comparisons =
(n-1) + (n-2) + ... + 4 + 3 + 2 + 1 = n(n-1)/2
Number of passes <= n - 1
where n is the no. of array elements.
            Complexity of Selection sort is also as same as of the Bubble sort, O(n^2).

Example:-
 

Bubble Sort Algorithm

Bubble Sort Algorithm :

Bubble sort algorithm is a method to sort the unsorted list. In Bubble sort technique largest number reaches its correct position /index on pass one. Pass 1 requires n-1 comparisons where n is the number of elements in the array. Similarly pass 2 requires one less comparison so number of comparisons are n-2 and so on until number of pass becomes equal to n-1. 
           So, total number of comparisons required are -
(n-1) + (n-2) + (n-3) + ........ + 3 + 2 + 1 = n(n-1)/2
           Thus, the complexity of bubble sort algorithm is O(n^2).

Example:- 
Sort the following array using Bubble Sort Algorithm-