-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0-bubble_sort.c
More file actions
46 lines (41 loc) · 784 Bytes
/
0-bubble_sort.c
File metadata and controls
46 lines (41 loc) · 784 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "sort.h"
/**
* swap_ints - Swap two integers in an array.
* @a: The first integer to swap.
* @b: The second integer to swap.
*/
void swap_ints(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
/**
* bubble_sort - Sort an array of integers in ascending order.
* @array: An array of integers to sort.
* @size: The size of the array.
*
* Description: Prints the array after each swap.
*/
void bubble_sort(int *array, size_t size)
{
size_t i, len = size;
bool bubbly = false;
if (array == NULL || size < 2)
return;
while (bubbly == false)
{
bubbly = true;
for (i = 0; i < len - 1; i++)
{
if (array[i] > array[i + 1])
{
swap_ints(array + i, array + i + 1);
print_array(array, size);
bubbly = false;
}
}
len--;
}
}