-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexing.c
85 lines (77 loc) · 1.21 KB
/
indexing.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
#include "push_swap.h"
void make_index(t_stack *stack, int32_t len, t_two_stacks **stacks)
{
int32_t *arr;
int32_t i;
t_stack *tmp;
i = 0;
tmp = stack;
arr = (int32_t *)malloc(sizeof(int32_t) * (len + 1));
if (!arr)
error_handle(stacks);
while (i < len)
{
arr[i] = tmp->value;
tmp = tmp->next;
++i;
}
q_sort(arr, 0, len - 1);
finish_indexing(stack, arr, len);
}
void q_sort(int32_t *num, int32_t start, int32_t end)
{
int32_t p;
int32_t i;
int32_t j;
if (start < end)
{
i = start;
j = end;
p = num[(i + j) / 2];
q_sort_main_loop(num, &i, &j, p);
q_sort(num, start, j);
q_sort(num, i, end);
}
}
void finish_indexing(t_stack *stack, int32_t *arr, int32_t len)
{
int32_t t;
t_stack *tmp;
t = 0;
tmp = stack;
while (tmp)
{
arr = &arr[0];
t = 0;
while (t <= len)
{
if (tmp->value == arr[t])
{
tmp->index = (t + 1);
break ;
}
++t;
}
tmp = tmp->next;
}
free(&arr[0]);
}
void q_sort_main_loop(int32_t *num, int32_t *i, int32_t *j, int32_t p)
{
int32_t tmp;
while (*i <= *j)
{
while (num[*i] < p)
++*i;
while (num[*j] > p)
--*j;
if (*i <= *j)
{
tmp = *(num + *i);
*(num + *i) = *(num + *j);
*(num + *j) = tmp;
++*i;
--*j;
}
}
}