fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int arr[] = {2, 3, 4, 1, 9, 5, 1, 2, 6, 8, 1, 3};
  6. int arrLength = 12;
  7. for (int i = 0; i < arrLength; i++)
  8. {
  9. int minPos = -1;
  10. for (int j = i; j < arrLength; j++)
  11. // either it's the first element, or it's greater than the last element
  12. // and either it's the first such element we find, or smaller than the best one
  13. if ((i == 0 || arr[j] > arr[i-1]) &&
  14. (minPos == -1 || arr[j] < arr[minPos]))
  15. {
  16. minPos = j;
  17. }
  18.  
  19. // no more elements to sort
  20. if (minPos == -1)
  21. break;
  22.  
  23. int temp = arr[i];
  24. arr[i] = arr[minPos];
  25. arr[minPos] = temp;
  26. }
  27. for (int i = 0; i < arrLength; i++)
  28. cout << arr[i] << " ";
  29. return 0;
  30. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
1 2 3 4 5 6 8 9 2 1 1 3