-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0-bubble_sort.c
More file actions
52 lines (45 loc) · 782 Bytes
/
0-bubble_sort.c
File metadata and controls
52 lines (45 loc) · 782 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
48
49
50
51
52
#include "sort.h"
/**
* bubble_sort - Sorts an array of integers in ascending order using
* the Bubble sort algorithm
* @array: The array to be sorted
* @size: The size of the array
* Return: Nothing
*/
void bubble_sort(int *array, size_t size)
{
size_t j;
int fswap; /* swap_flag */
size_t n = size;
if (size < 2)
return;
while (1)
{
fswap = 0; /* false */
for (j = 1; j < n; j++)
{
if (array[j - 1] > array[j])
{
swap(&array[j - 1], &array[j]);
fswap = 1; /* true */
print_array(array, size);
}
}
if (!fswap)
break;
n--;
}
}
/**
* swap - Swaps the position of two numbers
* @a: The first integer
* @b: The Second integer
* Return: Nothing
*/
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}