fork download
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int n = 0;
  6. cin >> n;
  7. int* a = new int[n];
  8.  
  9. for (int i = 0; i < n; i++) cin >> a[i];
  10.  
  11. cout << "Indices: ";
  12. for (int i = n-1; i > 0; --i) // У вас требование идти с конца
  13. {
  14.  
  15. int idx = 0, max = a[0];
  16. for (int j = 1; j <= i; ++j) // Ищем максимум
  17. if (max < a[j]) { idx = j; max = a[j]; }
  18.  
  19. // Вывод индекса, обмен
  20. cout << idx << " ";
  21. swap(a[idx], a[i]);
  22. }
  23. cout << "\nSorted array: ";
  24.  
  25. for (int i = 0; i < n; i++) cout << a[i] << " ";
  26. }
  27.  
Success #stdin #stdout 0s 4324KB
stdin
10
3 4 0 8 1 2 7 5 9 6
stdout
Indices: 8 3 6 3 3 1 0 0 1 
Sorted array: 0 1 2 3 4 5 6 7 8 9