Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 데이터 이해
- 쿼리문
- watchman error
- ADsP
- expo-cli error
- 선택정렬
- sockettimeout
- kotlin recyclerview checkbox error
- retrofit
- MySQL
- GitHub
- h2 error
- kotlin checkbox error
- Kotlin
- 자료구조
- watchmanresponse
Archives
- Today
- Total
Stand up lee
선택 정렬 본문
선택 정렬
선택 정렬이란 가장 작은 것을 선택해서 앞으로 보내는 정렬 기법이다. 가장 작은 것을 선택하는 데 N번, 앞으로 보내는 데 N번의 연산으로 O(N²)의 시간 복잡도를 가진다.
2 | 4 | 3 | 1 | 9 | 6 | 7 | 8 | 10 | 5 |
1 | 4 | 3 | 2 | 9 | 6 | 7 | 8 | 10 | 5 |
1 | 2 | 3 | 4 | 9 | 6 | 7 | 8 | 10 | 5 |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
한번 정렬할 때마다 발견하는 최솟값을 앞으로 보내고, 최솟값을 넣은 index 부터 시작하여 다시 최솟값을 탐색하여 앞으로 보내는 과정을 반복한다.
#include <stdio.h>
#include <limits.h>
#define SIZE 1000
int a[SIZE];
void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
int main(){
int n, min, index; // min : 한번 정렬할때마다 찾는 최소값
printf("정렬할 숫자 갯수를 입력해주세요\n");
scanf("%d", &n);
printf("정렬할 숫자 배열을 입력해주세요\n");
for(int i = 0; i < n; i++){
scanf("%d", &a[i]);
}
for(int i = 0; i < n; i++){
min = INT_MAX;
for(int j = i; j < n; j++){
if(min > a[j]){
min = a[j];
index = j;
}
}
swap(&a[i], &a[index]);
}
// 정렬된 숫자 배열 프린트
for(int i = 0; i < n; i++){
printf("%d ", a[i]);
}
}
'자료구조&알고리즘' 카테고리의 다른 글
병합 정렬(merge sort) (0) | 2021.09.29 |
---|---|
삽입 정렬 (insertion sort) (0) | 2021.09.28 |