data_structures
array_insertion_sort.c
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* array_insertion_sort.c :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2020/07/19 16:16:40 by unite #+# #+# */
10 /* Updated: 2020/09/07 21:50:59 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
14 #include "array.h"
15 
16 static void array_insert_next(t_array *array, size_t j)
17 {
18  while (j > 0)
19  {
20  if (array->type->cmp(array_get(array, j), array_get(array, j - 1)) < 0)
21  array_swap(array, j, j - 1);
22  else
23  break ;
24  j--;
25  }
26 }
27 
29 {
30  size_t i;
31  size_t j;
32 
33  if (array->type->cmp == NULL)
34  ds_exit_set(ENOTSUP);
35  i = 0;
36  while (i < array->size - 1)
37  {
38  if (array->type->cmp(array_get(array, i), array_get(array, i + 1)) > 0)
39  array_insert_next(array, i + 1);
40  i++;
41  }
42 }
array_get
void * array_get(const t_array *array, size_t index)
Returns the item at the specified position in this list.
Definition: array_get.c:16
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_insertion_sort
void array_insertion_sort(t_array *array)
Sorts this array using insertion sort.
Definition: array_insertion_sort.c:28
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
A resizable array.
Definition: array.h:47
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