我正在编写一个选择排序,给定一个无序元素数组,将使用已排序元素的索引填充一个新数组.例如,
[3, 2, 1]
Run Code Online (Sandbox Code Playgroud)
会回来的
[2, 1, 0] // original indexes of sorted array [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
不幸的是,它错误地填充了数组,重复了相同的索引.
这是我的代码:
void sort(float data[], int indx[], int len) {
int min;
float temp;
float tempData[len];
for (int x = 0; x < len; ++x){
tempData[x] = data[x];
}
for (int i = 0; i < len; ++i) {
min = i;
for (int j = i + 1; j < len; ++j) {
if (tempData[j] < tempData[min]) {
min = j;
} …Run Code Online (Sandbox Code Playgroud)