Problem with Quicksort, left pivot point

There is a small problem with my code. I have been searching for hours and cant find out what it is. All numbers get sorted except for 2.

My original numbers are like this:
10,55,22,40,45,31,5,3,60,11
Once "sorted" they are like this:
3 5 10 31 40 22 45 11 55 60

UI_QuickSort() is called in main()

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
//I know it's not popular but for now im forced to use it
#include <conio.h> 
//custom library at my school, i dont think its used in this code part
#include"console(v1.9).h"
#include"QuickSort.h"

using namespace std;

int pivot=0;
int nbElements = 0;

void DisplayArray(int tab[], int nbElements)
{
	for (int n = 0; n < nbElements; n++)
		cout << tab[n] << ' ';
	cout << endl;
}

void Swap(int tab[], int left, int right)
{
	int temp = tab[left];
	tab[left] = tab[right];
	tab[right] = temp;
}

int Partition(int tab[], int left, int right)
{
	int l = left, r = right, temp=0;
	int pivot = l;

	while (tab[l] <= tab[pivot])
		l++;
	while (tab[r] > tab[pivot])
		r--;
	if (l <= r)
	{
		Swap(tab, l, r);
		DisplayArray(tab, nbElements);
		pivot = l;
	}
	else
	{
		Swap(tab, pivot, r);
		DisplayArray(tab, nbElements);
		pivot = d;
	}
	return pivot;
}

void QuickSort(int tab[], int left, int right)
{
	if (left < right)
	{
		pivot = Partition(tab, left, right);
		QuickSort(tab, left, pivot - 1);
		QuickSort(tab, pivot + 1, right);
	}
}


void UI_QuickSort()
{
	const int NB_NOMBRES_MIN = 10;
	const int NB_NOMBRES_MAX = 20;
	int nbNombres = 10;
	int tabNombres[NB_NOMBRES_MAX] = {10,55,22,40,45,31,5,3,60,11};
	
	cout << "Numbers : ";
	DisplayArray(tab, nbElements);
	QuickSort(tab, 0, nbElements - 1);
	DisplayArray(tabNombres, nbNombres);//should display sorted numbers
	_getch();
}
1
2
3
4
while (tab[l] <= tab[pivot])
    l++;
while (tab[r] > tab[pivot])
    r--;

It seems you opted for the Hoare partition scheme:
https://en.wikipedia.org/wiki/Quicksort
In that case, your Partition() function is definitely wrong.
You don’t need to compare the element at position l (or r) to element at position pivot: you need to compare element at position l to pivot.

When asking a question, it’s a good idea to try to wonder how could people work out an answer.
If you post a code which is missing the basic elements to compile, such as a main(), for example, how could people test it?

If your code includes libraries you don’t share, how can people compile it?
If those libraries are irrelevant, why do you mention them?

Anyway, I’ve just realized perhaps you were not asking anything: as a matter of fact, you put no question. That’s could explain why you weren’t getting any answers, I think…

So, let’s say, just as a hobby, you can have a look here:
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
59
60
61
62
63
64
65
#include <iostream>


void displayArray(int* tab, int size);
void quickSort(int* tab, int left, int right);
void swap(int* tab, int left, int right);
int partition(int* tab, int left, int right);


int main()
{
    int tab_nombres[] = { 10, 55, 22, 40, 45, 31, 5, 3, 60, 11 };
    int size { static_cast<int>(sizeof(tab_nombres) / sizeof(tab_nombres[0])) };

    std::cout << "Numbers: ";
    displayArray(tab_nombres, size);
    quickSort(tab_nombres, 0, size - 1);
    displayArray(tab_nombres, size);
}


void displayArray(int* tab, int size)
{
    for (int n {}; n < size; ++n) {
        std::cout << tab[n] << ' ';
    }
    std::cout << '\n';
}


void quickSort(int* tab, int left, int right)
{
    if (left < right) {
        int pivot = partition(tab, left, right);
        quickSort(tab, left, pivot - 1);
        quickSort(tab, pivot + 1, right);
    }
}


void swap(int* tab, int left, int right)
{
    int temp = tab[left];
    tab[left] = tab[right];
    tab[right] = temp;
}


int partition(int* tab, int left, int right)
{
    // It seems you chose the Hoare partition scheme:
    // https://en.wikipedia.org/wiki/Quicksort
    int pivot = tab[(left + right) / 2];

    for(;;) {
        while ( tab[left]  < pivot ) { ++left;  }
        while ( tab[right] > pivot ) { --right; }

        if (left >= right) {
            return right;
        } else {
            swap(tab, left, right);
        }
    }
}


Output
Numbers: 10 55 22 40 45 31 5 3 60 11
3 5 10 11 22 31 40 45 55 60

Thank you! I will try to follow your advice next time i post.
Topic archived. No new replies allowed.