aboutsummaryrefslogtreecommitdiff
path: root/sort.c
blob: b215b31f59fdda4e94c08c44724e7109446abb8e (plain)
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
#include "sort.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#define SUM(c) (c[FI_RGBA_RED] + c[FI_RGBA_GREEN] + c[FI_RGBA_BLUE])

unsigned partition(pixelm *arr, unsigned lo, unsigned hi) {
    int pivot = arr[lo].val;
    int i = lo - 1;
    int j = hi + 1;

    while (true) {
        do {
            i++;
        } while (arr[i].val < pivot);

        do {
            j--;
        } while (arr[j].val > pivot);

        if (i >= j) {
            return j;
        }

        // Swap j and i
        pixelm temp;
        memcpy(&temp, arr+i, sizeof(pixelm));
        memcpy(arr+i, arr+j, sizeof(pixelm));
        memcpy(arr+j, &temp, sizeof(pixelm));
    }
}

void quicksort(pixelm *arr, unsigned lo, unsigned hi) {
    if (lo >= hi) {
        return;
    }

    unsigned p = partition(arr, lo, hi);
    quicksort(arr, lo, p);
    quicksort(arr, p+1, hi);

    return;
}