forked from laucavv/sorting_algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0-bubble_sort.c
More file actions
42 lines (36 loc) · 687 Bytes
/
0-bubble_sort.c
File metadata and controls
42 lines (36 loc) · 687 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
#include "sort.h"
/**
* bubble_sort - function that sorts an array of integers in ascending
* order using the Bubble sort algorithm
* @array: list of number to order
* @size: Lenght of array
* Return: Always 0
*/
void bubble_sort(int *array, size_t size)
{
int stop = (size - 1), i;
int flag, tmp = 0;
if (array == NULL || size < 2)
return;
for (i = 0; i < stop; i++)
{
flag = 0;
if (array[i] > array[i + 1])
{
tmp = array[i];
array[i] = array[i + 1];
array[i + 1] = tmp;
flag = 1;
print_array(array, size);
}
if (flag == 0 && i == (stop - 1))
{
return;
}
if (i == (stop - 1) && flag == 1)
{
i = -1;
stop = stop - 1;
}
}
}