Quick sort

Hello, I'm writing code to do a quicksort, however I'm running into problems with stack overflow. This is a question for a homework so I'll have to abide by some constraints. I'll spare you all the details but my choose_pivot has 3 different functions it can use, each picking a new pivot. So here is my code.

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
  void my_qsort(int array[], int size, int(*choose_pivot)(int[], int)){
	my_qsort(array, 0, size - 1, choose_pivot);
}

void my_qsort(int array[], int low, int high, int(*choose_pivot)(int[], int)){
	int i = low;
	int j = high;
	int size = high - low;
	int tmp;

	if (size > 1){
		int pivot = array[choose_pivot(array, size)];
		//int pivot = array[rand() % size + i];
		//int pivot = array[0];
		//int pivot = array[size/2];
		while (i < j) {
			while (array[j] > pivot && j > i) {
				j--;
			}
			while (array[i] < pivot && i <= j) {
				i++;
			}
			if (i < j){
				tmp = array[i];
				array[i] = array[j];
				array[j] = tmp;
				i++;
			}
		}
		my_qsort(array, low, i, choose_pivot);
		my_qsort(array, j, high, choose_pivot);
	}
	else
		return;
}


I've commented out some trails of pivots, these are merely for testing purposes. This code has to work with me using choose_pivot for the pivot.
Any help is greatly appreciated!
Line 12: I don't know how choose_pivot(...) work, but if you pass the parameter like that it's very likely that the wrong pivot is chosen.

Rather: int pivot = array[choose_pivot(array + low, size)]; // Note: + low
Last edited on
Topic archived. No new replies allowed.