|
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> |
2 | 8 | #include <stdio.h>
|
3 | 9 | #include <stdlib.h>
|
| 10 | +#include <time.h> |
4 | 11 |
|
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) |
7 | 18 | {
|
8 |
| - int i; |
9 |
| - for (i = 0; i < n; i++) |
| 19 | + for (int i = 0; i < n; i++) |
10 | 20 | {
|
11 | 21 | printf("%d ", arr[i]);
|
12 | 22 | }
|
13 |
| - |
14 | 23 | printf("\n");
|
15 | 24 | }
|
16 | 25 |
|
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 | + */ |
18 | 31 | void swap(int *first, int *second)
|
19 | 32 | {
|
20 | 33 | int temp = *first;
|
21 | 34 | *first = *second;
|
22 | 35 | *second = temp;
|
23 | 36 | }
|
24 | 37 |
|
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 |
28 | 42 | */
|
29 | 43 | void bubbleSort(int *arr, int size)
|
30 | 44 | {
|
31 | 45 | 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 */ |
33 | 50 | for (int j = 0; j < size - 1 - i; j++)
|
34 |
| - { |
| 51 | + { /* for each element in the array */ |
35 | 52 | if (arr[j] > arr[j + 1])
|
36 |
| - { |
| 53 | + { /* if the order of successive elements needs update */ |
37 | 54 | swap(&arr[j], &arr[j + 1]);
|
| 55 | + swapped = true; /* set flag */ |
38 | 56 | }
|
39 | 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 | + } |
40 | 64 | }
|
41 | 65 | }
|
42 | 66 |
|
43 |
| -int main(int argc, const char *argv[]) |
| 67 | +/** |
| 68 | + * Test function |
| 69 | + */ |
| 70 | +void test() |
44 | 71 | {
|
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)); |
48 | 74 |
|
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++) |
53 | 77 | {
|
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]); |
55 | 84 | }
|
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 |
| - |
65 | 85 | 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(); |
66 | 94 | return 0;
|
67 | 95 | }
|
0 commit comments