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

Bubble sort algorithm implementation More...

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

Functions

void display (const int *arr, int n)
 Display elements of array.
 
void swap (int *first, int *second)
 Swap two values by using pointer.
 
void bubbleSort (int *arr, int size)
 Bubble sort algorithm implementation.
 
void test ()
 Test function.
 
int main (int argc, const char *argv[])
 Driver Code.
 

Detailed Description

Bubble sort algorithm implementation

Function Documentation

◆ bubbleSort()

void bubbleSort ( int *  arr,
int  size 
)

Bubble sort algorithm implementation.

Parameters
arrarray to be sorted
sizesize of array
44{
45 for (int i = 0; i < size - 1; i++)
46 { /* for each array index */
47 bool swapped = false; /* flag to check if any changes had to be made */
48 /* perform iterations until no more changes were made or outer loop
49 executed for all array indices */
50 for (int j = 0; j < size - 1 - i; j++)
51 { /* for each element in the array */
52 if (arr[j] > arr[j + 1])
53 { /* if the order of successive elements needs update */
54 swap(&arr[j], &arr[j + 1]);
55 swapped = true; /* set flag */
56 }
57 }
58 if (!swapped)
59 {
60 /* since no more updates we made, the array is already sorted
61 this is an optimization for early termination */
62 break;
63 }
64 }
65}

◆ display()

void display ( const int *  arr,
int  n 
)

Display elements of array.

Parameters
arrarray to be display
nlength of array
18{
19 for (int i = 0; i < n; i++)
20 {
21 printf("%d ", arr[i]);
22 }
23 printf("\n");
24}

◆ main()

int main ( int  argc,
const char *  argv[] 
)

Driver Code.

90{
91 /* Intializes random number generator */
92 srand(time(NULL));
93 test();
94 return 0;
95}
void test()
Test function.
Definition bubble_sort.c:70
Here is the call graph for this function:

◆ swap()

void swap ( int *  first,
int *  second 
)

Swap two values by using pointer.

Parameters
firstfirst pointer of first number
secondsecond pointer of second number
32{
33 int temp = *first;
34 *first = *second;
35 *second = temp;
36}

◆ test()

void test ( void  )

Test function.

71{
72 const int size = 10;
73 int *arr = (int *)calloc(size, sizeof(int));
74
75 /* generate size random numbers from 0 to 100 */
76 for (int i = 0; i < size; i++)
77 {
78 arr[i] = rand() % 100;
79 }
80 bubbleSort(arr, size);
81 for (int i = 0; i < size - 1; ++i)
82 {
83 assert(arr[i] <= arr[i + 1]);
84 }
85 free(arr);
86}
void bubbleSort(int *arr, int size)
Bubble sort algorithm implementation.
Definition bubble_sort.c:43
#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: