Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
G

gautgaut

@gautgaut
About
Posts
5
Topics
2
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Quicksort slower than mergesort...
    G gautgaut

    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.

    C / C++ / MFC algorithms data-structures question

  • Quicksort slower than mergesort...
    G gautgaut

    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.

    C / C++ / MFC algorithms data-structures question

  • Quicksort slower than mergesort...
    G gautgaut

    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 ?

    C / C++ / MFC algorithms data-structures question

  • Quicksort slower than mergesort...
    G gautgaut

    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);
    

    }

    C / C++ / MFC algorithms data-structures question

  • Quicksort slower than mergesort...
    G gautgaut

    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);
    

    }

    C / C++ / MFC algorithms data-structures question
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups