- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0-bubble_sort.c
35 lines (32 loc) · 631 Bytes
/
0-bubble_sort.c
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
#include"sort.h"
/**
* bubble_sort - Sorts an array of integers using the Bubble sort algorithm
*
* @array: array to be sorted
* @size: size of array
*/
voidbubble_sort(int*array, size_tsize)
{
size_ti, j;
inttemp;
boolswapped;
if (!array||size<2)
return;
for (i=0; i<size-1; i++)
{
swapped= false;
for (j=0; j<size-i-1; j++)
{
temp=array[j];
if (temp>array[j+1])
{
array[j] =array[j+1];
array[j+1] =temp;
swapped= true;
print_array(array, size);
}
}
if (!swapped)
break; /* break out of here if at least one swap didn't occur */
}
}