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