Thank you for your exhaustive response, your answer has risen a few more interrogations. I will work on the subject and I will come back to you as soon as possible.
gautgaut
Posts
-
Quicksort slower than mergesort... -
Quicksort slower than mergesort...Thank you for your exhaustive response, your answer has risen a few more interrogations. I will work on the subject and I will come back to you as soon as possible.
-
Quicksort slower than mergesort...My list is not sorted at all :
void *ft_init_array_int(int len)
{
int *tab;srand(time(NULL)); if (!(tab = (int\*)malloc(len \* sizeof(int)))) return (NULL); while (len--) tab\[len\] = rand(); return ((void\*)tab);
}
I have noticed that if I my list has a lot of duplicate keys ("tab[len] = rand()%10" instead of "tab[len] = rand()"), my quicksort is much faster than my mergesort. I use the first element for the pivot. I wanted to have consistent result first and then improve my mergesort. For instance, it takes 7 seconds or so for my mergesort to sort 10.000.000 integers and around 13sec for my quicksort (qsort from stdlib.h takes 4sec). Does that mean that my algo (without improvement) does not work well enough ?
-
Quicksort slower than mergesort...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);
}
-
Quicksort slower than mergesort...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);
}