Selection Sort is the simplest sorting technique. Selection sort algorithm starts by compairing first two elements of an array and swapping if necessary, i.e., if you want
and exchange it with the element in the second position, and continues in this way until the entire array is sorted.
This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list.
and exchange it with the element in the second position, and continues in this way until the entire array is sorted.
This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list.
The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array. This process continues moving unsorted array boundary by one element to the right.
Assume that we have a list on n elements. By applying selection sort, the first element is compared with all remaining (n-1) elements. The smallest element is placed at the first location. Again, the second element is compared with remaining (n-1) elements. At the time of comparison, the smaller element is swapped with larger element. Similarly, entire array is checked for smallest element and then swapping is done accordingly. Here we need (n-1) passes or iterations to completely rearrange the data.
Assume that we have a list on n elements. By applying selection sort, the first element is compared with all remaining (n-1) elements. The smallest element is placed at the first location. Again, the second element is compared with remaining (n-1) elements. At the time of comparison, the smaller element is swapped with larger element. Similarly, entire array is checked for smallest element and then swapping is done accordingly. Here we need (n-1) passes or iterations to completely rearrange the data.
Selection Sort Algorithm:
Let's consider an array with values
{3, 6, 1, 8, 4, 5}
Below, we have a pictorial representation of how selection sort will sort the given array.
In the first pass, the smallest element found is 1, so it is placed at the first position, then leaving the first element, next smallest element is searched from the rest of the elements. We get 3 as the smallest, so it is then placed at the second position. Then leaving 1 and 3, we search for the next smallest element from the rest of the elements and put it at third position and keep doing this until array is sorted.
Selection Sort Pseudocode:
list : array of items
n : size of list
for i = 1 to n - 1
/* set current element as minimum*/
min = i
/* check the element to be minimum */
for j = i+1 to n
if list[j] < list[min] then
min = j;
end if
end for
/* swap the minimum element with the current element*/
if indexMin != i then
swap list[min] and list[i]
end if
end for
end procedure
⇒Selection sort C:http://bit.ly/2Uzrely
Selection sort Complexity:
- Worst Case Time Complexity : O(n^2)
- Best Case Time Complexity : O(n^2)
- Average Time Complexity : O(n^2)
- Space Complexity : O(1)
No comments:
Post a Comment