This commit is contained in:
2024-10-13 15:27:04 +02:00
parent d449d4b10a
commit 3a1e525a4b
15 changed files with 155 additions and 19 deletions

View File

@@ -64,5 +64,29 @@ void hanoi_rec(stack *p1, stack *p2, stack *p3,
}
void bubble_sort_stack(stack *p) {
int n = size_stack(p);
stack *p_aux = create_stack();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
push(pop(p), p_aux);
}
print_stacks(n - i, 2, p, p_aux);
for (int j = i; j < n - 1; j++) {
int a = pop(p);
int b = pop(p_aux);
if (a < b) {
push(a, p);
push(b, p);
} else {
push(b, p);
push(a, p);
}
}
for (int j = 0; j < i; j++) {
push(pop(p_aux), p);
}
printf("\n");
}
delete_stack(p_aux);
return;
}