-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectionSort.cpp
53 lines (50 loc) · 989 Bytes
/
SelectionSort.cpp
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
#include<bits/stdc++.h>
using namespace std;
void SelectionSortV(vector<int> &arr)
{
int n = arr.size();
for (int i = 0; i < n - 1; i++)
{
int min = i;
for(int j = i + 1; j < n; j++){
if(arr[j] < arr[min]){
min = j;
}
}
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
void SelectionSort(int arr[], int n)
{
for (int i = 0; i < n - 1; i++)
{
int min = i;
for (int j = i + 1; j < n; j++)
{
if (arr[j] < arr[min])
{
min = j;
}
}
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
int main()
{
vector<int> arr = {7, 4, 2, 1, 7, 5, 0};
for (int i = 0; i < 7; i++)
{
cout << arr[i] << " ";
}
SelectionSortV(arr);
cout << endl;
for (int i = 0; i < 7; i++)
{
cout << arr[i] << " ";
}
return 0;
}