-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionSort.cpp
More file actions
41 lines (39 loc) · 1.11 KB
/
selectionSort.cpp
File metadata and controls
41 lines (39 loc) · 1.11 KB
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
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
// this is the program for the selection sorted
int n;
cin>>n;
int arr[100001];
for(int i = 0 ; i < n ; i++){
cin>>arr[i];
}
// in selection sort i used to select all the element one
// after another and assume that element to be minimum.
int min,pos;
for(int i = 0 ; i < n ;i++){
min = arr[i];
pos = i;
for(int j = i+1 ; j < n ;j++){
//then i really find whether the assumed element is
// actually minimum or not if not then i find the minimum
//element from the selection and update the same
if(arr[j]<min){
min = arr[j];
pos = j;
}
}
// once i'll able to find the real minimum element then
// i can swap the values
int temp = arr[i];
arr[i] = arr[pos];
arr[pos] = temp;
// after this i am sure that 1 minimum element
// is sorted and placed at the right position
}
for(int i = 0 ; i < n ; i++){
cout<<arr[i]<<endl;
}
return 0;
}