프로그래밍 공부
Insertion Sort 본문
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
|
#include <iostream>
using namespace std;
void insertion_sort(int *arr, int n){
int i,j;
for(i = 1; i < n; i++){ // 배열의 두번째 원소부터 시작함
int tmp = arr[i];
for(j = i; j > 0 && arr[j - 1] > tmp; j--) {// j가 0보다 크고, arr[j - 1] 값이 tmp보다 클 때 반복함
arr[j] = arr[j-1];
}
arr[j] = tmp;
}
}
int main() {
int arr[5] = {3,2,5,9,1};
for(auto i : arr){
cout << i << ' ';
}
cout << '\n';
insertion_sort(arr,5);
for(auto i : arr){
cout << i << ' ';
}
return 0;
}
|
cs |
'CS > Data Struct' 카테고리의 다른 글
Linear Search/선형 검색 (0) | 2021.07.12 |
---|---|
Binary Search Tree (수정예정) (0) | 2021.06.24 |
Selection Sort (0) | 2020.10.21 |
Bubble Sort (0) | 2020.10.20 |
Run Length (0) | 2020.09.25 |
Comments