Advertisement
cd62131

sort

Feb 16th, 2014
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #define NUMBER 5
  3. void sort(int *input, int num) {
  4.   int i, j, t;
  5.   for (i = 1; i < num; i++) {
  6.     t = input[i];
  7.     if (input[i - 1] > t) {
  8.       j = i;
  9.       do { input[j] = input[j - 1]; j--; }
  10.       while (j > 0 && input[j - 1] > t);
  11.       input[j] = t;
  12.     }
  13.   }
  14. }
  15. int main(void) {
  16.   int i, input[NUMBER];
  17.   puts("整数配列を入力してください:");
  18.   for (i = 0; i < NUMBER; i++) { printf("input[%d]:", i); scanf("%d", &input[i]); }
  19.   sort(input, NUMBER);
  20.   puts("昇順に並び替えた配列は:");
  21.   for (i = 0; i < NUMBER; i++) printf("input[%d]:%d\n", i, input[i]);
  22.   return 0;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement