data_structures
array_quick_sort.c
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* array_quick_sort.c :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2020/07/19 14:38:31 by unite #+# #+# */
10 /* Updated: 2020/09/07 21:50:34 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
14 #include "array.h"
15 
16 static size_t array_partition(t_array *array, size_t lo, size_t hi)
17 {
18  size_t i;
19  size_t j;
20 
21  i = lo;
22  j = hi + 1;
23  while (1)
24  {
25  while (array->type->cmp(array->arr[++i], array->arr[lo]) <= 0)
26  if (i == hi)
27  break ;
28  while (array->type->cmp(array->arr[lo], array->arr[--j]) <= 0)
29  if (j == lo)
30  break ;
31  if (i >= j)
32  break ;
33  array_swap(array, i, j);
34  }
35  array_swap(array, j, lo);
36  return (j);
37 }
38 
39 static void array_quick_sort_recur(t_array *array, size_t lo, size_t hi)
40 {
41  size_t pivot;
42 
43  if (lo >= hi)
44  return ;
45  pivot = array_partition(array, lo, hi);
46  array_quick_sort_recur(array, lo, pivot);
47  array_quick_sort_recur(array, pivot + 1, hi);
48 }
49 
51 {
52  if (array->type->cmp == NULL)
53  ds_exit_set(ENOTSUP);
54  array_quick_sort_recur(array, 0, array->size - 1);
55 }
array_quick_sort
void array_quick_sort(t_array *array)
Sorts this array using quick sort.
Definition: array_quick_sort.c:50
s_type::cmp
int(* cmp)(const void *, const void *)
(optional) A function ponter used to compare members of this data type
Definition: types.h:52
array.h
array_swap
void array_swap(t_array *array, size_t ind1, size_t ind2)
Swaps elements at the two specified positions in the array.
Definition: array_swap.c:16
s_array::type
const t_type * type
The type of items in this array.
Definition: array.h:52
s_array::arr
void ** arr
The data.
Definition: array.h:51
s_array
A resizable array.
Definition: array.h:47
s_array::size
size_t size
The number of items in this array.
Definition: array.h:49
ds_exit_set
void ds_exit_set(int err)
Set errno to the specified value, print the error message, and exit the process.
Definition: ds_exit_set.c:22