-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path46-selection_sort.cpp
More file actions
61 lines (60 loc) · 814 Bytes
/
46-selection_sort.cpp
File metadata and controls
61 lines (60 loc) · 814 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//selection sort
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
class selection
{
public:
int min,minpos,n;
int ar[100];
selection()
{
min=0;
minpos=0;
}
void sort()
{
for(int i=0;i<n;i++)
{
min=ar[i];
minpos=i;
for(int j=i+1;j<n;j++)
{
if(ar[j]<min)
{
minpos=j;
min=ar[j];
}
}
int temp=ar[i];
ar[i]=ar[minpos];
ar[minpos]=temp;
}
}
void getvalue()
{
cout<<"enter total elements\n";
cin>>n;
cout<<"enter elements\n";
for(int i=0;i<n;i++)
cin>>ar[i];
}
void display()
{
for(int i=0;i<n;i++)
cout<<ar[i]<<endl;
}
};
int main()
{
selection obj;
obj.getvalue();
cout<<"elements before sorting\n";
obj.display();
obj.sort();
cout<<"elements after sorting\n";
obj.display();
getch();
return 0;
}