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
#include <stdio.h> #include <stdlib.h> #define N 10 void show_data(int *data, int n) { for (int i = 0; i < n; i++) printf("%5d", data[i]); printf("\n"); } void sort_data(int *data, int n) { for (int i = 1; i < n; i++) { for (int j = i; j > 0 && (data[j] < data[j - 1]); j--) { int temp = data[j]; data[j] = data[j - 1]; data[j - 1] = temp; } } } void main() { int data[N]; for (int i = 0; i < N; i++) data[i] = rand() % 100; show_data(data, N); sort_data(data, N); show_data(data, N); }