One family of internal sorting algorithms is selection sort. The basic idea of selection sort is to repeatedly select the smallest key in the remaining unsorted array. Simply put, an element is taken as the smallest key element
and then it is compared with the other elements to sort the array. It is implemented in the form of exchange selection sort that requires a single array to work with. In this technique, computer keeps on finding the next smallest element and brings it at its appropriate positions.
Selection sort can be used to sort an array in both ascending as well as descending order. The following program demonstrates
the implementation of selection sort in ascending order :
// Program to implement selection sort in C++ ( ascending order )
#include <iostream>
void selectionSort(int AR[],int size)
{
int i,j,temp;
for (i=0; i<size; i++)
{
for (j=i+1; j<=size; j++)
{
if ( AR[i] > AR[j] ) // sorts in ascending order
{
temp = AR[j];
AR[j] = AR[i];
AR[i] = temp;
}
} // end of sub loop
} // end of main loop
} // end of function selectionSort
int main()
{
int arr[20],size;
// prompts the user to enter the elements in the array
std::cout<<" Enter the no. of elements that you want to enter (max 20 ) : ";
std::cin>>size;
std::cout<<" Now enter the elements in the array ";
for (int i=0; i<size; i++)
{
std::cout<<" \n Element "<<i<<" : ";
std::cin>>arr[i];
}
selectionSort(arr,size); // calls the function to sort the array
std::cout<<" \n The sorted array is as follows ";
for (int i=0; i<size; i++)
{
std::cout<<" \n Element "<<i<<" : "<<arr[i];
}
return 0;
} // end of main
------ OUTPUT ------
Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.
------ Related Posts ------
Bubble Sort in C++ : http://programsplusplus.blogspot.in/2012/02/bubble-sort-in-c.html
Insertion Sort in C++ : http://programsplusplus.blogspot.in/2012/02/insertion-sort-in-c.html
and then it is compared with the other elements to sort the array. It is implemented in the form of exchange selection sort that requires a single array to work with. In this technique, computer keeps on finding the next smallest element and brings it at its appropriate positions.
Selection sort can be used to sort an array in both ascending as well as descending order. The following program demonstrates
the implementation of selection sort in ascending order :
// Program to implement selection sort in C++ ( ascending order )
#include <iostream>
void selectionSort(int AR[],int size)
{
int i,j,temp;
for (i=0; i<size; i++)
{
for (j=i+1; j<=size; j++)
{
if ( AR[i] > AR[j] ) // sorts in ascending order
{
temp = AR[j];
AR[j] = AR[i];
AR[i] = temp;
}
} // end of sub loop
} // end of main loop
} // end of function selectionSort
int main()
{
int arr[20],size;
// prompts the user to enter the elements in the array
std::cout<<" Enter the no. of elements that you want to enter (max 20 ) : ";
std::cin>>size;
std::cout<<" Now enter the elements in the array ";
for (int i=0; i<size; i++)
{
std::cout<<" \n Element "<<i<<" : ";
std::cin>>arr[i];
}
selectionSort(arr,size); // calls the function to sort the array
std::cout<<" \n The sorted array is as follows ";
for (int i=0; i<size; i++)
{
std::cout<<" \n Element "<<i<<" : "<<arr[i];
}
return 0;
} // end of main
------ OUTPUT ------
Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.
------ Related Posts ------
Bubble Sort in C++ : http://programsplusplus.blogspot.in/2012/02/bubble-sort-in-c.html
Insertion Sort in C++ : http://programsplusplus.blogspot.in/2012/02/insertion-sort-in-c.html