fork download
  1. #include <stdio.h>
  2.  
  3. int cmp(int x, int 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 *select(int p[], size_t len)
  16. {
  17. int *pos = p;
  18.  
  19. for (size_t i = 0; i < len; ++i) {
  20. if (cmp(p[i], *pos)) {
  21. pos = &p[i];
  22. }
  23. }
  24.  
  25. return pos;
  26. }
  27.  
  28. void disp(int p[], size_t len)
  29. {
  30. for (size_t i = 0; i < len; ++i) {
  31. printf("%d ", p[i]);
  32. }
  33. printf("\n");
  34. }
  35.  
  36. int main(void)
  37. {
  38. int a[] = {3, 1, 4, 1, 5, 9, 2, 6, 5};
  39. size_t n = sizeof a / sizeof a[0];
  40.  
  41. for (size_t i = 0; i < n - 1; ++i) {
  42. int *q = select(&a[i+1], n-i-1);
  43.  
  44. if (cmp(*q, a[i])) {
  45. swap(&a[i], q);
  46. }
  47. }
  48.  
  49. disp(a, n);
  50.  
  51. return 0;
  52. }
Success #stdin #stdout 0s 544KB
stdin
Standard input is empty
stdout
1 1 2 3 4 5 5 6 9