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