Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
|
Heap Sort implementation More...
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <inttypes.h>
Functions | |
void | swap (int8_t *first, int8_t *second) |
for assert | |
void | heapifyDown (int8_t *arr, const uint8_t size) |
heapifyDown Adjusts new root to the correct position in the heap This heapify procedure can be thought of as building a heap from the top down by successively shifting downward to establish the heap property. | |
void | heapifyUp (int8_t *arr, uint8_t i) |
heapifyUp Adjusts arr[i] to the correct position in the heap This heapify procedure can be thought of as building a heap from the bottom up by successively shifting upward to establish the heap property. | |
void | heapSort (int8_t *arr, const uint8_t size) |
Heap Sort algorithm. | |
static void | test () |
Self-test implementations. | |
int | main () |
Main function. | |
Heap Sort implementation
Heap-sort is a comparison-based sorting algorithm. Heap-sort can be thought of as an improved selection sort: like selection sort, heap sort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element from it and inserting it into the sorted region.
Unlike selection sort, heap sort does not waste time with a linear-time scan of the unsorted region; rather, heap sort maintains the unsorted region in a heap data structure to more quickly find the largest element in each step. Time Complexity : O(Nlog(N))
void heapifyDown | ( | int8_t * | arr, |
const uint8_t | size | ||
) |
heapifyDown Adjusts new root to the correct position in the heap This heapify procedure can be thought of as building a heap from the top down by successively shifting downward to establish the heap property.
arr | array to be sorted |
size | size of array |
void heapifyUp | ( | int8_t * | arr, |
uint8_t | i | ||
) |
heapifyUp Adjusts arr[i] to the correct position in the heap This heapify procedure can be thought of as building a heap from the bottom up by successively shifting upward to establish the heap property.
arr | array to be sorted |
i | index of the pushed element |
void heapSort | ( | int8_t * | arr, |
const uint8_t | size | ||
) |
Heap Sort algorithm.
arr | array to be sorted |
size | size of the array |
int main | ( | void | ) |
void swap | ( | int8_t * | first, |
int8_t * | second | ||
) |
for assert
for IO operations for dynamic memory allocation for random numbers generation for uint8_t, int8_t
Swapped two numbers using pointer
first | pointer of first number |
second | pointer of second number |
|
static |
Self-test implementations.