-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionSort.c
More file actions
43 lines (38 loc) · 1.15 KB
/
selectionSort.c
File metadata and controls
43 lines (38 loc) · 1.15 KB
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 <stdio.h>
/** 선택 정렬 */
void SelectionSort(int A[], int n) {
int minIdx; // 최소 인덱스
for (int i = 0; i < n; i++) {
minIdx = i; // 현재 포지션을 최소 인덱스로
for (int j = minIdx + 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;
}
/** 단계별 출력 */
for (int k = 0; k < n; k++) {
if (k == i) {
printf("[%d]%c", A[k], (k == n-1)?'\n':'\t');
continue;
}
if (k == minIdx) {
printf("[%d]%c", A[k], (k == n-1)?'\n':'\t');
continue;
}
printf("%d%c", A[k], (k == n-1)?'\n':'\t');
}
}
}
void main() {
int A[] = {30, 20, 40, 10, 5, 10, 30, 15};
int n = 8;
SelectionSort(A, n);
for (int i = 0; i < n; i++) {
printf("%d ", A[i]);
}
}