这个题目不太好,因为数据的数量太小了,才10个。即便想出一个高效的算法,可能也体现不出效率来。
a. 已知:
5 3 4 7 3 5 6 9 21 100
b. 添上序号
5 3 4 7 3 5 6 9 21 100
1 2 3 4 5 6 7 8 9 10
c. 根据第一行排序,得
3 3 4 5 5 6 7 9 21 100
2 5 3 1 6 7 4 8 9 10
d. 添上序号,得
3 3 4 5 5 6 7 9 21 100 // 这一行其实没用了
2 5 3 1 6 7 4 8 9 10
1 1 2 3 3 4 5 6 7 8
e. 根据d中第二行排序,得
5 3 4 7 3 5 6 9 21 100 // 这一行其实就是a
1 2 3 4 5 6 7 8 9 10
3 1 2 5 2 3 4 6 7 8
f. 输出e中的第一行和第三行
假如我用
C++来做的话,代码应该是

程序代码:
#include <cstdio>
#include <utility>
#include <algorithm>
using namespace std;
void foo( const int (&a)[10] )
{
pair<int,int> b[] = { {a[0],1}, {a[1],2}, {a[2],3}, {a[3],4}, {a[4],5}, {a[5],6}, {a[6],7}, {a[7],8}, {a[8],9}, {a[9],10} };
sort( begin(b), end(b), [](auto& lhs,auto& rhs){return lhs.first<rhs.first;} );
int index=1, pre=b[0].first;
for( auto& value : b )
{
if( value.first != pre )
{
pre = value.first;
++index;
}
value.first = index;
}
sort( begin(b), end(b), [](auto& lhs,auto& rhs){return lhs.second<rhs.second;} );
for( size_t i=0; i!=size(a); ++i )
printf( "%d%c", a[i], " \n"[i+1==size(a)] );
for( size_t i=0; i!=size(b); ++i )
printf( "%d%c", b[i].first, " \n"[i+1==size(b)] );
}
int main( void )
{
int a[] = { 5, 3, 4, 7, 3, 5, 6, 9, 21, 100 };
foo( a );
}