forked from souvikg544/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionSort.cpp
More file actions
51 lines (38 loc) · 875 Bytes
/
selectionSort.cpp
File metadata and controls
51 lines (38 loc) · 875 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
#include<bits/stdc++.h>
using namespace std;
int selectSort(int a[], int size){
int i, j, min, temp;
//outer loop
for(i = 0; i < size-1; i++){
min = i;
//inner loop
for(j = i+1; j < size; j++){
if(a[min] > a[j])
//update the new min if the condition is true
min = j;
//swap the element present in a[min] with element present in a[i]
temp = a[min];
a[min] = a[i];
a[i] = temp;
}
}
return 0;
}
int print(int a[], int size)
{
int i;
for (i = 0; i < size; i++)
cout<<a[i]<<" ";
return 0;
}
int main(){
//declaration and initialisation of an array
int a[] = {109,223,18,190,119};
//calculatingg the size of array
int size = sizeof(a)/sizeof(a[0]);
//calling the selectSort() function
selectSort(a, size);
//calling the print() function
print(a, size);
return 0;
}