fork(1) download
  1. #include <stdio.h>
  2.  
  3. int cmp(int x, int y) // compare x and y
  4. {
  5. return x > y;
  6. }
  7.  
  8. void swap(int *x, int *y)
  9. {
  10. int tmp = *x;
  11. *x = *y;
  12. *y = tmp;
  13. }
  14.  
  15. int main(void)
  16. {
  17. int a[] = {3, 1, 4, 1, 5, 9, 2, 6, 5};
  18. size_t n = sizeof a / sizeof a[0];
  19.  
  20. for (size_t i = 0; i < n - 1; ++i) {
  21. int count = 0;
  22.  
  23. for (size_t j = 0; j < n - 1 - i; ++j) {
  24. if (cmp(a[j], a[j + 1])) {
  25. swap(&a[j], &a[j + 1]);
  26. count = 1;
  27. }
  28. }
  29.  
  30. if (count == 0) {
  31. break;
  32. }
  33. }
  34.  
  35. for (size_t i = 0; i < n; ++i) {
  36. printf("%d ", a[i]);
  37. }
  38.  
  39. printf("\n");
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0s 5336KB
stdin
Standard input is empty
stdout
1 1 2 3 4 5 5 6 9