-
Notifications
You must be signed in to change notification settings - Fork 33
/
selectionSort.cpp
43 lines (43 loc) · 1.01 KB
/
selectionSort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
using namespace std;
int findSmallest (int[],int);
int main ()
{
int myarray[10] = {11,5,2,20,42,53,23,34,101,22};
int pos,temp,pass=0;
cout<<"\n Input list of elements to be Sorted\n";
for(int i=0;i<10;i++)
{
cout<<myarray[i]<<"\t";
}
for(int i=0;i<10;i++)
{
pos = findSmallest (myarray,i);
temp = myarray[i];
myarray[i]=myarray[pos];
myarray[pos] = temp;
pass++;
}
cout<<"\n Sorted list of elements is\n";
for(int i=0;i<10;i++)
{
cout<<myarray[i]<<"\t";
}
cout<<"\nNumber of passes required to sort the array: "<<pass;
return 0;
}
int findSmallest(int myarray[],int i)
{
int ele_small,position,j;
ele_small = myarray[i];
position = i;
for(j=i+1;j<10;j++)
{
if(myarray[j]<ele_small)
{
ele_small = myarray[j];
position=j;
}
}
return position;
}