Skip to content

Commit e43024e

Browse files
bubble sort optimization (#573)
1 parent 3dc9472 commit e43024e

File tree

1 file changed

+59
-31
lines changed

1 file changed

+59
-31
lines changed

sorting/bubble_sort.c

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,95 @@
1-
// sorting of array list using bubble sort
1+
/**
2+
* @file
3+
* @brief [Bubble sort](https://en.wikipedia.org/wiki/Bubble_sort) algorithm
4+
* implementation
5+
*/
6+
#include <assert.h>
7+
#include <stdbool.h>
28
#include <stdio.h>
39
#include <stdlib.h>
10+
#include <time.h>
411

5-
/*Displays the array, passed to this method*/
6-
void display(int *arr, int n)
12+
/**
13+
* Display elements of array
14+
* @param arr array to be display
15+
* @param n length of array
16+
*/
17+
void display(const int *arr, int n)
718
{
8-
int i;
9-
for (i = 0; i < n; i++)
19+
for (int i = 0; i < n; i++)
1020
{
1121
printf("%d ", arr[i]);
1222
}
13-
1423
printf("\n");
1524
}
1625

17-
/*Swap function to swap two values*/
26+
/**
27+
* Swap two values by using pointer
28+
* @param first first pointer of first number
29+
* @param second second pointer of second number
30+
*/
1831
void swap(int *first, int *second)
1932
{
2033
int temp = *first;
2134
*first = *second;
2235
*second = temp;
2336
}
2437

25-
/*This is where the sorting of the array takes place
26-
arr[] --- Array to be sorted
27-
size --- Array Size
38+
/**
39+
* Bubble sort algorithm implementation
40+
* @param arr array to be sorted
41+
* @param size size of array
2842
*/
2943
void bubbleSort(int *arr, int size)
3044
{
3145
for (int i = 0; i < size - 1; i++)
32-
{
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 */
3350
for (int j = 0; j < size - 1 - i; j++)
34-
{
51+
{ /* for each element in the array */
3552
if (arr[j] > arr[j + 1])
36-
{
53+
{ /* if the order of successive elements needs update */
3754
swap(&arr[j], &arr[j + 1]);
55+
swapped = true; /* set flag */
3856
}
3957
}
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+
}
4064
}
4165
}
4266

43-
int main(int argc, const char *argv[])
67+
/**
68+
* Test function
69+
*/
70+
void test()
4471
{
45-
int n;
46-
printf("Enter size of array:\n");
47-
scanf("%d", &n); // E.g. 8
72+
const int size = 10;
73+
int *arr = (int *)calloc(size, sizeof(int));
4874

49-
printf("Enter the elements of the array\n");
50-
int i;
51-
int *arr = (int *)malloc(n * sizeof(int));
52-
for (i = 0; i < n; i++)
75+
/* generate size random numbers from 0 to 100 */
76+
for (int i = 0; i < size; i++)
5377
{
54-
scanf("%d", &arr[i]);
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]);
5584
}
56-
57-
printf("Original array: ");
58-
display(arr, n); // Original array : 10 11 9 8 4 7 3 8
59-
60-
bubbleSort(arr, n);
61-
62-
printf("Sorted array: ");
63-
display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11
64-
6585
free(arr);
86+
}
87+
88+
/** Driver Code */
89+
int main(int argc, const char *argv[])
90+
{
91+
/* Intializes random number generator */
92+
srand(time(NULL));
93+
test();
6694
return 0;
6795
}

0 commit comments

Comments
 (0)