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