-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort_big.c
99 lines (90 loc) · 2.6 KB
/
sort_big.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_big.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oel-ouar <oel-ouar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/01 15:46:24 by oel-ouar #+# #+# */
/* Updated: 2021/11/01 18:57:00 by oel-ouar ### ########.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
int find_max(t_stack *stack)
{
int max;
max = stack->value;
stack = stack->next;
while (stack)
{
if (stack->value > max)
max = stack->value;
stack = stack->next;
}
return (max);
}
void sortList(t_stack *stack) {
t_stack *current = stack, *index = NULL;
int temp;
int count = 1;
while(current != NULL)
{
index = current->next;
count++;
while(index != NULL)
{
if(current->value > index->value)
{
temp = current->value;
current->value = index->value;
index->value = temp;
}
index = index->next;
}
current = current->next;
}
}
int find_mid(int argc, char **argv)
{
t_stack *current = NULL;
int count = 0;
current = parsing(argv, argc);
sortList(current);
while (count < argc / 2)
{
current = current->next;
count++;
}
return (current->value);
}
void sort_big(t_stack **stack, int argc, char **argv)
{
t_stack *stack_b = NULL;
int max;
int min;
int mid;
mid = find_mid(argc, argv);
max = find_max(*stack);
min = find_min(*stack);
while (*stack)
{
if ((*stack)->value != max
&& (*stack)->value != min)
{
push(stack, &stack_b);
write(1, "pb\n", 3);
if (stack_b->value < mid)
{
rotate(&stack_b, 1);
write(1, "rb\n", 3);
}
}
else
*stack = (*stack)->next;
}
while (stack_b)
{
printf("%d\n", stack_b->value);
stack_b = stack_b->next;
}
}