Given an array arr = {5, 16, 4, 7}, we can sort it through sort(arr, arr+sizeof(arr)/sizeof(arr[0])).
so now the array arr = {4, 5, 7, 16}
Well in c++ we can use pair datatype to do this easily. Sample code below;
arr = {5, 16, 4, 7};
vector >V;
for(int i=0;i<4;i++){
pairP=make_pair(arr[i],i);
V.push_back(P);
}
sort(V.begin(),V.end());
So V[i].first is the ith value and V[i].second is the ith index. So to print the index in the sorted array.
for(int i=0;i<4;i++)cout<
Note that while sorting an array (or vector) of pair items, array is first sorted based on the first values. If two pair have same first value then they are sorted based on their second value.