Expansion: A version of quicksort for wordlists

It is intended to be used with the full version not the direct quicksort
as the direct one is not currently correctly protected against end index
being out of bounds.
This commit is contained in:
Jérôme Guélen 2025-03-10 12:37:50 +01:00
parent 95d9f6282a
commit e7f12b54e9
No known key found for this signature in database
4 changed files with 128 additions and 9 deletions

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* wordlist.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/11 11:14:10 by khais #+# #+# */
/* Updated: 2025/03/11 11:14:32 by khais ### ########.fr */
/* Created: 2025/02/13 15:46:02 by khais #+# #+# */
/* Updated: 2025/03/21 10:11:30 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,19 +6,79 @@
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/08 17:29:05 by jguelen #+# #+# */
/* Updated: 2025/03/08 17:31:56 by jguelen ### ########.fr */
/* Updated: 2025/03/10 12:36:27 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "wordlist_quicksort.h"
static void ft_swap_wordlist_contents(t_wordlist *list1, t_wordlist *list2)
{
t_worddesc *tmp_word;
tmp_word = list1->word;
list1->word = list2->word;
list2->word = tmp_word->word;
}
/*
** The pivot is selected as in classic variations to be the last element of the
** list initially.
** The partitionning makes it so in the end all elements smaller than the pivot,
** which in this case will be determined by a simple order of growing ascii
** value on the word->word component of the wordlist, are on the left of the
** pivot value and all elements greater on the right.
** @RETURN the index at wich the pivot is now to be found.
*/
static int wordlist_quicksort_partition(t_wordlist *list, int start, int end)
{
int i;
int j;
t_wordlist *pivot;
t_wordlist *list_i;
t_wordlist *list_j;
i = start;
j = start;
list_i = wordlist_get_elem(list, start);
list_j = wordlist_get_elem(list, start);
pivot = wordlist_get_elem(list, end);
while (j < end)
{
if (ft_strcmp(list_j->word->word, pivot->word->word) > 0)
{
ft_swap_wordlist_contents(list_i, list_j);
i++;
list_i = list_i->next;
}
j++;
list_j = list_j->next;
}
ft_swap_wordlist_contents(list_i, pivot);
return (i);
}
/*
** TODO
** Returns the wordlist list sorted in ascending ascii order.
** Proceeds by directly swapping the inside contents and not by rewiring the
** nodes themselves.
*/
t_wordlist *wordlist_quicksort(t_wordlist *list)
t_wordlist *wordlist_quicksort(t_wordlist *list, int start, int end)
{
int pivot;
if (start >= end || !list || start < 0)
return (list);
pivot = wordlist_quicksort_partition(list, start, end);
wordlist_quicksort(list, start, pivot - 1);
wordlist_quicksort(list, pivot + 1, end);
return (list);
}
/*
** Is intended as a shortcut for practical use as a full list sort.
*/
t_wordlist *wordlist_quicksort_full(t_wordlist *list)
{
return (wordlist_quicksort(list, 0, wordlist_size(list) - 1));
}

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* wordlist_quicksort.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/08 17:26:52 by jguelen #+# #+# */
/* Updated: 2025/03/08 17:28:59 by jguelen ### ########.fr */
/* Updated: 2025/03/10 12:34:23 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,6 +15,7 @@
# include "wordlist.h"
t_wordlist *wordlist_quicksort(t_wordlist *list);
t_wordlist *wordlist_quicksort(t_wordlist *list, int low, int high);
t_wordlist *wordlist_quicksort_full(t_wordlist *list);
#endif

View file

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* wordlist_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/10 09:51:34 by jguelen #+# #+# */
/* Updated: 2025/03/10 12:23:36 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "wordlist.h"
#include <stdlib.h>
/*
** Returns the number of words present in the wordlist given as parameter.
** NOTE: Assumes that that list not to be circular.
*/
int wordlist_size(t_wordlist *wordlist)
{
int size;
size = 0;
while (wordlist)
{
size++;
wordlist = wordlist->next;
}
return (size);
}
t_wordlist *wordlist_last(t_wordlist *list)
{
if (!list)
return (NULL);
while (list->next)
list = list->next;
return (list);
}
/*
** Returns the node at index idx in list or NULL if idx is out
** of bounds.
*/
t_wordlist *wordlist_get_elem(t_wordlist *list, int idx)
{
if (list == NULL || idx < 0)
return (NULL);
while (idx > 0 && list != NULL)
{
list = list->next;
idx--;
}
if (list == NULL)
return (NULL);
return (list);
}