-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0-bubble_sort.c
More file actions
47 lines (42 loc) · 817 Bytes
/
Copy path0-bubble_sort.c
File metadata and controls
47 lines (42 loc) · 817 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
47
#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.
* Return: void.
*/
void bubble_sort(int *array, size_t size)
{
size_t idx, len = size;
bool Bubbly = false;
if (array == NULL || size < 2)
return;
while (Bubbly == false)
{
Bubbly = true;
for (idx = 0; idx < len - 1; idx++)
{
if (array[idx] > array[idx + 1])
{
swap_ints(array + idx, array + idx + 1);
print_array(array, size);
Bubbly = false;
}
}
len--;
}
}