双向插入排序错误

时间:2018-05-17 12:33:46

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

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

这是一张说明过程的图片。

enter image description here

这里的数组是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.这让我相信某处出现了错误。

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:3)

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

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

所有这一切只是一个标准的插入排序。 (如果我正确理解算法的话。)

将变量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;
        } 
    }
}

答案 1 :(得分:2)

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

希望这有帮助

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 :(得分:0)

这里的问题是你的算法只在交换整数的数组中进行一次传递。由于在第一遍中将93交换到后面,第二次迭代查看[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

相关问题