-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort2.cpp
More file actions
44 lines (36 loc) · 759 Bytes
/
Copy pathsort2.cpp
File metadata and controls
44 lines (36 loc) · 759 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
#include <iostream>
using namespace std;
void selection_sort(int arr[], int n)
{
for (int i = 0; i < n - 1; i++) // outer loop
{
int mini = i;
for (int j = i + 1; j < n; j++) // start from i+1
{
if (arr[j] < arr[mini])
{
mini = j;
}
}
// swap arr[i] and arr[mini]
int temp = arr[mini];
arr[mini] = arr[i];
arr[i] = temp;
}
}
int main()
{
int n;
cin >> n;
int arr[1000]; // use a fixed size large enough (instead of VLA)
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
selection_sort(arr, n);
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
return 0;
}