-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path0-bubble_sort.c
More file actions
52 lines (44 loc) · 766 Bytes
/
0-bubble_sort.c
File metadata and controls
52 lines (44 loc) · 766 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 - Calls function
* @array: Array to be sorted
* @size: Size of array
* Description: Function that sorts an array using the bubble sort method
* Return: 0
*/
void bubble_sort(int *array, size_t size)
{
unsigned int i, j;
bool swapped;
if (!array)
return;
for (i = 0; i < size - 1; i++)
{
swapped = false;
for (j = 0; j < size - i - 1; j++)
{
if (array[j] > array[j + 1])
{
swap_func(&array[j], &array[j + 1]);
swapped = true;
print_array(array, size);
}
}
if (!swapped)
break;
}
}
/**
* swap_func - Function that swaps two values
*
* @a: Fisrt value
* @b: Second value
* Return: 0
*/
void swap_func(int *a, int *b)
{
int tmp;
tmp = *b;
*b = *a;
*a = tmp;
}