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
|
|
|
|
2021-03-01 00:41:58 +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
|
|
|
|
|
|
|
/**
|
2021-02-24 21:24:42 +03:00
|
|
|
* @brief Swapped two numbers using pointer
|
2021-02-24 21:30:41 +03:00
|
|
|
* @param first pointer of first number
|
2021-02-24 21:30:52 +03:00
|
|
|
* @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
|
|
|
{
|
2021-03-01 22:38:40 +03:00
|
|
|
int8_t temp = *first;
|
2021-02-24 11:24:57 +03:00
|
|
|
*first = *second;
|
|
|
|
*second = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-24 22:06:51 +03:00
|
|
|
* @brief Returns the index having minimum value using recursion
|
2021-02-24 21:28:06 +03:00
|
|
|
* @param arr array to be sorted
|
2021-02-24 11:24:57 +03:00
|
|
|
* @param size size of array
|
2021-03-01 07:46:43 +03:00
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-24 21:25:01 +03:00
|
|
|
* @brief Selection Sort algorithm implemented using recursion
|
2021-02-24 11:24:57 +03:00
|
|
|
* @param arr array to be sorted
|
2021-02-24 22:07:02 +03:00
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-24 22:16:56 +03:00
|
|
|
* @brief Self-test implementations
|
|
|
|
* @returns void
|
2021-02-24 11:24:57 +03:00
|
|
|
*/
|
2021-02-24 22:16:47 +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);
|
|
|
|
}
|
|
|
|
|
2021-02-24 22:17:18 +03:00
|
|
|
/**
|
|
|
|
* @brief Main function
|
|
|
|
* @returns 0 on exit
|
|
|
|
*/
|
2021-02-24 11:24:57 +03:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
/* Intializes random number generator */
|
|
|
|
srand(time(NULL));
|
2021-02-26 08:47:06 +03:00
|
|
|
|
2021-02-24 22:15:17 +03:00
|
|
|
test(); // run self-test implementations
|
2021-02-24 11:24:57 +03:00
|
|
|
return 0;
|
2021-02-24 21:24:42 +03:00
|
|
|
}
|