-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection_sort.cpp
More file actions
51 lines (51 loc) · 1.06 KB
/
selection_sort.cpp
File metadata and controls
51 lines (51 loc) · 1.06 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
42
43
44
45
46
47
48
49
50
51
#include<iostream>
using namespace std;
int enter_numbers(int m);
int selection_sort(int *arr,int n);
int swap(int *arr,int start,int end);
int enter_numbers(int m){
int *p=new int[m];
for(int i=0;i<m;i++){
cin>>p[i];
}
selection_sort(p,m);
cout<<"SOrted numbers"<<endl;
for(int i=0;i<m;i++){
cout<<p[i]<<" ";
}
cout<<endl;
return 0;
}
int swap(int *arr,int start,int end){
int temp;
temp=arr[start];
arr[start]=arr[end];
arr[end]=temp;
return end;
}
int selection_sort(int *arr,int n){
for(int startpos=0;startpos<n;startpos++){
int minpos=startpos;
for(int j=minpos+1;j<n;j++){
if(arr[minpos]>arr[j]){
minpos=j;
}
//after this comment 5 lines are optional it's just used to know inside loop processing
cout<<"Sorted numbers"<<startpos<<endl;
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
swap(arr,startpos,minpos);
}
return 0;
}
int main(){
int n;
cout<<"How many numbers : ";
cin>>n;
cout<<"Enter Numbers :"<<endl;
enter_numbers(n);
return 0;
}