Search code examples
c++stringquicksort

Why is my C++ QuickSort function for strings stuck in an infinite loop?


I am having problems with using a QuickSort function to organize Strings in C++

Been using a couple of YT tutorials and internet examples to build this function, but it keeps repeating indefinetly, i get the logic of why it repeats itself, but i don't know how to fix it so it stops doing so

Would really appreciate if someone could point out if i am doing something worng or how to fix it

struct Personaslista
{
    std::string Nombre;
    int EspacioArreglo = 0;
} X[CantidadDePersonas];


void QUICKSORT(int Primero, int Ultimo, int B)
{
    int I, J, Central = 0;
    //const wchar_t* Pivote;
    std::string PIVOTE;

    Central = (Primero + Ultimo) / 2; //Posición central del arreglo

    //Agarrar valor del medio del Arreglo

    //Pivote = X[Central].Clave;
    //Pivote = X[Central].Clave;
    PIVOTE = X[Central].Nombre;

    //Separar Segmentos

    I = Primero;
    J = Ultimo;


    do
    {
        //Separar el Arreglo en 2

        while (X[I].Nombre <= PIVOTE) //Valores Menores al Pivote
        {
            I++;
        }
        while (X[J].Nombre > PIVOTE) //Valores Mayores al Pivote
        {
            J--;
        }

        
        if (X[I].Nombre <= X[J].Nombre)
        {
            //const wchar_t* Temp;
            std::string Temp;
            int Poral;
            Temp = X[I].Nombre;
            Poral = X[I].EspacioArreglo;
            X[I].Nombre = X[J].Nombre;
            X[I].EspacioArreglo = X[J].EspacioArreglo;
            X[J].Nombre = Temp;
            X[J].EspacioArreglo = Poral;
            I++;
            J--;

        }


    } while (I <= J);

    if (Primero < J)
    {
        //Primero = J;
        QUICKSORT(Primero, J, B);
    }

    if (I < Ultimo)
    {
        
        QUICKSORT(I, Ultimo, B);
    }

    return;
}

Solution

  • Well, for starters, your code did not even compile. You are missing variables? I tried to fix it and also flipped the directions of a few inequalities (assuming you want the sort to be in ascending order).

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    #define CantidadDePersonas 8
    
    struct Personaslista {
      string Nombre;
      int EspacioArreglo;
    } X[CantidadDePersonas];
    
    void QUICKSORT(int Primero, int Ultimo, int B) {
      int I, J, Central = 0;
      string PIVOTE;
    
      Central = (Primero + Ultimo) / 2; // Posición central del arreglo
    
      // Agarrar valor del medio del Arreglo
    
      // Pivote = X[Central].Clave;
      // Pivote = X[Central].Clave;
      PIVOTE = X[Central].Nombre;
    
      // Separar Segmentos
    
      I = Primero;
      J = Ultimo;
      do { // Separar el Arreglo en 2
        // Valores Menores al Pivote
        while (X[I].Nombre <= PIVOTE) {
          I++;
        } // Valores Mayores al Pivote
        while (X[J].Nombre > PIVOTE) {
          J--;
        }
        if (X[I].Nombre > X[J].Nombre) {
          // const wchar_t* Temp;
          string Temp;
          int Poral;
          Temp = X[I].Nombre;
          Poral = X[I].EspacioArreglo;
          X[I].Nombre = X[J].Nombre;
          X[I].EspacioArreglo = X[J].EspacioArreglo;
          X[J].Nombre = Temp;
          X[J].EspacioArreglo = Poral;
          I++;
          J--;
        }
      } while (I <= J);
      if (Primero > J) {
        // Primero = J;
        QUICKSORT(Primero, J, B);
      }
      if (I > Ultimo) {
        QUICKSORT(I, Ultimo, B);
      }
      return;
    }
    
    int main(int argc, char *argv[]) {
      int i = 72;
      struct Personaslista *ptr = X;
      struct Personaslista *endPtr = X + sizeof(X) / sizeof(X[0]);
      while (ptr < endPtr) {
        (*ptr).EspacioArreglo = i - 65;
        (*ptr).Nombre = (char)i;
        cout << (*ptr).EspacioArreglo << ", " << (*ptr).Nombre << endl;
        i--;
        ptr++;
      }
      cout << "---" << endl;
      QUICKSORT(0, CantidadDePersonas - 1, -1);
    
      ptr = X;
      while (ptr < endPtr) {
        cout << (*ptr).EspacioArreglo << ", " << (*ptr).Nombre << endl;
        ptr++;
      }
      return 0;
    }
    

    You also never really initialised Nombre. Note that you are still never really using the recursive variable B. This works when there are no recursive calls.

    g++ temp.cpp -o temp.out
    ./temp.out
    

    Now prints,

    7, H
    6, G
    5, F
    4, E
    3, D
    2, C
    1, B
    0, A
    ---
    0, A
    1, B
    2, C
    4, E
    5, F
    3, D
    6, G
    7, H
    

    Can you implement the recursion correctly now?