/***************************************************************
* Name: Prof. Rafael Orta
* Course: Computer Science & Programming
* Class: CS04225
*****************************************************************
* Purpose: Demonstrate the use of selection sort.
*****************************************************************/
#include<iostream>
using namespace std;
int main()
{
int i=0; // Variable used to traverse the array
int j=0; // Variable used to traverse the array subset of the array
int loc=0; // Variable holding the location of the element to swap.
int temp=0; // Variable used to swap values
int min=0; // Varaible used to keep the lowest value found.
int n=11; // Variable used to keep track of the elements in the array.
int a[11] = {5,16,3,17,18,15,4,2,1,20,6}; // Array declaration
// Displaying the unsorted list
cout<<"\nUnsorted list is as follows\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
// Sorting the list.
for(i=0; i<n; i++)
{
min=a[i]; //Assume first value is the lowest.
loc=i; //Assigs the location of the first element to compare.
for(j=i+1;j<n;j++)
{
if(min>a[j])
{
min=a[j];
loc=j;
}
}
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
// Displaying the sorted list.
cout<<"\nSorted list is as follows\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}