-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_sort_array.c
126 lines (115 loc) · 3.01 KB
/
ft_sort_array.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_sort_array.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dlana <dlana@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/29 20:20:26 by dlana #+# #+# */
/* Updated: 2021/09/30 18:34:33 by dlana ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void ft_record_id(t_point *points, int *array)
{
int count;
int i;
t_list *tmp;
tmp = points->a;
i = 0;
count = 0;
while (i != points->count_a)
{
while (tmp->numb != array[count] && count <= points->count_a)
count++;
if (tmp->numb == array[count])
{
tmp->id_new = count;
tmp = tmp->next;
}
i++;
count = 0;
}
}
static void ft_check_duplicate(t_point *points, int *array)
{
int count;
count = 0;
while (count < points->count_a)
{
if (count > 0 && array[count] == array[count - 1])
ft_error();
if (count < points->count_a - 1 && array[count] == array[count + 1])
ft_error();
count++;
}
}
static int *ft_go_to_array(int *array, int *start, int *end, int middle)
{
int left;
int right;
int tmp;
left = *start;
right = *end;
while (left < right)
{
while (array[left] < middle && left < right)
left++;
while (array[right] > middle && left < right)
right--;
if (array[left] == array[right] && left != right)
ft_error();
if (array[left] > array[right])
{
tmp = array[left];
array[left] = array[right];
array[right] = tmp;
}
}
*start = left;
*end = right;
return (array);
}
static int *ft_quick_sort(int *array, int start, int end, int count)
{
int middle;
int middle_id;
int left;
int right;
if (start < end && start != end)
{
left = start;
right = end;
middle_id = (left + right) / 2;
middle = array[middle_id];
*array = *ft_go_to_array(array, &left, &right, middle);
if (right == end && left > start)
left--;
if (start <= left && left != end)
ft_quick_sort(array, start, left, count);
if (right == start && right < end)
right++;
if (right <= end && start != right)
ft_quick_sort(array, right, end, count);
}
return (array);
}
void ft_sort_array(t_point *points)
{
int *array;
int count;
t_list *tmp;
tmp = points->a;
count = 0;
array = malloc(sizeof(int) * points->count_a);
while (count != points->count_a)
{
array[count] = tmp->numb;
tmp = tmp->next;
count++;
}
*array = *ft_quick_sort(array, 0, (count - 1), (count - 1));
ft_check_duplicate(points, array);
ft_record_id(points, array);
free(array);
}