Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
heap_sort_2.c File Reference

Heap Sort implementation More...

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <inttypes.h>
Include dependency graph for heap_sort_2.c:

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.
 

Detailed Description

Heap Sort implementation

Author
Dhruv Pasricha

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))

Function Documentation

◆ heapifyDown()

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.

Parameters
arrarray to be sorted
sizesize of array
Returns
void
49{
50 uint8_t i = 0;
51
52 while (2 * i + 1 < size)
53 {
54 uint8_t maxChild = 2 * i + 1;
55
56 if (2 * i + 2 < size && arr[2 * i + 2] > arr[maxChild])
57 {
58 maxChild = 2 * i + 2;
59 }
60
61 if (arr[maxChild] > arr[i])
62 {
63 swap(&arr[i], &arr[maxChild]);
64 i = maxChild;
65 }
66 else
67 {
68 break;
69 }
70 }
71}

◆ heapifyUp()

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.

Parameters
arrarray to be sorted
iindex of the pushed element
Returns
void
83{
84 while (i > 0 && arr[(i - 1) / 2] < arr[i])
85 {
86 swap(&arr[(i - 1) / 2], &arr[i]);
87 i = (i - 1) / 2;
88 }
89}

◆ heapSort()

void heapSort ( int8_t *  arr,
const uint8_t  size 
)

Heap Sort algorithm.

Parameters
arrarray to be sorted
sizesize of the array
Returns
void
98{
99 if (size <= 1)
100 {
101 return;
102 }
103
104 for (uint8_t i = 0; i < size; i++)
105 {
106 // Pushing `arr[i]` to the heap
107
108 /*heapifyUp Adjusts arr[i] to the correct position in the heap*/
109 heapifyUp(arr, i);
110 }
111
112 for (uint8_t i = size - 1; i >= 1; i--)
113 {
114 // Moving current root to the end
115 swap(&arr[0], &arr[i]);
116
117 // `heapifyDown` adjusts new root to the correct position in the heap
118 heapifyDown(arr, i);
119
120 }
121}
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 though...
Definition heap_sort_2.c:48
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...
Definition heap_sort_2.c:82
Here is the call graph for this function:

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
150{
151 // Intializes random number generator
152 srand(time(NULL));
153
154 test(); // run self-test implementations
155 return 0;
156}
static void test()
Self-test implementations.
Definition heap_sort_2.c:127
Here is the call graph for this function:

◆ swap()

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

Parameters
firstpointer of first number
secondpointer of second number
33{
34 int8_t temp = *first;
35 *first = *second;
36 *second = temp;
37}

◆ test()

static void test ( void  )
static

Self-test implementations.

Returns
void
128{
129 const uint8_t size = 10;
130 int8_t *arr = (int8_t *)calloc(size, sizeof(int8_t));
131
132 /* generate size random numbers from 0 to 100 */
133 for (uint8_t i = 0; i < size; i++)
134 {
135 arr[i] = rand() % 100;
136 }
137 heapSort(arr, size);
138 for (uint8_t i = 0; i < size - 1; ++i)
139 {
140 assert(arr[i] <= arr[i + 1]);
141 }
142 free(arr);
143}
void heapSort(int8_t *arr, const uint8_t size)
Heap Sort algorithm.
Definition heap_sort_2.c:97
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition malloc_dbg.h:26
#define calloc(elemCount, elemSize)
This macro replace the standard calloc function with calloc_dbg.
Definition malloc_dbg.h:22
Here is the call graph for this function: