Bubble sort algorithm implementation using recursion.
More...
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
|
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.
|
|
Bubble sort algorithm implementation using recursion.
◆ bubbleSort()
void bubbleSort |
( |
int * |
arr, |
|
|
int |
size |
|
) |
| |
Bubble sort algorithm implements using recursion.
- Parameters
-
arr | array to be sorted |
size | size 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 {
47 }
48}
void bubbleSort(int *arr, int size)
Bubble sort algorithm implements using recursion.
Definition bubble_sort_recursion.c:29
◆ main()
Driver Code.
73{
74
75 srand(time(NULL));
77 return 0;
78}
void test()
Test function.
Definition bubble_sort_recursion.c:53
◆ swap()
void swap |
( |
int * |
first, |
|
|
int * |
second |
|
) |
| |
Swapped two numbers using pointer.
- Parameters
-
first | first pointer of first number |
second | second pointer of second number |
18{
19 int temp = *first;
20 *first = *second;
21 *second = temp;
22}
◆ test()
Test function.
54{
55 const int size = 10;
56 int *arr = (
int *)
calloc(size,
sizeof(
int));
57
58
59 for (int i = 0; i < size; i++)
60 {
61 arr[i] = rand() % 100;
62 }
64 for (int i = 0; i < size - 1; ++i)
65 {
66 assert(arr[i] <= arr[i + 1]);
67 }
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