fork(1) download
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. const int sz = 10;
  7. int arr[sz];
  8.  
  9. void printArray() {
  10. for (int i=0; i<sz; i++) {
  11. cout<<arr[i]<<" ";
  12. }
  13. cout<<endl;
  14. }
  15.  
  16. void printArray(int pos) {
  17. cout<<"Array after "<<pos<<"-th min position changed: \n";
  18. for (int j=0; j<=pos; j++) {
  19. cout<<arr[j]<<" ";
  20. }
  21. cout<<"|";
  22. for (int j=pos+1; j<sz; j++) {
  23. cout<<" "<<arr[j];
  24. }
  25. cout<<endl;
  26. }
  27.  
  28. int main(){
  29. for (int i=0; i<sz; i++) {
  30. arr[i] = rand() % 100;
  31. }
  32.  
  33. cout<<"Original array: \n";
  34. printArray();
  35.  
  36. for (int i=0; i<sz-1; i++) {
  37. int minPosition = i;
  38. for (int j=i+1; j<sz; j++) {
  39. if (arr[minPosition] > arr[j]) {
  40. minPosition = j;
  41. }
  42. }
  43. swap(arr[i], arr[minPosition]);
  44. printArray(i);
  45. }
  46.  
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0.01s 5476KB
stdin
Standard input is empty
stdout
Original array: 
83 86 77 15 93 35 86 92 49 21 
Array after 0-th min position changed: 
15 | 86 77 83 93 35 86 92 49 21
Array after 1-th min position changed: 
15 21 | 77 83 93 35 86 92 49 86
Array after 2-th min position changed: 
15 21 35 | 83 93 77 86 92 49 86
Array after 3-th min position changed: 
15 21 35 49 | 93 77 86 92 83 86
Array after 4-th min position changed: 
15 21 35 49 77 | 93 86 92 83 86
Array after 5-th min position changed: 
15 21 35 49 77 83 | 86 92 93 86
Array after 6-th min position changed: 
15 21 35 49 77 83 86 | 92 93 86
Array after 7-th min position changed: 
15 21 35 49 77 83 86 86 | 93 92
Array after 8-th min position changed: 
15 21 35 49 77 83 86 86 92 | 93