-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.c
47 lines (43 loc) · 1.45 KB
/
sort.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yhadari <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/29 15:08:49 by yhadari #+# #+# */
/* Updated: 2021/06/03 21:05:38 by yhadari ### ########.fr */
/* */
/* ************************************************************************** */
#include "pushswap.h"
void swaping(t_sort *tab, int *swap, int index)
{
*swap = tab->str[index + 1];
tab->str[index + 1] = tab->str[index];
tab->str[index] = *swap;
}
t_sort sort(t_stack a)
{
int i;
int index;
int swap;
t_sort tab;
i = 0;
index = a.top;
swap = 0;
tab.str = malloc (sizeof(int) * a.top + 1);
while (i <= a.top)
tab.str[i++] = a.data[index--];
i = 0;
while (i++ <= a.top + 1)
{
index = a.top;
while (index-- > 0)
{
if (tab.str[index + 1] < tab.str[index])
swaping(&tab, &swap, index);
}
}
tab.med = tab.str[(a.top + 1) / 2];
return (tab);
}