Quicksort slower than mergesort...
C / C++ / MFC
1
Posts
1
Posters
0
Views
1
Watching
-
I try to implement classical sorting algorithm, I do not understand why my quicksort is slower than my mergesort. I spend a lot of time trying to understand why. Do you have any idea ? For both :
#define ARRAY_INDEX(tab, i) ((void*)((char*)(tab) + (i) * type_size))
My mergesort :
void merge(void *tab, void *aux, int a, int b)
{
int i, j, k, mid;ft\_copy\_tab(tab, aux, a, b); mid = (a + b) / 2; i = a; j = mid + 1; k = a; while (k <= b) { if (i > mid) ft\_copy\_index(tab, aux, k, j++); else if (j > b) ft\_copy\_index(tab, aux, k, i++); else if (f\_cmp(ARRAY\_INDEX(aux, i), ARRAY\_INDEX(aux, j)) > 0) ft\_copy\_index(tab, aux, k, j++); else ft\_copy\_index(tab, aux, k, i++); k++; } return ;
}
My quicksort :
void ft_quicksort(void *tab, int a, int b)
{
int i, lower, upper;if (a >= b) return ; lower = a; upper = b; i = a; while (i <= upper) { if (f\_cmp(ARRAY\_INDEX(tab, lower), ARRAY\_INDEX(tab, i)) > 0) { ft\_swap(tab, lower, i, type\_size); lower++; i++; } else if (f\_cmp(ARRAY\_INDEX(tab, lower), ARRAY\_INDEX(tab, i)) < 0) { ft\_swap(tab, i, upper, type\_size); upper--; } else ++i; } ft\_sort(tab, a, lower - 1); ft\_sort(tab, upper + 1, b);
}