added selection_sort_recursive.c

This commit is contained in:
DhruvPasricha 2021-02-24 13:54:57 +05:30
parent 28fc15c65b
commit 98a1b14242

View File

@ -0,0 +1,100 @@
/**
* @file
* @brief [Selection Sort](https://en.wikipedia.org/wiki/Selection_sort)
* implementation using recursion.
*/
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/**
* Swapped two numbers using pointer
* @param first first pointer of first number
* @param second second pointer of second number
*/
void swap(int *first, int *second)
{
int temp = *first;
*first = *second;
*second = temp;
}
/**
* returned the index having minimum value using recursion
* @param arr array to be sorted
* @param size size of array
*/
int findIndex(int *arr, int size)
{
if (size == 1)
{
return 0;
}
// marking recursive call to reach starting element
int min_index = findIndex(arr, size - 1);
if (arr[size - 1] < arr[min_index])
{
min_index = size - 1;
}
return min_index;
}
/**
* Selection Sort algorithm implemented using recursion
* @param arr array to be sorted
* @param size size of array
*/
void selectionSort(int *arr, int size)
{
if (size == 1)
{
return;
}
/* findIndex(arr, size) returned the index having min value*/
int min_index = findIndex(arr, size);
/* 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);
}
/**
* Test function
*/
void test()
{
const int size = 10;
int *arr = (int *)calloc(size, sizeof(int));
/* generate size random numbers from 0 to 100 */
for (int i = 0; i < size; i++)
{
arr[i] = rand() % 100;
}
selectionSort(arr, size);
for (int i = 0; i < size - 1; ++i)
{
assert(arr[i] <= arr[i + 1]);
}
free(arr);
}
/** Driver Code */
int main()
{
/* Intializes random number generator */
srand(time(NULL));
test();
return 0;
}