minishell
ft_qsort.c File Reference
#include "ft_sort.h"
#include "../ft_mem/ft_mem.h"
#include "../ft_math/ft_math.h"
Include dependency graph for ft_qsort.c:

Go to the source code of this file.

Functions

unsigned char * _at (unsigned char *base, size_t i, size_t memb_size)
 
void _ft_qsort (unsigned char *base, size_t nmemb, size_t memb_size, t_compar compar)
 
bool ft_qsort (void *base, size_t nmemb, size_t memb_size, t_compar compar)
 

Function Documentation

◆ _at()

unsigned char* _at ( unsigned char *  base,
size_t  i,
size_t  memb_size 
)

Definition at line 20 of file ft_qsort.c.

21 {
22  return (base + (i * memb_size));
23 }
Here is the caller graph for this function:

◆ _ft_qsort()

void _ft_qsort ( unsigned char *  base,
size_t  nmemb,
size_t  memb_size,
t_compar  compar 
)

Definition at line 25 of file ft_qsort.c.

27 {
28  size_t i;
29  size_t i_last;
30 
31  if (base == NULL || nmemb <= 1 || memb_size <= 0 || compar == NULL)
32  return ;
33  i = 1;
34  i_last = 0;
35  while (i < nmemb)
36  {
37  if (0 < compar(base, _at(base, i, memb_size)))
38  ft_swap(_at(base, i, memb_size),
39  _at(base, ++i_last, memb_size), memb_size);
40  i++;
41  }
42  ft_swap(base, _at(base, i_last, memb_size), memb_size);
43  if (2 <= i_last)
44  _ft_qsort(base, i_last, memb_size, compar);
45  if (2 <= (nmemb - i_last - 1))
46  _ft_qsort(_at(base, i_last + 1, memb_size), nmemb - i_last - 1,
47  memb_size, compar);
48 }
void ft_swap(void *a, void *b, size_t bytes)
Definition: ft_swap.c:32
void _ft_qsort(unsigned char *base, size_t nmemb, size_t memb_size, t_compar compar)
Definition: ft_qsort.c:25
unsigned char * _at(unsigned char *base, size_t i, size_t memb_size)
Definition: ft_qsort.c:20
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ft_qsort()

bool ft_qsort ( void *  base,
size_t  nmemb,
size_t  memb_size,
t_compar  compar 
)

Definition at line 50 of file ft_qsort.c.

51 {
52  if (!can_mulp(nmemb, memb_size))
53  return (false);
54  _ft_qsort((unsigned char *)base, nmemb, memb_size, compar);
55  return (true);
56 }
bool can_mulp(size_t a, size_t b)
Definition: can_mul.c:50
Here is the call graph for this function: