implementation of Bubble sort algorithm
More...
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
|
#define | MAX 20 |
| for rand() calls for assert(<expr>)
|
|
|
void | bubble_sort (int *array_sort) |
| Bubble sort implementation.
|
|
static void | test () |
| Test implementations.
|
|
int | main () |
| Main function.
|
|
implementation of Bubble sort algorithm
worst-case: O(n^2) best-case: O(n) average-complexity: O(n^2)
- Author
- Unknown author
-
Gabriel Fioravante
◆ MAX
for rand() calls for assert(<expr>)
for boolean values: true, false
◆ bubble_sort()
void bubble_sort |
( |
int * |
array_sort | ) |
|
Bubble sort implementation.
- Parameters
-
array_sort | the array to be sorted |
- Returns
- void
25{
26 bool is_sorted = false;
27
28
29
30
31 while (!is_sorted)
32 {
33 is_sorted = true;
34
35
36 for (
int i = 0; i <
MAX - 1; i++)
37 {
38
39 if (array_sort[i] > array_sort[i + 1])
40 {
41
42 int change_place = array_sort[i];
43 array_sort[i] = array_sort[i + 1];
44 array_sort[i + 1] = change_place;
45
46
47
48 is_sorted = false;
49 }
50 }
51 }
52}
#define MAX
for rand() calls for assert(<expr>)
Definition bubble_sort_2.c:17
◆ main()
Main function.
- Returns
- 0 on exit
84{
86 return 0;
87}
static void test()
Test implementations.
Definition bubble_sort_2.c:58
◆ test()
static void test |
( |
void |
| ) |
|
|
static |
Test implementations.
- Returns
- void
58 {
59
60 int array_sort[
MAX] = {0};
61
62
63
64 for (
int i = 0; i <
MAX; i++)
65 {
66 array_sort[i] = rand() % 101;
67 }
68
69
71
72
73 for (
int i = 0; i <
MAX - 1; i++)
74 {
75 assert(array_sort[i] <= array_sort[i+1]);
76 }
77}
void bubble_sort(int *array_sort)
Bubble sort implementation.
Definition bubble_sort_2.c:24