-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradixsort.c
More file actions
129 lines (119 loc) · 2.88 KB
/
radixsort.c
File metadata and controls
129 lines (119 loc) · 2.88 KB
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
127
128
129
/* ************************************************************************** */
/* */
/* :::::::: */
/* radixsort.c :+: :+: */
/* +:+ */
/* By: dkramer <dkramer@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2021/12/14 16:46:37 by dkramer #+# #+# */
/* Updated: 2022/01/19 18:24:27 by dkramer ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft/ft_printf/libftprintf.h"
#include "libft/libft.h"
#include <stdlib.h>
#include "push_swap.h"
int *sort_under3(int *stacka, t_str *str, int len)
{
if (stacka[0] + 2 == stacka[1])
rotateames(stacka, str);
if (stacka[0] == stacka[1] + 2)
rotateames(stacka, str);
if (stacka[0] == stacka[1] + 1)
swapames(stacka, str);
if (len >= 3 && stacka[2] == 0)
revrotateames(stacka, str);
return (stacka);
}
int *sort(int *stacka, int *stackb, t_str *str)
{
int lenb;
int i;
i = 0;
stacka = indexing(str, stacka, str->lena);
if (str->error == 1)
return (0);
stacka = sort_under3(stacka, str, str->lena);
if (str->error == 1)
return (0);
lenb = str->lenb;
if (lenb > 1 && stackb[0] < stackb[1])
swapbmes(stackb, str);
while (i < lenb)
{
stackb = pusha(stackb, &stacka, str);
if (str->error == 1)
return (0);
i++;
}
return (stacka);
}
int *sort_under5(int *stacka, int *stackb, t_str *str)
{
int x;
int lena;
lena = str->lena;
x = 0;
while (x < lena)
{
if (stacka[0] <= 1)
{
stacka = pushb(stacka, &stackb, str);
if (str->error == 1)
return (0);
}
else
rotateames(stacka, str);
if (str->lenb == 0 && issorted(str, stacka))
return (stacka);
x++;
}
return (sort(stacka, stackb, str));
}
void radixsorting(int *stackb, int **stacka, t_str *str, int i)
{
int x;
x = 0;
str->beforelena = str->lena;
while (x < str->beforelena)
{
if ((*stacka[0] >> i & 1) == 1)
rotateames(*stacka, str);
else
{
*stacka = pushb(*stacka, &stackb, str);
if (str->error == 1)
return ;
}
x++;
}
x = 0;
str->beforelenb = str->lenb;
while (x < str->beforelenb)
{
stackb = pusha(stackb, stacka, str);
if (str->error == 1)
return ;
x++;
}
}
int *ft_radixsort(int *stackb, int *stacka, t_str *str)
{
int i;
int a;
i = 0;
a = 0;
while (str->highestnr)
{
str->highestnr = str->highestnr >> 1;
a++;
}
while (i < a && !issorted(str, stacka))
{
radixsorting(stackb, &stacka, str, i);
if (str->error == 1)
return (0);
i++;
}
return (stacka);
}