#include <stdio.h>
int BubbleSort(int arr[10], int n) {
int a = 0, b = 0, c = 0; // 순환문 사용 변수
int temp = 0; // 임시 변수
int compare = 0; // 비교 횟수
int change = 0; // 자리 교환 횟수
for (a = 0; a < n - 1; a++) {
for (b = 0; b < (n - 1)-a; b++) {
if (arr[b] < arr[b + 1]) {
temp = arr[b];
arr[b] = arr[b + 1];
arr[b + 1] = temp;
change++;
}
compare++;
}
for (c = 0; c < n; c++) {
printf(" %3d", arr[c]);
}
printf(" | 비교 : %2d , 교환 : %2d\\n", compare, change);
}
return 0;
}
void main() {
int arr[10] = { 50 ,23, 84, 49, 71, 15, 99, 62, 36, 7 };
printf("\\n === Bubble Sort Start === \\n\\n");
BubbleSort(arr, 10);
printf("\\n === Bubble Sort End === \\n\\n");
}
<aside> ➡️ === Bubble Sort Start ===
50 84 49 71 23 99 62 36 15 7 | 비교 : 9 , 교환 : 6 84 50 71 49 99 62 36 23 15 7 | 비교 : 17 , 교환 : 11 84 71 50 99 62 49 36 23 15 7 | 비교 : 24 , 교환 : 14 84 71 99 62 50 49 36 23 15 7 | 비교 : 30 , 교환 : 16 84 99 71 62 50 49 36 23 15 7 | 비교 : 35 , 교환 : 17 99 84 71 62 50 49 36 23 15 7 | 비교 : 39 , 교환 : 18 99 84 71 62 50 49 36 23 15 7 | 비교 : 42 , 교환 : 18 99 84 71 62 50 49 36 23 15 7 | 비교 : 44 , 교환 : 18 99 84 71 62 50 49 36 23 15 7 | 비교 : 45 , 교환 : 18
=== Bubble Sort End ===
</aside>
#include <stdio.h>
int InsertSort(int arr[10], int n) {
int a = 0, b = 0, c = 0; // 순환문 사용 변수
int temp = 0; // 임시 변수
int compare = 0; // 비교 횟수
int change = 0; // 자리 교환 횟수
for (a = 1; a < n; a++) {
for (b = a; b > 0; b--) {
compare++;
if (arr[b - 1] < arr[b]) {
temp = arr[b];
arr[b] = arr[b - 1];
arr[b - 1] = temp;
change++;
}
else {
break;
}
}
for (c = 0; c < n; c++) {
printf(" %3d", arr[c]);
}
printf(" | 비교 : %2d , 교환 : %2d\\n", compare, change);
}
return 0;
}
void main() {
int arr[10] = { 5 ,23, 84, 49, 71, 15, 99, 62, 36, 7 };
printf("\\n === Insert Sort Start === \\n\\n");
InsertSort(arr, 10);
printf("\\n === Insert Sort End === \\n\\n");
}
<aside> ➡️ === Insert Sort Start ===
23 5 84 49 71 15 99 62 36 7 | 비교 : 1 , 교환 : 1 84 23 5 49 71 15 99 62 36 7 | 비교 : 3 , 교환 : 3 84 49 23 5 71 15 99 62 36 7 | 비교 : 6 , 교환 : 5 84 71 49 23 5 15 99 62 36 7 | 비교 : 10 , 교환 : 8 84 71 49 23 15 5 99 62 36 7 | 비교 : 12 , 교환 : 9 99 84 71 49 23 15 5 62 36 7 | 비교 : 18 , 교환 : 15 99 84 71 62 49 23 15 5 36 7 | 비교 : 23 , 교환 : 19 99 84 71 62 49 36 23 15 5 7 | 비교 : 27 , 교환 : 22 99 84 71 62 49 36 23 15 7 5 | 비교 : 29 , 교환 : 23
=== Insert Sort End ===
</aside>
#include <stdio.h>
int SelectionSort(int arr[10], int n) {
int a = 0, b = 0, c = 0; // 순환문 사용 변수
int temp = 0; // 임시 변수
int index = 0;
int compare = 0; // 비교 횟수
int change = 0; // 자리 교환 횟수
for (a = 0; a < n-1; a++) {
index = a;
for (b = a+1; b < n; b++) {
if (arr[index] < arr[b]) {
index = b;
}
compare++;
}
temp = arr[a];
arr[a] = arr[index];
arr[index] = temp;
change++;
for (c = 0; c < n; c++) {
printf(" %3d", arr[c]);
}
printf(" | 비교 : %2d , 교환 : %2d\\n", compare, change);
}
return 0;
}
void main() {
int arr[10] = { 50 ,23, 84, 49, 71, 15, 99, 62, 36, 7 };
printf("\\n === Selection Sort Start === \\n\\n");
SelectionSort(arr, 10);
printf("\\n === Selection Sort End === \\n\\n");
}
<aside> ➡️ === Selection Sort Start ===
99 23 84 49 71 15 50 62 36 7 | 비교 : 9 , 교환 : 1 99 84 23 49 71 15 50 62 36 7 | 비교 : 17 , 교환 : 2 99 84 71 49 23 15 50 62 36 7 | 비교 : 24 , 교환 : 3 99 84 71 62 23 15 50 49 36 7 | 비교 : 30 , 교환 : 4 99 84 71 62 50 15 23 49 36 7 | 비교 : 35 , 교환 : 5 99 84 71 62 50 49 23 15 36 7 | 비교 : 39 , 교환 : 6 99 84 71 62 50 49 36 15 23 7 | 비교 : 42 , 교환 : 7 99 84 71 62 50 49 36 23 15 7 | 비교 : 44 , 교환 : 8 99 84 71 62 50 49 36 23 15 7 | 비교 : 45 , 교환 : 9
=== Selection Sort End ===
</aside>