Selection Sort is an operation that is performed on arrays.
Out of the 4 ways of sorting arrays which are : Bubble Sort, Selection Sort, Insertion Sort, Merge Sort it is one of the easiest to understand algorithm.
As the name suggests it basically selects 1 element from the array and takes it to the first position. Now the Selection of that element depends upon us, it can be either the smallest element(for ascending order) or the greatest element(descending order).
So going as per the definition there are two ways in which we can do the same.
Now talking about the second method we are basically starting from the 1st element and then comparing it with all elements of the array. As and when we find an element smaller than that particular element we swap. after comparision with all elements we have the smallest element at the 1st position. This process is called 1st pass. Then we will take the next index which would be our next pass or the 2nd pass and comparing it with the next elements.
Now Look at the example Closely :
Out of the 4 ways of sorting arrays which are : Bubble Sort, Selection Sort, Insertion Sort, Merge Sort it is one of the easiest to understand algorithm.
As the name suggests it basically selects 1 element from the array and takes it to the first position. Now the Selection of that element depends upon us, it can be either the smallest element(for ascending order) or the greatest element(descending order).
So going as per the definition there are two ways in which we can do the same.
- One can be by swapping the smallest element in the array by the element number on which we are on.
Eg: if the array was 3 , 8 , 2 , 1
after 1st pass it becomes 1 , 8 , 2 , 3
2nd pass 1 , 2 , 8 , 3
3rd pass 1 , 2 , 3 , 8. - The other and the easier way can be by finding the smallest element and bring it to the position on which we are working by swapping irrespective of the element with which we are swapping it with.
We will be talking about the second method.
Though the first method can be found on http://www.thecrazyprogrammer.com/2014/12/selection-sort.html
Now talking about the second method we are basically starting from the 1st element and then comparing it with all elements of the array. As and when we find an element smaller than that particular element we swap. after comparision with all elements we have the smallest element at the 1st position. This process is called 1st pass. Then we will take the next index which would be our next pass or the 2nd pass and comparing it with the next elements.
Now Look at the example Closely :
So thats the 1st pass. Now Look at the 2nd pass :
So in the second pass you saw 2 was constant and the comparision were from 2nd index and so it goes on to finally gives 2 , 4 , 5 , 6 , 7
Here we have the code in C++ :
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size, arr[50], i, j, temp;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter Array Elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Sorting array using selection sort...\n";
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<"Now the Array after sorting is :\n";
for(i=0; i<size; i++)
{
cout<<arr[i]<<" ";
}
getch();
}
For the understanding of this code watch our video on selection sort.
No comments:
Write comments