程式範例
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 | /** * Program name: insertion_sort.c * Description: 插入排序法 * Created date: 2012/08/28 */ #include <stdio.h> #define n 5 void main(){ int arr[n]={21,3,13,2,8}; int i,t; for(i=1;i<n;i++){ t = arr[i]; while(t < arr[i-1]){ arr[i] = arr[i-1]; i = i-1; } arr[i] = t; } printf("[插入排序結果]: "); for(i=0;i<n;i++) printf("%d,",arr[i]); printf("\n"); system("pause"); } |
沒有留言:
張貼留言