-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionSort.cpp
More file actions
48 lines (48 loc) · 1.16 KB
/
selectionSort.cpp
File metadata and controls
48 lines (48 loc) · 1.16 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
#include<iostream>
using namespace std;
void swap(int &a, int &b) {
int temp;
temp=a;
a=b;
b=temp;
}
void display(int *a, int n) {
int i;
for(i=0;i<n;i++){
cout<<a[i]<<" ";
}
}
void selectSort(int *a, int n){
int i,j,loc, min;
//min-store smallest value in array ,loc-store location of minimum value
for(i=0;i<n;i++)
{
min=a[i]; //every array element will be copied in min variable one by one and will be compared with all the next right side elements
loc=i;
for(j=i;j<n;j++)
{
if(a[j]<min) //It will find smallest element on right side of comparing element in Array
{
loc=j;
min=a[j];
}
}
swap(a[i], a[loc]) ; //Smallest element in right side of array will be swapped with the comparing element
}
}
int main(){
int n, i;
cout<<"\nEnter Number of Elements ";
cin>>n;
int a[n];
cout<<"\nEnter "<<n<<" Elements ";
for(i=0;i<n;i++){
cin>>a[i];
}
cout<<"\nBefore Sorting : ";
display(a,n) ;
selectSort(a,n) ;
cout<<"\nAfter Sorting : ";
display(a,n);
return 0;
}