SegFault on QuickSort Implementation

Hi, I implemented QuickSort to work with integers and it worked and know that I use the same implementation for Comparable objects i'm getting a Segmentation Fault, please help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
void Quick_Sort(Comparable datos[], int min, int max)
{
    int indice_pivote;

    if ((max-min) > 0)
    {
        indice_pivote = Encontrar_Pivote(datos, min, max);
        Quick_Sort(datos, min, indice_pivote-1);
        Quick_Sort(datos, indice_pivote+1, max);
    }
}

int Encontrar_Pivote(Comparable datos[], int min, int max)
{
    Comparable pivote = datos[min];
    int indice_pivote = min++;
    int res = 0;

    while (min<max)
    {
        if (datos[min].Compare_To(pivote)<=0 && datos[max].Compare_To(pivote)>=0)
        {
            min++;
            max--;
        }
        else if (datos[min].Compare_To(pivote)>=0 && datos[max].Compare_To(pivote)<=0)
        {
            Rotar(datos, min, max);
            min++;
            max--;
        }
        else if (datos[min].Compare_To(pivote)<=0 && datos[max].Compare_To(pivote)<=0)
        {
            min++;
        }
        else if (datos[min].Compare_To(pivote)>=0 && datos[max].Compare_To(pivote)>=0)
        {
            max--;
        }
    }

    if (min > max)
        res = max;
    else if (datos[min].Compare_To(pivote) < 0)
        res = min;
    else if (datos[min].Compare_To(pivote) > 0)
        res = (min - 1);

    Rotar(datos, res, indice_pivote);
    return res;
}

void Rotar(Comparable datos[], int primero, int segundo)
{
    Comparable aux = datos[primero];
    datos[primero] = datos[segundo];
    datos[segundo] = aux;
}

void Rotar(Comparable datos[], int primero, int segundo)
{
Comparable aux = datos[primero];
datos[primero] = datos[segundo];
datos[segundo] = aux;
}


Can I know what is the implementation of Comparable I presume is a class ? Above code attempt to do assignment between each other so I want to see your assignment operator overloading code (if any).

Also since C++ support operator overloading, you can shorten your code datos[min].Compare_To(pivote)<=0 which I presume is doing a comparison between 2 Comparable objects correct ?

E.g
datos[min] < pivote // this is much shorter code and then in your Comparable class you need to overload the < operator and maybe others too
Topic archived. No new replies allowed.