TheAlgorithms-C/sorting/selection_sort_recursive.c

110 lines
2.4 KiB
C
Raw Normal View History

2021-02-24 11:24:57 +03:00
/**
* @file
2021-02-24 21:42:15 +03:00
* @author [Dhruv Pasricha](https://github.com/DhruvPasricha)
2021-02-24 11:24:57 +03:00
* @brief [Selection Sort](https://en.wikipedia.org/wiki/Selection_sort)
* implementation using recursion.
*/
2021-03-01 19:36:40 +03:00
#include <assert.h> /// for assert
#include <stdio.h> /// for IO operations
#include <stdlib.h> /// for dynamic memory allocation
#include <time.h> /// for random numbers generation
2021-03-01 19:36:40 +03:00
#include <inttypes.h> /// for uint8_t, int8_t
2021-02-24 11:24:57 +03:00
/**
* @brief Swapped two numbers using pointer
* @param first pointer of first number
* @param second pointer of second number
2021-02-24 11:24:57 +03:00
*/
2021-03-01 19:36:40 +03:00
void swap(int8_t *first, int8_t *second)
2021-02-24 11:24:57 +03:00
{
int8_t temp = *first;
2021-02-24 11:24:57 +03:00
*first = *second;
*second = temp;
}
/**
* @brief Returns the index having minimum value using recursion
* @param arr array to be sorted
2021-02-24 11:24:57 +03:00
* @param size size of array
* @return min_index index of an element having a minimum value
2021-02-24 11:24:57 +03:00
*/
2021-03-01 19:36:40 +03:00
uint8_t findIndex(const int8_t *arr, const uint8_t size)
2021-02-24 11:24:57 +03:00
{
if (size == 1)
{
return 0;
}
// marking recursive call to reach starting element
2021-02-25 15:18:28 +03:00
uint8_t min_index = findIndex(arr, size - 1);
2021-02-24 11:24:57 +03:00
if (arr[size - 1] < arr[min_index])
{
min_index = size - 1;
}
return min_index;
}
/**
* @brief Selection Sort algorithm implemented using recursion
2021-02-24 11:24:57 +03:00
* @param arr array to be sorted
* @param size size of the array
* @returns void
2021-02-24 11:24:57 +03:00
*/
2021-03-01 19:36:40 +03:00
void selectionSort(int8_t *arr, const uint8_t size)
2021-02-24 11:24:57 +03:00
{
2021-03-01 19:36:40 +03:00
if (size <= 1)
2021-02-24 11:24:57 +03:00
{
return;
}
/* findIndex(arr, size) returned the index having min value*/
2021-02-25 15:18:28 +03:00
uint8_t min_index = findIndex(arr, size);
2021-02-24 11:24:57 +03:00
/* arr[min_index] is the minimum value in the array*/
if (min_index != 0)
{
swap(&arr[0], &arr[min_index]);
}
/*sorted the remaining array recursively*/
selectionSort(arr + 1, size - 1);
}
/**
* @brief Self-test implementations
* @returns void
2021-02-24 11:24:57 +03:00
*/
static void test()
2021-02-24 11:24:57 +03:00
{
2021-02-25 15:18:28 +03:00
const uint8_t size = 10;
2021-03-01 19:36:40 +03:00
int8_t *arr = (int8_t *)calloc(size, sizeof(int8_t));
2021-02-24 11:24:57 +03:00
/* generate size random numbers from 0 to 100 */
2021-02-25 15:18:28 +03:00
for (uint8_t i = 0; i < size; i++)
2021-02-24 11:24:57 +03:00
{
arr[i] = rand() % 100;
}
selectionSort(arr, size);
2021-02-25 15:18:28 +03:00
for (uint8_t i = 0; i < size - 1; ++i)
2021-02-24 11:24:57 +03:00
{
assert(arr[i] <= arr[i + 1]);
}
free(arr);
}
/**
* @brief Main function
* @returns 0 on exit
*/
2021-02-24 11:24:57 +03:00
int main()
{
/* Intializes random number generator */
srand(time(NULL));
test(); // run self-test implementations
2021-02-24 11:24:57 +03:00
return 0;
}