-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path0-bubble_sort.c
More file actions
49 lines (44 loc) · 810 Bytes
/
0-bubble_sort.c
File metadata and controls
49 lines (44 loc) · 810 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
#include "sort.h"
/**
* bubble_sort - A function to sort an array of integers
* @array: The array of integers to sort
* @size: Size of the array
*
* Return: None
*/
void bubble_sort(int *array, size_t size)
{
size_t i, tmp_size;
bool b_flag = false;
if (array == NULL || size < 2)
return;
tmp_size = size;
while (b_flag == false)
{
b_flag = true;
for (i = 0; i < tmp_size - 1; i++)
{
if (array[i] > array[i + 1])
{
swap(array + i, array + i + 1);
print_array(array, size);
b_flag = false;
}
}
tmp_size--;
}
}
/**
* swap - Function to swap position of 2 elements in list
* @first: Fist element
* @second: Second element
*
* Return: None
*/
void swap(int *first, int *second)
{
int tmp_elem;
tmp_elem = *first;
*first = *second;
*second = tmp_elem;
}