fork download
  1. #include <vector>
  2. #include <iostream>
  3.  
  4. void Swap(int a[], int i, int j)
  5. {
  6. int temp = a[i];
  7. a[i] = a[j];
  8. a[j] = temp;
  9. }
  10.  
  11. int smallest(int *arr, int size)
  12. {
  13. int small;
  14. if (size == 1)
  15. {
  16. return 0;
  17. }
  18. small = smallest(arr, size - 1);
  19. //go from the last to the first index in the array and then work backwards to find the smallest one all the way back to the beginning
  20. return (arr[size - 1] < arr[small]) ? size - 1 : small;
  21. }
  22.  
  23. void chooseSort(int * arr, int size)
  24. {
  25. if (size <= 1)
  26. {
  27. return;
  28. }
  29. int index = smallest(arr, size);
  30. Swap(arr, index, 0);
  31. chooseSort(arr + 1, size - 1);
  32. }
  33.  
  34. int main() {
  35. int a[] = {8,2,3,1,7,5};
  36. chooseSort(a, 6);
  37. for(auto x:a) std::cout << x << std::endl;
  38. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
1
2
3
5
7
8