1// SPDX-FileCopyrightText: 2021 Gabriel Fioravante2// SPDX-FileCopyrightText: 2025 Sören Tempel <soeren+git@soeren-tempel.net>3//4// SPDX-License-Identifier: GPL-3.0-only56// Taken from: https://github.com/TheAlgorithms/C/blob/f241de90e1691dc7cfcafcbecd89ef12db922e6b/sorting/bubble_sort_2.c78#include <stdint.h>9#include <stdbool.h>1011void bubble_sort(int* array_sort, unsigned max)12{13 bool is_sorted = false;1415 /* keep iterating over entire array16 * and swaping elements out of order17 * until it is sorted */18 while (!is_sorted)19 {20 is_sorted = true;2122 /* iterate over all elements */23 for (int i = 0; i < max - 1; i++)24 {25 /* check if adjacent elements are out of order */26 if (array_sort[i] > array_sort[i + 1])27 {28 /* swap elements */29 int change_place = array_sort[i];30 array_sort[i] = array_sort[i + 1];31 array_sort[i + 1] = change_place;32 /* elements out of order were found33 * so we reset the flag to keep ordering34 * until no swap operations are executed */35 is_sorted = false;36 }37 }38 }39}4041int rand(uint64_t *seed)42{43 *seed = 6364136223846793005ULL*(*seed) + 1; // From musl libc.44 return (*seed); // can't shift here because we don't have that45}4647void fillary(int *ary, unsigned n) {48 uint64_t seed = 42;49 for (unsigned i = 0; i < n; i++) {50 ary[i] = rand(&seed);51 }52}5354int entry(unsigned max) {55 int array[max];5657 fillary(array, max);58 bubble_sort(array, max);5960 for (unsigned i = 0; i < max-1; i++) {61 if (array[i] > array[i+1]) {62 return 1;63 }64 }6566 return 0;67}6869int main()70{71 entry(5000);72}