SELECTION SORT
Selection sort is conceptually the most simplest sorting algorithm. It is named as selection sort because at each iteration an index is selected and at the end of that iteration, elements till that index are sorted.
There are two ways of performing selection sort:
1.This algorithm finds the maximum value in the array and exchanges it with the element in the last position, then finds the second largest element and exchanges it with the second last position, and continues in this way until the array is sorted.
2. This algorithm finds the minimum value in the array and exchanges it with the element in the first position, then finds the second smallest value and exchanges it with the second position and continues in this way until the array is sorted.
Here we go with the 2nd way...
EXAMPLE
STEPS
1. Input the elements of an array.
2. Pass the array and its length to the function Insertion_Sort(int A , int N )
3. Intialize i with 0 and assign i to mid_index.
4.Initialize j with (i+1).
5.Compare the value at jth index with the value at min_index , update min_index if the condition
A[j]<A[mid_index] is true, with the lower value index.
6. Increment j as j+1;
7. Repeat steps (5-6) till j<N , where N is the length of an array.
8. If min_index is not equal to i, exchange the value at min_index with the value at ith index.
9. Increment i as i+i;
10. Repeat steps (8-9) till i<N.
PROGRAM
//Program demonstrating Selection Sort
# include <iostream>
using namespace std;
void selection_sort(int A[],int N)
{
int i,j,min_index,temp;
for(i=0;i<N;i++)
{
min_index = i;
for( j=(i+1);j<N;j++ ) //Loop to find the smallest element in the remaining array.
{
if(A[j]<A[min_index])
{
min_index = j;
}
}
if(min_index != i)
{
temp = A[min_index];
A[min_index] = A[i];
A[i] = temp;
}
}
cout<<"SORTED ARRAY:"<<endl;
for(i=0;i<N;i++)
{
cout<<"A["<<i<<"]: "<<A[i]<<endl;
}
}
int main()
{
int Arr[5],i;
for(i=0;i<5;i++) //Loop to input the elements of an array(unsorted basically)
{
cin>>Arr[i];
}
selection_sort(Arr,5);
return 0;
}
OUTPUT
Selection sort is conceptually the most simplest sorting algorithm. It is named as selection sort because at each iteration an index is selected and at the end of that iteration, elements till that index are sorted.
There are two ways of performing selection sort:
1.This algorithm finds the maximum value in the array and exchanges it with the element in the last position, then finds the second largest element and exchanges it with the second last position, and continues in this way until the array is sorted.
2. This algorithm finds the minimum value in the array and exchanges it with the element in the first position, then finds the second smallest value and exchanges it with the second position and continues in this way until the array is sorted.
Here we go with the 2nd way...
EXAMPLE
STEPS
1. Input the elements of an array.
2. Pass the array and its length to the function Insertion_Sort(int A , int N )
3. Intialize i with 0 and assign i to mid_index.
4.Initialize j with (i+1).
5.Compare the value at jth index with the value at min_index , update min_index if the condition
A[j]<A[mid_index] is true, with the lower value index.
6. Increment j as j+1;
7. Repeat steps (5-6) till j<N , where N is the length of an array.
8. If min_index is not equal to i, exchange the value at min_index with the value at ith index.
9. Increment i as i+i;
10. Repeat steps (8-9) till i<N.
//Program demonstrating Selection Sort
# include <iostream>
using namespace std;
void selection_sort(int A[],int N)
{
int i,j,min_index,temp;
for(i=0;i<N;i++)
{
min_index = i;
for( j=(i+1);j<N;j++ ) //Loop to find the smallest element in the remaining array.
{
if(A[j]<A[min_index])
{
min_index = j;
}
}
if(min_index != i)
{
temp = A[min_index];
A[min_index] = A[i];
A[i] = temp;
}
}
cout<<"SORTED ARRAY:"<<endl;
for(i=0;i<N;i++)
{
cout<<"A["<<i<<"]: "<<A[i]<<endl;
}
}
int main()
{
int Arr[5],i;
for(i=0;i<5;i++) //Loop to input the elements of an array(unsorted basically)
{
cin>>Arr[i];
}
selection_sort(Arr,5);
return 0;
}
OUTPUT