fork download
  1.  
  2.  
  3. #include <stdio.h>
  4.  
  5. int cmp(int x, int y) // compare x with y
  6. {
  7. return x < y; // 1 if x < y, 0 otherwise
  8. }
  9.  
  10. void swap(int *x, int *y)
  11. {
  12. int tmp = *x;
  13.  
  14. *x = *y;
  15. *y = tmp;
  16. }
  17.  
  18. int *select(int p[], size_t len)
  19. {
  20. int *pos = p; // pointer to the highest-precedence element
  21.  
  22. for (size_t i = 0; i < len; ++i) {
  23. if (cmp(p[i], *pos)) {
  24. pos = &p[i];
  25. }
  26. }
  27.  
  28. return pos;
  29. }
  30.  
  31. void disp(int p[], size_t len)
  32. {
  33. for (size_t i = 0; i < len; ++i) {
  34. printf("%d ", p[i]);
  35. }
  36. printf("\n");
  37. }
  38.  
  39. int main(void)
  40. {
  41. int a[] = {3, 1, 4, 1, 5, 9, 2, 6, 5};
  42. size_t n = sizeof a / sizeof a[0];
  43.  
  44. for (size_t i = 0; i < n - 1; ++i) {
  45.  
  46. int *q = select(&a[i+1], n-i-1);
  47.  
  48. if (cmp(*q, a[i])) {
  49. swap(&a[i], q);
  50. }
  51.  
  52. }
  53.  
  54. disp(a, n);
  55.  
  56. return 0;
  57. }
  58.  
Success #stdin #stdout 0s 544KB
stdin
Standard input is empty
stdout
1 1 2 3 4 5 5 6 9