fork download
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. const int SIZE = 7;
  7.  
  8. int main()
  9. {
  10. int intArray[SIZE] = {5, 3, 32, -1, 1, 104, 53};
  11. int i, j, k, l, m = 0;
  12. int temp2[SIZE][2];
  13. int indices[SIZE] = {};
  14.  
  15. for(i = 0; i < 7; i++){
  16. temp2[i][0] = intArray[i];
  17. temp2[i][1] = i;
  18. }
  19.  
  20. cout << "Unsorted Array looks like this." << endl;
  21. for (j = 0; j < 7; j++){
  22. cout << temp2[j][0];
  23. cout << " : ";
  24. cout << temp2[j][1] << endl;
  25. }
  26.  
  27. for(k = 0; k < SIZE; k++)
  28. {
  29. for(j = 0; j < SIZE-1-k; j++)
  30. {
  31. if(temp2[j+1][0] < temp2[j][0])
  32. {
  33. l = temp2[j][0];
  34. temp2[j][0] = temp2[j+1][0];
  35. temp2[j+1][0] = l;
  36. l = temp2[j][1];
  37. temp2[j][1] = temp2[j+1][1];
  38. temp2[j+1][1] = l;
  39. }
  40. }
  41. }
  42.  
  43. cout << "Sorted Array looks like this." << endl;
  44. for (m = 0; m < SIZE; m++)
  45. {
  46. cout << temp2[m][0];
  47. cout << " : ";
  48. cout << temp2[m][1] << endl;
  49. }
  50.  
  51. for(i = 0; i < SIZE; i++){
  52. indices[i] = temp2[i][1];
  53. }
  54.  
  55. cout << "Indices of Sorted Array look like this." << endl;
  56. for(i = 0; i < SIZE; i++){
  57. cout << indices[i] << endl;
  58. }
  59.  
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
Unsorted Array looks like this.
5 : 0
3 : 1
32 : 2
-1 : 3
1 : 4
104 : 5
53 : 6
Sorted Array looks like this.
-1 : 3
1 : 4
3 : 1
5 : 0
32 : 2
53 : 6
104 : 5
Indices of Sorted Array look like this.
3
4
1
0
2
6
5