快速排序无限循环数字重建

时间:2015-05-15 18:44:35

标签: c++ c sorting infinite-loop

我正在尝试将一些代码从MATLAB导出到MEX文件(a.la C)。对于我想要导出的代码段,我需要实现一个可以对2D数组进行排序的算法。打开数字配方的副本,我找到基于索引的排序。我已经用零索引实现了这个函数,以简单的方式,我稍后将简化。目前的问题是,当我运行我的函数时,它会在传递某些随机数集时进入无限循环。例如集:

0.8147,
0.9058,
0.127,
0.9134,
0.6324,
0.0975,
0.2785,
0.5469,
0.9575,
0.9649,
0.1576,
0.9706,
0.9572,
0.4854,
0.8003,
0.1419,
0.4218,
0.9157,
0.7922,
0.9595

这些数字都没有重复。下面是我的C代码中的数字接收版本。关于什么是错的任何建议?我已经花了3个小时。该算法为表现良好的随机数集提供了适当的索引。

#define SWAP(a,b) itemp=(a);(a)=(b);(b)=itemp;
#define M 7
#define NSTACK 50

long* IndexSort(unsigned long int vectorLength, double* column)
{

unsigned long i, indxt, ir = vectorLength, itemp,j, k, l = 1;
int jstack = 0, *istack;
long *indx;
double a;

istack = (int*)malloc(NSTACK*sizeof(int));
//initalize output
indx = (long*)malloc(vectorLength*sizeof(long));

for (j = 0; j < vectorLength; j++)
{
    indx[j] = j;

}

// 
while (true)
{
    if (ir - l < M)
    {   
        for (j = l+1; j <= ir;j++)
        {
            indxt = indx[j-1];
            a = column[indxt];
            for (i = j-1; i >= 1; i--)
            {
                if (column[indx[i - 1]] <= a)
                {
                    break;
                }
                indx[i + 1 - 1] = indx[i - 1];
            }
            indx[i + 1 - 1] = indxt;
        }
        if (jstack == 0)
        {
            break;
        }
        ir = istack[jstack--];
        l = istack[jstack--];
    }
    else
    {   

        k = (l + ir) >> 1;

        SWAP(indx[k - 1], indx[l + 1 - 1])

        if (column[indx[l + 1 - 1]] > column[indx[ir - 1]]){
            SWAP(indx[l + 1 - 1], indx[ir - 1])
        }

        if (column[indx[l - 1]] > column[indx[ir - 1]]){
            SWAP(indx[l - 1], indx[ir - 1])
        }

        if (column[indx[l + 1 - 1]] > column[indx[l - 1]]){
            SWAP(indx[l + 1 - 1], indx[l - 1])
        }

        i = l + 1;
        j = ir;
        indxt = indx[l - 1];
        a = column[indxt];
        while (true)
        {
            do i++; while (column[indx[i - 1]] < a);
            do j--; while (column[indx[j - 1]] > a);
            if (j < i) break;
            SWAP(indx[i - 1], indx[j - 1])
        }
        indx[l - 1] = indx[j - 1];
        indx[j - 1] = indxt;
        jstack += 2;
        if (jstack > NSTACK) error("NSTACK too small for IndexSort");

        if (ir - i + 1 > j - 1){
            istack[jstack] = ir;
            istack[jstack - 1] = l;
            ir = j - 1;
        }

        else{
            istack[jstack] = j - 1;
            istack[jstack - 1] = l;
            l = i;
        }


    }
}
free(istack);
return indx;
}

编辑1: 我将#defines添加到顶部。 回应第一波评论:

  • 此函数调用生成一系列已排序的索引。我将获取这些值并使用它们对2D数组进行排序。我道歉,我应该更清楚这一点。
  • 至于使用当前不可能的调试器。该函数是从MEX包装器调用的。我会写一个主要功能,以便我可以对此进行调查。

编辑2:

  • 按照建议移动了自由命令仍然没有变化。
  • 更新问题陈述。我在论坛上阅读问题,大多数人都崩溃了。我的问题实际上是一个无限循环,我不知道它在哪里发起。

先谢谢你的帮助,

威尔

1 个答案:

答案 0 :(得分:2)

如果目标是从给定数组中对索引进行排序,则使用C ++ 11,std::sort和lambdas的以下代码应该可以完成这项工作:

C ++ 11示例

#include <algorithm>
#include <iostream>
#include <array>

long* IndexSort(unsigned long int vectorLength, double* column)
{
    long *index = new long[vectorLength](); // I really don't recommend this line
    long n = 0;
    std::generate( index, index + vectorLength, [&] { return n++; });
    std::sort(index, index + vectorLength, [&](long v1, long v2) 
             { return column[v1] < column[v2]; });
    return index;
}

using namespace std;

int main()
{  
  std::array<double,20> testData = 
                        {0.8147,0.9058,0.127,0.9134,0.6324,0.0975,0.2785,
                        0.5469,0.9575,0.9649,0.1576,0.9706,0.9572,0.4854,
                        0.8003,0.1419,0.4218,0.9157,0.7922,0.9595};
  long *indices = IndexSort(testData.size(), &testData[0]);
  for (size_t i = 0; i < testData.size(); ++i )
    cout << testData[indices[i]] << "  has an index of " << indices[i] << "\n";
  delete [] indices;
}

直播示例:http://ideone.com/EsOFKt

请注意,我们所做的只是构建一个已排序的索引数组,并使用传递给lambda的两个索引比较原始的双向量。

请注意,在索引的开头分配内存是不必要的,因为std::vector<long>是执行此操作的常用方法。但是,我试图模仿返回指向动态分配内存的指针的原始代码要求。

C ++ 0x,98示例

如果您使用的是预C ++ 11编译器,则可以将代码更改为:

#include <algorithm>
#include <iostream>

struct IndexSorter
{
   double *m_array;
   IndexSorter(double *oArray) : m_array(oArray) {}
   bool operator()(long v1, long v2) const { return m_array[v1] < m_array[v2]; }
};

long* IndexSort(unsigned long int vectorLength, double* column)
{
    long *index = new long[vectorLength](); // I really don't recommend this line
    for (unsigned long i = 0; i < vectorLength; ++i) 
        index[i] = i;

    std::sort(index, index + vectorLength, IndexSorter(column));
    return index;
}

using namespace std;

int main()
{  
  double testData[] =   {0.8147,0.9058,0.127,0.9134,0.6324,0.0975,0.2785,
                        0.5469,0.9575,0.9649,0.1576,0.9706,0.9572,0.4854,
                        0.8003,0.1419,0.4218,0.9157,0.7922,0.9595};
  unsigned long testSize = sizeof(testData) / sizeof(testData[0]);
  long *indices = IndexSort(testSize, &testData[0]);
  for (unsigned long i = 0; i < testSize; ++i )
    cout << testData[indices[i]] << "  has an index of " << indices[i] << "\n";
  delete [] indices;
}

直播示例:http://ideone.com/nhMwSs

C示例

由于此问题也被标记为C,因此这是C实施:

#include <stdlib.h>
#include <stdio.h>

static double *myArray;

int compareIndices(const void* v1, const void *v2)
{
    long val1 = *(long *)v1;
    long val2 = *(long *)v2;
    if (myArray[val1] < myArray[val2])
        return -1;
    else
    if ( myArray[val1] > myArray[val2])
        return 1;
    return 0;
}

long* IndexSort(unsigned long int vectorLength, double* column)
{
    myArray = column;
    long *index = malloc(vectorLength * sizeof(long));
    if ( index )
    {
        unsigned long i;
        for (i = 0; i < vectorLength; ++i)
            index[i] = i;
        qsort(index, vectorLength, sizeof(long), compareIndices);
        return index;
    }
    return 0;
}

int main()
{
    double testData[] = { 0.8147, 0.9058, 0.127, 0.9134, 0.6324, 0.0975, 0.2785,
        0.5469, 0.9575, 0.9649, 0.1576, 0.9706, 0.9572, 0.4854,
        0.8003, 0.1419, 0.4218, 0.9157, 0.7922, 0.9595 };
    unsigned long numItems = sizeof(testData) / sizeof(testData[0]);
    long *indices = IndexSort(numItems, testData);
    if ( indices )
    {
        unsigned long i;
        for (i = 0; i < numItems; ++i)
            printf("%lf has an index of %ld\n", testData[indices[i]], indices[i]);
        free(indices);
    }
}

直播示例:http://ideone.com/8SZH1m