반응형
insertion sort
1. 소스
#include <stdio.h> int main() { int n, array[1000], c, d, t; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for (c = 0; c < n; c++) { scanf("%d", &array[c]); } for (c = 1 ; c <= n - 1; c++) { d = c; while ( d > 0 && array[d] < array[d-1]) { t = array[d]; array[d] = array[d-1]; array[d-1] = t; d--; } } printf("Sorted list in ascending order:\n"); for (c = 0; c <= n - 1; c++) { printf("%d\n", array[c]); } return 0; }
2. 결과
hacker@HACKER:~/c$ gcc -o insertion_sort insertion_sort.c hacker@HACKER:~/c$ ./insertion_sort Enter number of elements 5 Enter 5 integers -10 1 -3 20 100 Sorted list in ascending order: -10 -3 1 20 100
반응형
'C 언어' 카테고리의 다른 글
binary search (0) | 2016.07.12 |
---|---|
search (0) | 2016.07.12 |
selection sort (0) | 2016.07.12 |
bubble sort (0) | 2016.07.12 |
substr (0) | 2016.07.12 |