1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <stdio.h> void swap(int *a, int *b) { int tmp; tmp = *a; *a = *b; *b = tmp; } int main(void) { int x = 4, y = 5; printf("x=%d, y=%d \n", x, y); swap(&x, &y); printf("x=%d, y=%d \n", x, y); } | cs |
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 | #include <stdio.h> #define N_DATA 8 void swap(int data[], int i, int j) { int tmp; tmp = data[i]; data[i] = data[j]; data[j] = tmp; } void print_data(int list[], int n) { int i; for (i = 0; i < n; i++) { printf("%d ", list[i]); } printf("\n"); } void selection_sort(int data[], int n) { int i, j, least; for (i = 0; i < n-1; i++) { least = i; for (j = i + 1; j < n; j++) { if (data[j] < data[least]) least = j; } swap(data, i, least); } } int main(void) { int data[N_DATA] = { 12, 23, 98, 45, 11, 92, 56, 79 }; printf("Á¤·Ä Àü:"); print_data(data, N_DATA); selection_sort(data, N_DATA); printf("Á¤·Ä ÈÄ:"); print_data(data, N_DATA); return 0; } | cs |