Radix Sort on Strings数组不正常?

时间:2014-04-13 21:31:21

标签: c++ string sorting radix

我非常接近让这个程序正常工作,但是我正在尝试让这个程序使用基数排序按字母顺序排列一组字符串,并且我有一个问题,想弄清楚如何

void msd(string* sortMe)
{
    aux = new string[numElements];  //Set size of the array used to sort each character.
    msd(sortMe, 0, numElements, 0);
}

void msd(string* sortMe, int l, int r, int d) //Here I use key-indexed     counting :http://algs4.cs.princeton.edu/lectures/51DemoKeyIndexedCounting.pdf
{
    if(r < l || r == l) return; //if we get to 1 left, it is sorted
    int count[256+1]= {0};  //256 because we are using ASCII. + 1 for key-indexed counting
    for(int i =0; i < numElements; i++)
    {
         count[sortMe[i][d] + 1]++;   //For each letter found, you must put it in the ascii code + 1
    }
    for(int k = 1; k < 256; k++)    //Everything but null
    {
         count[k] += count[k-1];     //Get cumulates(everything before added together + what it is)
    }
    for(int i =0; i < numElements; i++)
    {
         aux[count[sortMe[i][d]]++] = sortMe[i]; //using your count array, you must move over elements from sortMe, to the aux array
    }
    for(int i = 0; i < numElements; i++)
    {
         sortMe[i] = aux[i];      //Then you must copy the elements from the aux array back to the main array.
    }
    for(int i = 0; i< numElements; i++)
    {
         cout<< aux[i]<< endl;
    }
    for(int i = 0; i < 255; i++)
    {
         msd(sortMe, l + count[i], l+ count[i+1], d+1); //You must then call recursively. The count array when finished, will give you what indexes(sub arrays to sort) you must sort between. Also, you are moving to the next character in the string(d)
    }
}

第一次迭代非常有效,并按第一个字符对所有字符串进行排序。然而,第二次迭代抛出窗外的所有内容,并按第二个字符对它们进行排序。我该如何解决这个问题呢?

0 个答案:

没有答案
相关问题