Advertisement
cd62131

sort

May 23rd, 2019
987
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.51 KB | None | 0 0
  1. #include <stdio.h>
  2. static void print(int *a, int n) {
  3.   for (int i = 0; i < n; ++i) { printf("%s%d", (i == 0 ? "" : " "), a[i]); }
  4.   puts("");
  5. }
  6. static void sort(int *a, int n) {
  7.   for (int i = 0; i < n; ++i) {
  8.     for (int j = i + 1, t; j < n; ++j) {
  9.       if (a[j] < a[i]) { t = a[i], a[i] = a[j], a[j] = t; }
  10.       printf("i = %d\tj = %d\t", i, j), print(a, n);
  11.     }
  12.   }
  13. }
  14. int main(void) {
  15.   int a[] = {7, 4, 1, 3, 5, 2};
  16.   int n = sizeof(a) / sizeof(a[0]);
  17.   print(a, n), puts(""), sort(a, n);
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement