Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions 06. Sorting/Selection Sort cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include<iostream>
using namespace std;

int findSmallest (int[],int);
int main ()
{
int myarray[5] = {12,45,8,15,33};
int pos,temp;
cout<<"\n Input list of elements to be Sorted\n";
for(int i=0;i<5;i++)
{
cout<<myarray[i]<<"\t";
}
for(int i=0;i<5;i++)
{
pos = findSmallest (myarray,i);
temp = myarray[i];
myarray[i]=myarray[pos];
myarray[pos] = temp;
}
cout<<"\n Sorted list of elements is\n";
for(int i=0;i<5;i++)
{
cout<<myarray[i]<<"\t";
}
return 0;
}
int findSmallest(int myarray[],int i)
{
int ele_small,position,j;
ele_small = myarray[i];
position = i;
for(j=i+1;j<5;j++)
{
if(myarray[j]<ele_small)
{
ele_small = myarray[j];
position=j;
}
}
return position;
}
Output:

Input list of elements to be Sorted

12 45 8 15 33

Sorted list of elements is

8 12 15 33 45