Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
bubble_sort_recursion.c File Reference

Bubble sort algorithm implementation using recursion. More...

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Include dependency graph for bubble_sort_recursion.c:

Functions

void swap (int *first, int *second)
 Swapped two numbers using pointer.
 
void bubbleSort (int *arr, int size)
 Bubble sort algorithm implements using recursion.
 
void test ()
 Test function.
 
int main ()
 Driver Code.
 

Detailed Description

Bubble sort algorithm implementation using recursion.

Function Documentation

◆ bubbleSort()

void bubbleSort ( int *  arr,
int  size 
)

Bubble sort algorithm implements using recursion.

Parameters
arrarray to be sorted
sizesize of array
30{
31 if (size == 1)
32 {
33 return;
34 }
35 bool swapped = false;
36 for (int i = 0; i < size - 1; ++i)
37 {
38 if (arr[i] > arr[i + 1])
39 {
40 swap(arr + i, arr + i + 1);
41 swapped = true;
42 }
43 }
44 if (swapped)
45 {
46 bubbleSort(arr, size - 1);
47 }
48}
void bubbleSort(int *arr, int size)
Bubble sort algorithm implements using recursion.
Definition bubble_sort_recursion.c:29
Here is the call graph for this function:

◆ main()

int main ( void  )

Driver Code.

73{
74 /* Intializes random number generator */
75 srand(time(NULL));
76 test();
77 return 0;
78}
void test()
Test function.
Definition bubble_sort_recursion.c:53
Here is the call graph for this function:

◆ swap()

void swap ( int *  first,
int *  second 
)

Swapped two numbers using pointer.

Parameters
firstfirst pointer of first number
secondsecond pointer of second number
18{
19 int temp = *first;
20 *first = *second;
21 *second = temp;
22}

◆ test()

void test ( void  )

Test function.

54{
55 const int size = 10;
56 int *arr = (int *)calloc(size, sizeof(int));
57
58 /* generate size random numbers from 0 to 100 */
59 for (int i = 0; i < size; i++)
60 {
61 arr[i] = rand() % 100;
62 }
63 bubbleSort(arr, size);
64 for (int i = 0; i < size - 1; ++i)
65 {
66 assert(arr[i] <= arr[i + 1]);
67 }
68 free(arr);
69}
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition malloc_dbg.h:26
#define calloc(elemCount, elemSize)
This macro replace the standard calloc function with calloc_dbg.
Definition malloc_dbg.h:22
Here is the call graph for this function: