基数排序C ++实现字符串基数、字符串

2023-09-12 21:22:57 作者:走过最长的路就是你的套路

我只需要在C ++语言,适用于字符串基数排序实施

我已经有一个它适用于普通的整数

 矢量<矢量< INT> >块[7];
无效radixSort(INT rsarr [],INT的长度){

    INT指数;
    矢量< int的>帮手;
    矢量<矢量< int的> > helper2;
    对于(INT E = 0; E< 10,E ++){
        helper2.push_back(辅助);
    }
    对(INT R = 0;为r 7; R ++){
    块[R] = helper2;
    }
    对于(INT Y = 0; Y<长度; Y ++){

        指数=(INT)(rsarr [Y])%10;
        块[0] [指数] .push_back((rsarr [Y]));
    }

    对于(INT J = 1; J< 7,J ++)
    {
        为(中间体K = 0; K&小于10; k ++)
        {
            的for(int i = 0; I<块[J-1] [K] .size();我++)
            {
            指数=(int)的(嵌段[J-1] [k]的[I] / POW(10,j)的)%10;
            块[J] [指数] .push_back(块[J-1] [k]的[I]);
            }

        }
    }
    INT Q = 0;
    为(中间体F = 0; F&其中;块[6] [0] .size();˚F++){
        rsarr [Q] =块[6] [0] [F];
        q ++;
    }
    如果(块[6] [1] .size()== 1)
    {
        rsarr [Q] =块[6] [1] [0];
    }
    对于(INT Z = 0; Z 7;; Z ++)
    {
        块[0] .clear();
    }
}
 

解决方案

功能为基数排序。

  //这是排序函数调用radixSort功能。
无效数据结构网络化::排序()
{

  矢量<串GT; tempOneDimWordList;

  tempOneDimWordList =词表;
  WordList.clear();

  radixSort(tempOneDimWordList,(无符号整数)tempOneDimWordList.size(),0);
}


// MSD基数函数定义进行排序的话
// lexicgraphically使用最significat位。
无效数据结构网络:: radixSort(矢量<串GT; tempOneDimWordList,
                  无符号整型oneDimVecSize,无符号​​整型偏移)
{

  如果(偏移== lengthOfMaxWord.length){
    返回;
  }
  矢量<串GT; towDimWordlist [MAX_LENGTH]

  为(unsigned int类型I = 0; I< oneDimVecSize;我++){
    如果(偏移&其中; tempOneDimWordList [I] .size()){
      焦炭C = tempOneDimWordList [I] [偏移]

      如果(C!='\ 0'){
    towDimWordlist [(((unsigned int类型)C))。
      的push_back(tempOneDimWordList [I]);
      }
    }
    其他{
      WordList.push_back(tempOneDimWordList [I]);
    }
  }

  //此循环用于递归地调用函数
  //根据偏移排序的话。
  为(unsigned int类型I = 0;我≤(无符号整数)MAX_LENGTH;我++){
    无符号整型sizeCheck =(无符号整数)towDimWordlist [I] .size();
    如果(sizeCheck→1){
      radixSort(towDimWordlist [I],sizeCheck,偏移+ 1);
    }
    否则,如果(sizeCheck == 1)
      {
    WordList.push_back(towDimWordlist [I] [0]);
      }
  }
 

看一看这里的这个博客,我采写。完整的源$ C ​​$ c和测试输入文件的下载链接都可以有。它的作品真的精分选任意长度的字符串。我有很多的痛苦,而解决这个问题。所以想分享,如果它可以帮助别人。快乐共享。 :)

字符串排序算法 基数排序和三向快排

i just need radix sort implementation in c++ language which works for strings

i already have the one which works for normal integers

vector < vector < int> > blocks[7];
void radixSort(int rsarr[],int length){

    int index;
    vector<int> helper;
    vector< vector<int> > helper2;
    for(int e=0;e<10;e++){
        helper2.push_back(helper);
    }
    for(int r=0;r<7;r++){
    blocks[r]=helper2;
    }
    for(int y=0;y<length;y++){

        index=(int)(rsarr[y])%10;
        blocks[0][index].push_back((rsarr[y])); 
    }

    for(int j=1;j<7;j++)
    {   
        for(int k=0;k<10;k++)
        {
            for(int i=0;i<blocks[j-1][k].size();i++)
            {           
            index=(int)(blocks[j-1][k][i]/pow(10,j))%10;
            blocks[j][index].push_back(blocks[j-1][k][i]);
            }

        }       
    }           
    int q=0;
    for(int f=0;f<blocks[6][0].size();f++){         
        rsarr[q]=   blocks[6][0][f];
        q++;        
    }
    if(blocks[6][1].size()==1)
    {
        rsarr[q]=blocks[6][1][0];   
    }
    for(int z=0;z<7;z++)
    {
        blocks[0].clear();
    }
}

解决方案

Functions for radix sort.

// this is the sort function which call the radixSort Function.
void Datastructure::sort()
{

  vector<string> tempOneDimWordList;

  tempOneDimWordList = WordList;
  WordList.clear();

  radixSort(tempOneDimWordList, (unsigned int)tempOneDimWordList.size(), 0);    
}


// MSD radix function definition to sort words 
//lexicgraphically using most significat bits.
void Datastructure::radixSort(vector<string> tempOneDimWordList, 
                  unsigned int oneDimVecSize,  unsigned int offset)
{

  if(offset == lengthOfMaxWord.length ){
    return;
  }
  vector<string> towDimWordlist [MAX_LENGTH];

  for (unsigned int i = 0; i < oneDimVecSize; i++){
    if(offset < tempOneDimWordList[i].size()){
      char c = tempOneDimWordList[i][offset];

      if (c != '\0'){
    towDimWordlist[(((unsigned int)c) )].
      push_back(tempOneDimWordList[i]);
      }
    }
    else{
      WordList.push_back(tempOneDimWordList[i]);
    }
  }

  // this loop is used to call the function recursively
  // to sort the words according to offset.
  for (unsigned int i = 0; i < (unsigned int)MAX_LENGTH; i++) {
    unsigned int sizeCheck = (unsigned int)towDimWordlist[i].size();
    if (sizeCheck > 1){         
      radixSort(towDimWordlist[i], sizeCheck, offset+1);        
    }
    else if(sizeCheck == 1)
      {
    WordList.push_back(towDimWordlist[i][0]);
      }
  }

Have a look here in this blog that I have written. Download link of the full source code and test input files are available there. It works really fine for sorting strings of arbitrary length. I had lots of pain while solving this problem. So thought to share if it helps someone else. Happy sharing. :)