insertion_sort_recursive (#647)

* insertion_sort_recursive

* feat: add insertion sort recursive algorithm

* Update sorting/insertion_sort_recursive.c

Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>

* Update sorting/insertion_sort_recursive.c

Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>

* Update insertion_sort_recursive.c

* Update sorting/insertion_sort_recursive.c

Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>

* Update sorting/insertion_sort_recursive.c

Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>

* Update sorting/insertion_sort_recursive.c

Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>

Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>
This commit is contained in:
vinayak 2020-10-16 18:31:45 +05:30 committed by GitHub
parent a050a48bfd
commit bad50992fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,71 @@
/**
* @file
* @brief [Insertion sort](https://en.wikipedia.org/wiki/Insertion_sort)
* algorithm implementation.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/**
* @addtogroup sorting Sorting algorithms
* @{
*/
/**
* Insertion sort algorithm implements using Recursion
* @param arr array to be sorted
* @param size size of array
*/
void RecursionInsertionSort(int *arr, int size)
{
if(size <= 0)
{
return;
}
// marking recursive call to reach starting element
RecursionInsertionSort(arr,size-1);
int key = arr[size-1];
int j = size-2;
// swapping logic for insertion sort
while(j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = key;
}
/** @} */
/** Test function
* @returns None
*/
static void test()
{
const int size = rand() % 500; /* random array size */
int *arr = (int *)calloc(size, sizeof(int));
/* generate size random numbers from -50 to 49 */
for (int i = 0; i < size; i++)
{
arr[i] = (rand() % 100) - 50;/* signed random numbers */
}
RecursionInsertionSort(arr, size);
for (int i = 0; i < size ; ++i)
{
assert(arr[i] <= arr[i + 1]);
}
free(arr);
}
/** Main function
* @returns integer 0
*/
int main(int argc, const char *argv[])
{
/* Intializes random number generator */
srand(time(NULL));
test();
return 0;
}