Selection Sort implementation using recursion.
More...
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <inttypes.h>
|
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.
|
|
Selection Sort implementation using recursion.
- Author
- Dhruv Pasricha
◆ findIndex()
uint8_t findIndex |
( |
const int8_t * |
arr, |
|
|
const uint8_t |
size |
|
) |
| |
Returns the index having minimum value using recursion.
- Parameters
-
arr | array to be sorted |
size | size 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
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
◆ main()
Main function.
- Returns
- 0 on exit
103{
104
105 srand(time(NULL));
106
108 return 0;
109}
static void test()
Self-test implementations.
Definition selection_sort_recursive.c:80
◆ selectionSort()
void selectionSort |
( |
int8_t * |
arr, |
|
|
const uint8_t |
size |
|
) |
| |
Selection Sort algorithm implemented using recursion.
- Parameters
-
arr | array to be sorted |
size | size of the array |
- Returns
- void
57{
58 if (size <= 1)
59 {
60 return;
61 }
62
63
65
66
67 if (min_index != 0)
68 {
69 swap(&arr[0], &arr[min_index]);
70 }
71
72
74}
void selectionSort(int8_t *arr, const uint8_t size)
Selection Sort algorithm implemented using recursion.
Definition selection_sort_recursive.c:56
◆ 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
-
first | pointer of first number |
second | pointer 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
86 for (uint8_t i = 0; i < size; i++)
87 {
88 arr[i] = rand() % 100;
89 }
91 for (uint8_t i = 0; i < size - 1; ++i)
92 {
93 assert(arr[i] <= arr[i + 1]);
94 }
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