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

Selection Sort implementation using recursion. More...

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

Functions

void swap (int8_t *first, int8_t *second)
 for assert
 
uint8_t findIndex (const int8_t *arr, const uint8_t size)
 Returns the index having minimum value using recursion.
 
void selectionSort (int8_t *arr, const uint8_t size)
 Selection Sort algorithm implemented using recursion.
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Selection Sort implementation using recursion.

Author
Dhruv Pasricha

Function Documentation

◆ findIndex()

uint8_t findIndex ( const int8_t *  arr,
const uint8_t  size 
)

Returns the index having minimum value using recursion.

Parameters
arrarray to be sorted
sizesize of array
Returns
min_index index of an element having a minimum value
33{
34 if (size == 1)
35 {
36 return 0;
37 }
38
39 // marking recursive call to reach starting element
40 uint8_t min_index = findIndex(arr, size - 1);
41
42 if (arr[size - 1] < arr[min_index])
43 {
44 min_index = size - 1;
45 }
46
47 return min_index;
48}
uint8_t findIndex(const int8_t *arr, const uint8_t size)
Returns the index having minimum value using recursion.
Definition selection_sort_recursive.c:32
Here is the call graph for this function:

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
103{
104 /* Intializes random number generator */
105 srand(time(NULL));
106
107 test(); // run self-test implementations
108 return 0;
109}
static void test()
Self-test implementations.
Definition selection_sort_recursive.c:80
Here is the call graph for this function:

◆ selectionSort()

void selectionSort ( int8_t *  arr,
const uint8_t  size 
)

Selection Sort algorithm implemented using recursion.

Parameters
arrarray to be sorted
sizesize of the array
Returns
void
57{
58 if (size <= 1)
59 {
60 return;
61 }
62
63 /* findIndex(arr, size) returned the index having min value*/
64 uint8_t min_index = findIndex(arr, size);
65 /* arr[min_index] is the minimum value in the array*/
66
67 if (min_index != 0)
68 {
69 swap(&arr[0], &arr[min_index]);
70 }
71
72 /*sorted the remaining array recursively*/
73 selectionSort(arr + 1, size - 1);
74}
void selectionSort(int8_t *arr, const uint8_t size)
Selection Sort algorithm implemented using recursion.
Definition selection_sort_recursive.c:56
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
20{
21 int8_t temp = *first;
22 *first = *second;
23 *second = temp;
24}

◆ test()

static void test ( void  )
static

Self-test implementations.

Returns
void
81{
82 const uint8_t size = 10;
83 int8_t *arr = (int8_t *)calloc(size, sizeof(int8_t));
84
85 /* generate size random numbers from 0 to 100 */
86 for (uint8_t i = 0; i < size; i++)
87 {
88 arr[i] = rand() % 100;
89 }
90 selectionSort(arr, size);
91 for (uint8_t i = 0; i < size - 1; ++i)
92 {
93 assert(arr[i] <= arr[i + 1]);
94 }
95 free(arr);
96}
#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: