2020-07-21 18:16:11 +03:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief [Bubble sort](https://en.wikipedia.org/wiki/Bubble_sort) algorithm
|
|
|
|
* implementation
|
|
|
|
*/
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdbool.h>
|
2017-04-04 05:29:30 +03:00
|
|
|
#include <stdio.h>
|
2020-04-24 03:08:15 +03:00
|
|
|
#include <stdlib.h>
|
2020-07-21 18:16:11 +03:00
|
|
|
#include <time.h>
|
2016-09-03 19:58:04 +03:00
|
|
|
|
2020-07-21 18:16:11 +03:00
|
|
|
/**
|
|
|
|
* Display elements of array
|
|
|
|
* @param arr array to be display
|
|
|
|
* @param n length of array
|
|
|
|
*/
|
|
|
|
void display(const int *arr, int n)
|
2020-04-24 03:08:15 +03:00
|
|
|
{
|
2020-07-21 18:16:11 +03:00
|
|
|
for (int i = 0; i < n; i++)
|
2020-04-24 03:08:15 +03:00
|
|
|
{
|
2018-11-08 10:22:51 +03:00
|
|
|
printf("%d ", arr[i]);
|
2017-04-04 05:29:30 +03:00
|
|
|
}
|
2018-11-08 10:22:51 +03:00
|
|
|
printf("\n");
|
|
|
|
}
|
2017-04-04 05:29:30 +03:00
|
|
|
|
2020-07-21 18:16:11 +03:00
|
|
|
/**
|
|
|
|
* Swap two values by using pointer
|
|
|
|
* @param first first pointer of first number
|
|
|
|
* @param second second pointer of second number
|
|
|
|
*/
|
2020-04-24 03:08:15 +03:00
|
|
|
void swap(int *first, int *second)
|
|
|
|
{
|
2018-11-08 10:22:51 +03:00
|
|
|
int temp = *first;
|
|
|
|
*first = *second;
|
|
|
|
*second = temp;
|
|
|
|
}
|
2017-04-04 05:29:30 +03:00
|
|
|
|
2020-07-21 18:16:11 +03:00
|
|
|
/**
|
|
|
|
* Bubble sort algorithm implementation
|
|
|
|
* @param arr array to be sorted
|
|
|
|
* @param size size of array
|
2018-11-08 10:22:51 +03:00
|
|
|
*/
|
2020-04-24 03:08:15 +03:00
|
|
|
void bubbleSort(int *arr, int size)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < size - 1; i++)
|
2020-07-21 18:16:11 +03:00
|
|
|
{ /* for each array index */
|
|
|
|
bool swapped = false; /* flag to check if any changes had to be made */
|
|
|
|
/* perform iterations until no more changes were made or outer loop
|
|
|
|
executed for all array indices */
|
2020-04-24 03:08:15 +03:00
|
|
|
for (int j = 0; j < size - 1 - i; j++)
|
2020-07-21 18:16:11 +03:00
|
|
|
{ /* for each element in the array */
|
2020-04-24 03:08:15 +03:00
|
|
|
if (arr[j] > arr[j + 1])
|
2020-07-21 18:16:11 +03:00
|
|
|
{ /* if the order of successive elements needs update */
|
2020-04-24 03:08:15 +03:00
|
|
|
swap(&arr[j], &arr[j + 1]);
|
2020-07-21 18:16:11 +03:00
|
|
|
swapped = true; /* set flag */
|
2017-04-04 05:29:30 +03:00
|
|
|
}
|
|
|
|
}
|
2020-07-21 18:16:11 +03:00
|
|
|
if (!swapped)
|
|
|
|
{
|
|
|
|
/* since no more updates we made, the array is already sorted
|
|
|
|
this is an optimization for early termination */
|
|
|
|
break;
|
|
|
|
}
|
2017-04-04 05:29:30 +03:00
|
|
|
}
|
2018-11-08 10:22:51 +03:00
|
|
|
}
|
2017-04-04 05:29:30 +03:00
|
|
|
|
2020-07-21 18:16:11 +03:00
|
|
|
/**
|
|
|
|
* Test function
|
|
|
|
*/
|
|
|
|
void test()
|
2020-04-24 03:08:15 +03:00
|
|
|
{
|
2020-07-21 18:16:11 +03:00
|
|
|
const int size = 10;
|
|
|
|
int *arr = (int *)calloc(size, sizeof(int));
|
2020-04-24 03:08:15 +03:00
|
|
|
|
2020-07-21 18:16:11 +03:00
|
|
|
/* generate size random numbers from 0 to 100 */
|
|
|
|
for (int i = 0; i < size; i++)
|
2020-04-24 03:08:15 +03:00
|
|
|
{
|
2020-07-21 18:16:11 +03:00
|
|
|
arr[i] = rand() % 100;
|
|
|
|
}
|
|
|
|
bubbleSort(arr, size);
|
|
|
|
for (int i = 0; i < size - 1; ++i)
|
|
|
|
{
|
|
|
|
assert(arr[i] <= arr[i + 1]);
|
2018-11-08 10:22:51 +03:00
|
|
|
}
|
2020-04-24 03:08:15 +03:00
|
|
|
free(arr);
|
2020-07-21 18:16:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Driver Code */
|
|
|
|
int main(int argc, const char *argv[])
|
|
|
|
{
|
|
|
|
/* Intializes random number generator */
|
|
|
|
srand(time(NULL));
|
|
|
|
test();
|
2017-04-04 05:29:30 +03:00
|
|
|
return 0;
|
2018-11-08 10:22:51 +03:00
|
|
|
}
|