#include "head.h"

//=== === === RADIX SORT === === ===
uint32_t get_max(uint32_t* arr,int n);
void counting_sort(uint32_t* arr, int n, int exp);
void sort_radix(uint32_t* arr,int n){
    uint32_t max = get_max(arr, n);

    for(uint32_t exp = 1; max / exp > 0; exp *= 10)
        counting_sort(arr, n, exp);
}
uint32_t get_max(uint32_t* arr, int n) {
    uint32_t max = arr[0];
    for(int i = 1; i < n; i++)
        if(arr[i] > max)
            max = arr[i];
    return max;
}
void counting_sort(uint32_t* arr, int n, int exp) {
    uint32_t output[n];
    uint32_t count[10] = {0};

    for(int i = 0; i < n; i++)
        count[(arr[i] / exp) % 10]++;

    for(int i = 1; i < 10; i++)
        count[i] += count[i - 1];

    for(int i = n - 1; i >= 0; i--) {
        output[count[(arr[i] / exp) % 10] - 1] = arr[i];
        count[(arr[i] / exp) % 10]--;
    }

    for(int i = 0; i < n; i++)
        arr[i] = output[i];
}
// === === === HEAP SORT  === === ===
void heapify(uint32_t* arr, int n, int i);
void swap(uint32_t* a, uint32_t *b);
void sort_heap(uint32_t* arr,int n){
    for(int i = n/2 - 1; i >= 0; i--)
        heapify(arr, n, i);

    for(int i = n - 1; i > 0; i--) {
        swap(&arr[0], &arr[i]);
        heapify(arr, i, 0);
    }
}
void swap(uint32_t* a, uint32_t *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}
void heapify(uint32_t* arr, int n, int i) {
    int largest = i;
    int left = 2 * i + 1;
    int right = 2 * i + 2;

    if(left < n && arr[left] > arr[largest])
        largest = left;

    if(right < n && arr[right] > arr[largest])
        largest = right;

    if(largest != i) {
        swap(&arr[i], &arr[largest]);
        
        heapify(arr, n, largest);
    }
}

// === === === BUBBLE SORT === === ===
void sort_bubble(uint32_t* arr,int n){
    for(int i = 0; i < n - 1; i++) {
        int swapped = 0;
        for(int j = 0; j < n - i - 1; j++) {
            if(arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                swapped = 1;
            }
        }
        if(!swapped) break; 
    }
}

// === === === MERGE SORT === === ===
void merge(uint32_t* arr, int left, int mid, int right);
void sort_merge(uint32_t* arr, int left, int right){
    if(left < right) {
        int mid = left + (right - left) / 2;

        sort_merge(arr, left, mid);
        sort_merge(arr, mid + 1, right);
        merge(arr, left, mid, right);
    }
}
void merge(uint32_t* arr, int left, int mid, int right) {
    int n1 = mid - left + 1;
    int n2 = right - mid;

    int L[n1], R[n2];

    for(int i = 0; i < n1; i++)
        L[i] = arr[left + i];
    for(int j = 0; j < n2; j++)
        R[j] = arr[mid + 1 + j];

    int i = 0, j = 0, k = left;
    while(i < n1 && j < n2) {
        if(L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
        } else {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
    while(i < n1) { arr[k] = L[i]; i++; k++;}
    while(j < n2) { arr[k] = R[j]; j++; k++;}
}
