qute

A software analysis framework built around the QBE intermediate language

git clone https://git.8pit.net/qute.git

 1// SPDX-FileCopyrightText: 2021 Gabriel Fioravante
 2// SPDX-FileCopyrightText: 2025 Sören Tempel <soeren+git@soeren-tempel.net>
 3//
 4// SPDX-License-Identifier: GPL-3.0-only
 5
 6// Taken from: https://github.com/TheAlgorithms/C/blob/f241de90e1691dc7cfcafcbecd89ef12db922e6b/sorting/bubble_sort_2.c
 7
 8#include <stdint.h>
 9#include <stdbool.h>
10
11void bubble_sort(int* array_sort, unsigned max)
12{
13    bool is_sorted = false;
14
15    /* keep iterating over entire array
16     * and swaping elements out of order
17     * until it is sorted */
18    while (!is_sorted)
19    {
20        is_sorted = true;
21
22        /* 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 found
33                 * so we reset the flag to keep ordering
34                 * until no swap operations are executed */
35                is_sorted = false;
36            }
37        }
38    }
39}
40
41int 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 that
45}
46
47void 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}
53
54int entry(unsigned max) {
55    int array[max];
56
57    fillary(array, max);
58    bubble_sort(array, max);
59
60    for (unsigned i = 0; i < max-1; i++) {
61        if (array[i] > array[i+1]) {
62            return 1;
63        }
64    }
65
66    return 0;
67}
68
69int main()
70{
71	entry(5000);
72}