MCPcopy Create free account
hub / github.com/ShahjalalShohag/code-library / main

Function main

Basics/Selection Sort.cpp:4–32  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2using namespace std;
3
4int32_t main() {
5 ios_base::sync_with_stdio(0);
6 cin.tie(0);
7 int n; cin >> n;
8 int a[n + 1];
9 for (int i = 1; i <= n; i++) {
10 cin >> a[i];
11 }
12 int swaps = 0;
13 for (int i = 1; i <= n; i++) {
14 int min_index = i;
15 for (int j = i + 1; j <= n; j++) {
16 if (a[j] < a[min_index]) {
17 min_index = j;
18 }
19 }
20 if (i != min_index) {
21 swap(a[i], a[min_index]);
22 ++swaps;
23 }
24 }
25 for (int i = 1; i <= n; i++) {
26 if (i > 1) cout << ' ';
27 cout << a[i];
28 }
29 cout << '\n';
30 cout << swaps << '\n';
31 return 0;
32}
33// https://vjudge.net/problem/Aizu-ALDS1_2_B

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected