fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int a[5] = {4, 1, 2, 3, 5};
  6. int* classorder[5];
  7.  
  8. // Initialize classorder with addresses of elements in a
  9. for (int i = 0; i < 5; i++) {
  10. classorder[i] = &a[i];
  11. }
  12.  
  13. // Sort the pointers based on the values they point to
  14. for (int i = 0; i < 5; i++) {
  15. for (int j = 0; j < i; j++) {
  16. if (*classorder[i] > *classorder[j]) {
  17. // Swap pointers to reorder
  18. int* temp = classorder[i];
  19. classorder[i] = classorder[j];
  20. classorder[j] = temp;
  21. }
  22. }
  23. }
  24.  
  25. // Output sorted values
  26. for (int i = 0; i < 5; i++) {
  27. cout << classorder[i]-&a[0] << " ";
  28. }
  29.  
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
4 0 3 2 1