mirror of
https://github.com/TheAlgorithms/C
synced 2024-12-24 20:16:57 +03:00
Merge pull request #802 from northernSage/fix-bubble-sort-2
[feat/fix/docs]: Fix bubble_sort2.c implementation bug and some documentation/structure improvements
This commit is contained in:
commit
7847be8ceb
@ -1,46 +1,87 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define MAX 20
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
/**
|
||||
* @file
|
||||
* @brief implementation of [Bubble sort](https://en.wikipedia.org/wiki/Bubble_sort) algorithm
|
||||
* @details
|
||||
* worst-case: O(n^2)
|
||||
* best-case: O(n)
|
||||
* average-complexity: O(n^2)
|
||||
|
||||
int main()
|
||||
{
|
||||
int i, arraySort[MAX] = {0}, isSort = FALSE, changePlace;
|
||||
|
||||
/* For example
|
||||
Insertion random values in array to test
|
||||
* @author Unknown author
|
||||
* @author [Gabriel Fioravante](https://github.com/northernSage)
|
||||
*/
|
||||
|
||||
for (i = 0; i < MAX; i++)
|
||||
{
|
||||
arraySort[i] = rand() % 101;
|
||||
}
|
||||
#include <stdlib.h> /// for rand() calls
|
||||
#include <assert.h> /// for assert(<expr>)
|
||||
#include <stdbool.h> /// for boolean values: true, false
|
||||
|
||||
/* Algorithm of bubble methods */
|
||||
#define MAX 20
|
||||
|
||||
while (isSort)
|
||||
{
|
||||
isSort = FALSE;
|
||||
/**
|
||||
* @brief Bubble sort implementation
|
||||
* @param array_sort the array to be sorted
|
||||
* @returns void
|
||||
*/
|
||||
void bubble_sort(int* array_sort)
|
||||
{
|
||||
bool is_sorted = false;
|
||||
|
||||
for (i = 0; i < MAX - 1; i++)
|
||||
/* keep iterating over entire array
|
||||
* and swaping elements out of order
|
||||
* until it is sorted */
|
||||
while (!is_sorted)
|
||||
{
|
||||
if (arraySort[i] > arraySort[i + 1])
|
||||
is_sorted = true;
|
||||
|
||||
/* iterate over all elements */
|
||||
for (int i = 0; i < MAX - 1; i++)
|
||||
{
|
||||
changePlace = arraySort[i];
|
||||
arraySort[i] = arraySort[i + 1];
|
||||
arraySort[i + 1] = changePlace;
|
||||
isSort = TRUE;
|
||||
/* check if adjacent elements are out of order */
|
||||
if (array_sort[i] > array_sort[i + 1])
|
||||
{
|
||||
/* swap elements */
|
||||
int change_place = array_sort[i];
|
||||
array_sort[i] = array_sort[i + 1];
|
||||
array_sort[i + 1] = change_place;
|
||||
/* elements out of order were found
|
||||
* so we reset the flag to keep ordering
|
||||
* until no swap operations are executed */
|
||||
is_sorted = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* See if it works */
|
||||
|
||||
for (i = 0; i < MAX; i++)
|
||||
{
|
||||
printf("%d\n", arraySort[i]);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test implementations
|
||||
* @returns void
|
||||
*/
|
||||
static void test() {
|
||||
/* simple int array for testing */
|
||||
int array_sort[MAX] = {0};
|
||||
|
||||
/* populate our test array with
|
||||
* random integer numbers */
|
||||
for (int i = 0; i < MAX; i++)
|
||||
{
|
||||
array_sort[i] = rand() % 101;
|
||||
}
|
||||
|
||||
/* sort array */
|
||||
bubble_sort(array_sort);
|
||||
|
||||
/* check if array ir correctly ordered */
|
||||
for (int i = 0; i < MAX - 1; i++)
|
||||
{
|
||||
assert(array_sort[i] <= array_sort[i+1]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
test(); // run self-test implementations
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user