fork download
  1. #include <stdio.h>
  2. typedef int(*sort_func)(int, int);
  3.  
  4. void bubble_sort(int arr[], int len,sort_func test)
  5. {
  6. int temp;
  7. for (int i = 0; i < len-1; i++)
  8. {
  9. for (int j = 0; j < (len - i) - 1; j++)
  10. {
  11. if (test(arr[j], arr[j+1]))
  12. {
  13. temp = arr[j];
  14. arr[j] = arr[j + 1];
  15. arr[j + 1] = temp;
  16. }
  17. }
  18. }
  19. }
  20.  
  21. int case1(int n1, int n2)
  22. {
  23. if (n1 > n2)
  24. return 1;
  25. else
  26. return 0;
  27. }
  28. int case2(int n1, int n2)
  29. {
  30. if (n1 < n2)
  31. return 1;
  32. else
  33. return 0;
  34. }
  35.  
  36.  
  37. int main()
  38. {
  39. int arr1[10] = { 3,5,1,2,5,8,3,2,5,7 };
  40. bubble_sort(arr1, sizeof(arr1) / sizeof(int),case1);
  41. for (int i = 0; i < sizeof(arr1) / sizeof(int); i++)
  42. {
  43. printf("%d ", arr1[i]);
  44. }
  45.  
  46. return 0;
  47. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
1 2 2 3 3 5 5 5 7 8