【问题标题】:Two-way insertion sorting error双向插入排序错误
【发布时间】:2018-10-27 17:33:13
【问题描述】:

我正在尝试进行双向插入排序。它应该取数组中的第一个值,然后通过将其与第一个值进行比较来对数组中的以下数字进行排序。如果数字较大,则放在数组中的第一个后面,如果较小,则放在前面。

这是一个说明该过程的图像。

这里的数组是6 5 3 1 8 7 2 4,从上往下读就是排序过程的每一步。它将数字 6 与其余数字进行比较,然后相应地放置它们。

到目前为止,我有这个代码:

void twowaysort(int n, int a[])
{
    int j;
    int first = a[0];
    for (int i = 1; i < n; i++) {
        if (a[i] > first) {
            j = i + 1;
            while (j <= n - 1 && a[j] < a[j - 1]) {
                swap(a[j - 1], a[j]);
                j = j + 1;
            }
        }
        if (a[i] < first) {
            j = i - 1;
            while (j >= 0 && a[j] > a[j + 1]) {
                swap(a[j + 1], a[j]);
                j = j - 1;
            }
        }
    }
}

虽然这适用于上面的数组,但似乎无法对以下内容进行排序:13 93 58 33 58 63 11 41 87 32。这让我相信某处有错误。

任何帮助将不胜感激。

【问题讨论】:

  • 我的建议是使用带有调试器(如 Visual Studio)的 IDE,并单步执行查看 a 数组(在监视窗口中使用 a,10)和 a[j] 的代码, a[j-1] 和 a[j+1] 在您踏步时显示在监视窗口中。我在 VS 2013 中完成了此操作,发现 58 和 33 存在问题,但我并不完全理解算法,因此无法提出修复建议。
  • 这个算法很难遵循。我首先将ij 重命名为有意义的名称。然后添加一些 cmets,这样我们就可以按照那里发生的逻辑进行操作了。
  • 感谢您的建议,我已经尝试调试并观察值如何随着循环的进行而变化,但仍然无法弄清楚。如果有帮助,我会尝试将 cmets 添加到代码中。
  • 算法的解释需要更加具体。向左推和向右推是什么意思?您的for 循环正在从左到右扫描您只能向下交换到左侧。
  • 数组中左右没有意义。上例中 6 的索引是:0, 1, 2, 3, 3, 3, 4, 5。这是增加而不是停留在同一位置。

标签: c++ algorithm sorting insertion-sort two-way


【解决方案1】:

我用一个向量实现了这个,我保存了起始数字的位置,然后插入左边是数字较低或右边如果数字更大。然后数字向左或向右移动,直到它们更大或更小。

希望对你有帮助

int poz = 0; //starting value position
vector<int> b;    
b.push_back(a[0]);//first value

for (int i = 1; i < n; i++)
{
    if (a[i] > prad)    //if greater
    {
        vector<int>::iterator it = b.begin() + poz;    //position of starting element
        it = b.insert(it + 1, a[i]); //insertion to the right
        int t = poz + 1;    //position of the inserted element
        while (t + 1 < b.size() && b[t] > b[t + 1])    
        {
            swap(b[t], b[t + 1]);   
            t++;                    //we go right until our number is greater
        }
    }
    else  //same here but instead of going right we go left until the value is lower
    {
        vector<int>::iterator it = b.begin() + poz;
        it = b.insert(it, a[i]);
        poz++; 
        int t = poz - 1;
        while (t > 0 && b[t] < b[t - 1])
        {
            swap(b[t], b[t - 1]);
            t--;                
        }
    }
}

【讨论】:

  • 说的有道理,没想到用向量来实现这样一个看似简单的算法,但它确实有效!非常感谢。
【解决方案2】:

我注意到的第一件事是即使有一个选定的值,也没有相应的选定索引。所以必须添加和使用。

第二件事是选择的值只是一个边界。每次当前排序的值必须冒泡时,它都会下降。

所以总而言之,这只是一个标准的插入排序。 (如果我正确理解了算法。)

将变量 ij 重命名为 to_sort_idxlook_at_idx

void twowaysort( int a_size, int a[] )
{
    if ( a_size < 2 )
        return;

    int selected_idx   = 0;
    int selected_value = a[ selected_idx ];

    for ( int to_sort_idx = 1; to_sort_idx < a_size; to_sort_idx++ )
    {
        if ( a[ to_sort_idx ] > selected_value )
        {
            int look_at_idx = to_sort_idx;

            while ( look_at_idx > selected_idx && a[ look_at_idx ] < a[ look_at_idx - 1] )
            {              
                std::swap( a[ look_at_idx -1 ], a[ look_at_idx  ] );
                --look_at_idx;
            }
        }
        else //if ( a[ to_sort_idx ] <= selected_value )
        {
            int look_at_idx = to_sort_idx - 1;

            while ( look_at_idx >= 0 && a[ look_at_idx ] > a[ look_at_idx + 1 ] )
            {
                std::swap( a[ look_at_idx ], a[ look_at_idx + 1] );
                --look_at_idx;
            }

            ++selected_idx;
        } 
    }
}

【讨论】:

  • @execut4ble 这修复了您的代码,但请验证算法。在我看来,这实现了插入排序。不是双向的。
  • 我会收回它:你是对的,这与简单的插入排序相同,而不是该算法如何描述排序。
【解决方案3】:

这里的问题是您的算法只在交换整数的数组上进行一次传递。由于在第一次通过时将 93 交换到后面,第二次迭代查看 a[2] 现在是 33(而不是 58)。所以你基本上跳过了处理 58。这个算法只对数组进行部分排序。您必须在这里多次通过才能获得您想要的...

void twowaysort(int n, int a[])
{
    int j;
    int first;

    for (int k = 0; k < n; ++k)
    {
        first = a[0];
        for (int i = 1; i < n; i++) 
        {
            if (a[i] > first) 
            {
                j = i + 1;
                while (j <= n - 1 && a[j] < a[j - 1]) 
                {
                    swap(a[j - 1], a[j]);
                    j = j + 1;
                }
            }
            if (a[i] < first) 
            {
                j = i - 1;
                while (j >= 0 && a[j] > a[j + 1]) 
                {
                    swap(a[j + 1], a[j]);
                    j = j - 1;
                }
            }
        }
    }

}

输出:11 13 32 33 41 58 58 63 87 93

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多