数组排序,并返回排序后的数组对应原数组的下标
用b数组来记录下标位置
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
void sort(int* a, int length, int* b)
{
int i,j, t1, t;
for(j=0; j<length; j++)
for(i=0; i<length-1-j; i++)
if(a[i]<a[i+1])
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
t1=b[i];
b[i]=b[i+1];
b[i+1]=t1;
}
}
int main()
{
int a[] = { 8,6,3,0,4,0,5};
int b[7] = { 0,1,2,3,4,5,6};//用来保存原数组对应的下标以及排序后的下标
sort(a, 7, b);
cout<<"排序后的数组为:"<<endl;
for(int i=0; i<7; i++)
cout<<a[i]<<" ";
cout<<endl;
cout<<"排序后的数组对应的下标:"<<endl;
for(int i=0; i<7; i++)
cout<<b[i]<<" ";
cout<<endl;
return 0;
}
![《数组排序,并返回排序后的数组对应原数组的下标》](https://ddrvcn.oss-cn-hangzhou.aliyuncs.com/2022/09/5425719656145029089.png)