【问题标题】:How to put the sorted indexes of an unsorted array into a new array如何将未排序数组的排序索引放入新数组
【发布时间】:2016-06-03 08:52:39
【问题描述】:

如果我有一个数组,例如array = {7, 4, 5, 8, 22},并且我想找到数组的排序索引,例如sortedIndicies = {2, 0, 1, 3, 4},我该怎么做呢?我对编码还很陌生,我一直在尝试只使用 for 循环和 if 语句。我尝试创建原始数组的副本,对新数组进行冒泡排序,然后将新排序数组中的值与原始数组进行比较并使用这些索引,但如果代码在数组中有多个相同元素,我会遇到问题.有什么建议? (以我能更好理解的方式使用 fors 和 ifs。)提前致谢。

【问题讨论】:

  • 值得看看你在这方面的尝试。
  • 如下所述,排序后的索引是 S[] = {1,2,0,3,4}。一个简单的用法是 sorted_array[i] = array[S[i]]。如问题中所述,排名将是 R[] = {2,0,1,3,4}。一个简单的用法是 sorted_array[R[i]] = array[i]。您可以使用 R[S[i]] = i 将 S[] 转换为 R[]。

标签: c++ arrays list sorting indexing


【解决方案1】:

可能的解决方案是创建一个 {value, index} 对数组,按值对它们进行排序(使用 std::pair,它已经有适当的 operator <)并从排序后的数组中读取索引。应该很容易写。

另一种解决方案是创建一个索引数组,并使用第一个数组中的值对它们进行排序。在 c++11 中它可能看起来像这样:

template<class T>
std::vector<size_t> get_sorted_indexes(std::vector<T> const &array)
{
    std::vector<size_t> indexes(array.size());
    std::iota(indexes.begin(), indexes.end(), 0);
    std::sort(indexes.begin(), indexes.end(), [&array](size_t l_idx, size_t r_idx)
    {
        return array[l_idx] < array[r_idx];
    });
    return indexes;
}

iota 用于填充索引向量。索引与原始数组中的值进行比较 [l_idx] &lt; array[r_idx]

是的。这不是“fors and ifs”解决方案。但你不需要它们。只需按照您需要的方式使用sort 函数

【讨论】:

    【解决方案2】:

    首先排序的索引是{1,2,0,3,4}

    仅使用 for 并且如果您可以这样做

    #include <iostream>
    using namespace std;
    
    int main() {
        int arraySize = 5;
    
        int arr[arraySize] = { 7, 4, 5, 8, 22 };
    
        int arrtemp[arraySize] = { 0 };
    
        int temp;
    
        int sortedIndices[5];
    
        for (int i = 0; i < arraySize; i++) {
            arrtemp[i] = arr[i];               //making a copy of the original array
        }
    
        //Bubble sorting
        //Sorting the copied array
        for (int i = 0; i < arraySize; i++) {
            for (int j = 0; j < arraySize - 1; j++) {
                if (arrtemp[j] > arrtemp[i]) {
                    //swap them
                    temp = arrtemp[i];
                    arrtemp[i] = arrtemp[j];
                    arrtemp[j] = temp;
                }
            }
        }
    
        //tracking the location of the sorted values
        for (int i = 0; i < arraySize; i++)
            for (int j = 0; j < arraySize; j++) {
                if (arrtemp[i] == arr[j]) {
                    sortedIndices[i] = j;
                }
            }
    
        cout << "Original Array\n";
        for (int i = 0; i < arraySize; i++) {
            cout << arr[i] << " ";
        }
        cout << endl;
    
        cout << "Sorted array\n";
        for (int i = 0; i < arraySize; i++) {
            cout << arrtemp[i] << " ";
        }
        cout << endl;
    
        cout<<"Sorted indices are\n";
        for (int i = 0; i < arraySize; i++) {
            cout << sortedIndices[i] << " ";
        }
    }
    

    输出:

    原始数组

    7 4 5 8 22

    排序数组

    4 5 7 8 22

    排序后的索引是

    1 2 0 3 4

    【讨论】:

    • 我希望它会有所帮助。
    • 你真的不需要复制原始数组。您可以直接对索引数组进行排序。 Here is your code with all the extra wasteful work removed.
    • 感谢您的帮助!我之前尝试过,但是如果int arr[arraySize]= { 7, 4, 5, 8, 22 }; 中有多个相同的值,我遇到了问题,例如,int arr[arraySize]= { 7, 4, 4, 8, 22 }; 会导致逻辑错误。
    • @DonnieB:或者您可以直接对索引数组进行排序,而忘记在排序后尝试恢复索引的错误步骤。点击我之前评论中的文字链接。
    • 谢谢。 @Blastfurnace
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-03
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    • 2014-11-06
    • 1970-01-01
    相关资源
    最近更新 更多