forked from ash638/code-for-hactoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionsort.cpp
More file actions
42 lines (41 loc) · 794 Bytes
/
Selectionsort.cpp
File metadata and controls
42 lines (41 loc) · 794 Bytes
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
#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;
}