알고리즘
[정렬] 삽입 정렬
parkit
2018. 9. 11. 01:03
728x90
반응형
오름차순
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 | #include <iostream> #include <queue> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> using namespace std; int num[1000 + 1] = { 0, }; int Size = 0; void insertSort() { for (int i = 1; i < Size; i++) { int temp = num[i]; for (int j = i; j >= 1; j--) { if (num[j - 1] >= temp) // temp가 더 작다면 { num[j] = num[j - 1]; if (j == 1) { num[j - 1] = temp; break; } } else // temp가 더 크다면 { num[j] = temp; break; } } } } int main(void) { scanf("%d", &Size); for (int i = 0; i < Size; i++) { scanf("%d", &num[i]); } insertSort(); for (int i = 0; i < Size; i++) { printf("%d\n", num[i]); } return 0; } | cs |
728x90
반응형