mirror of https://github.com/TheAlgorithms/C
Bubble sort recursion (#574)
* bubble sort by recursion * updating DIRECTORY.md * fix compile error Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
parent
5d9bf07851
commit
3dc947213a
|
@ -338,6 +338,7 @@
|
|||
* [Bogo Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/bogo_sort.c)
|
||||
* [Bubble Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/bubble_sort.c)
|
||||
* [Bubble Sort 2](https://github.com/TheAlgorithms/C/blob/master/sorting/bubble_sort_2.c)
|
||||
* [Bubble Sort Recursion](https://github.com/TheAlgorithms/C/blob/master/sorting/bubble_sort_recursion.c)
|
||||
* [Bucket Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/bucket_sort.c)
|
||||
* [Cocktail Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/cocktail_sort.c)
|
||||
* [Comb Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/comb_sort.c)
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
/**
|
||||
* @file
|
||||
* @brief [Bubble sort](https://en.wikipedia.org/wiki/Bubble_sort) algorithm
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bubble sort algorithm implements using recursion
|
||||
* @param arr array to be sorted
|
||||
* @param size size of array
|
||||
*/
|
||||
void bubbleSort(int *arr, int size)
|
||||
{
|
||||
if (size == 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
bool swapped = false;
|
||||
for (int i = 0; i < size - 1; ++i)
|
||||
{
|
||||
if (arr[i] > arr[i + 1])
|
||||
{
|
||||
swap(arr + i, arr + i + 1);
|
||||
swapped = true;
|
||||
}
|
||||
}
|
||||
if (swapped)
|
||||
{
|
||||
bubbleSort(arr, 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;
|
||||
}
|
||||
bubbleSort(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;
|
||||
}
|
Loading…
Reference in New Issue