-
Notifications
You must be signed in to change notification settings - Fork 0
/
n2_Sort.cpp
70 lines (63 loc) · 1.2 KB
/
n2_Sort.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// #include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#define debug(x) cout << #x << " " << x << endl;
void printArr(int k, int *a, int n){
printf("%d: ", k);
for(int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}
void bubbleSort(int a[], int n){
for(int i = 0; i < n-1; i++){
bool isSorted = true;
for(int j = 0; j < n-i-1; j++){
if(a[j] > a[j+1]){
isSorted = false;
int tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}
}
// printArr(i, a, n);
if (isSorted)
break;
}
}
void insertionSort(int a[], int n){
for(int i = 1; i < n; i++){
int ai = a[i];
int j = i-1;
while(j >= 0 && ai < a[j]){
a[j+1] = a[j];
j--;
}
a[j+1] = ai;
// printArr(i, a, n);
}
}
void selectionSort(int a[], int n){
for(int i = 0; i < n; i++){
int minIdx = i;
for(int j = i+1; j < n; j++){
if(a[minIdx] > a[j])
minIdx = j;
}
if(minIdx != i){
int tmp = a[minIdx];
a[minIdx] = a[i];
a[i] = tmp;
}
printArr(i, a, n);
}
}
int main(){
int n; cin >> n;
int a[n];
for(int i = 0; i < n; i++)
cin >> a[i];
selectionSort(a, n);
printArr(0, a, n);
return 0;
}