解决这个排序挑战的算法是什么? [关闭]

问题描述 投票:0回答:0

The original image version of the challenge has issues

以下重申了已解决问题的挑战:

a) 数组 A[] 包含 n 个苹果大小,其中 A[i] 是苹果的唯一大小。没有两个苹果是一样大的。创建索引 0 到 n-1 的数组 I[],然后根据 A[] 使用比较函数对 I[] 进行归并排序:CompareApples(i,j),如果 A[i] < A[j] or 1 if A[i] > A,则返回 -1 [j].在 I[] 排序后 {A[I[0]] < A[I[1]] < A[I[2]] < ... < A[I[n-1]]}. The only access to A[] is via the compare function CompareApples().

b) 数组 B[] 包含 n 个盒子大小,其中盒子 B[j] 的唯一大小对应于苹果 A[i] 的唯一大小。创建索引 0 到 n-1 的数组 J[],然后根据 B[] 对 J[] 进行排序,使用函数 CompareFit(i, j),如果 A[i] < B[j], 0 if A[i] == B[j], or 1 if A[i] > B[j,则返回 -1 ].对 A[] 或 B[] 的唯一访问是通过比较函数 CompareFit()。在 J[] 排序后,然后 {B[J[0]] < B[J[1]] < B[J[2]] < ... < B[J[n-1]]}. The challenge is to create a function to sort J[] according to B[] using CompareFit(), with the goal of having time complexity O(n log(n)).

对于a)b),额外的数组是允许的,例如归并排序的工作数组。

algorithm sorting mergesort
© www.soinside.com 2019 - 2024. All rights reserved.