Skip to content

Commit 1392223

Browse files
committed
Adding C BubbleSort function
1 parent 9ccbda7 commit 1392223

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/c/BubbleSort.c

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <stdio.h>
2+
3+
void swap(int array[], int j)
4+
{
5+
int t = array[j];
6+
array[j] = array[j + 1];
7+
array[j + 1] = t;
8+
}
9+
10+
void bubble_sort(int array[], int n)
11+
{
12+
for (int i = 0; i < n - 1; i++)
13+
{
14+
for (int j = 0; j < n - i - 1; j++)
15+
{
16+
if (array[j] > array[j + 1])
17+
{
18+
swap(array, j);
19+
}
20+
}
21+
}
22+
}
23+
24+
int main()
25+
{
26+
int array_size = 9;
27+
int array[10] = {99, 33, 22, 10, 5, 7, 9, 0, 15, 27};
28+
29+
bubble_sort(array, array_size);
30+
31+
printf("Lista ordenada:\n");
32+
for (int i = 0; i < array_size - 1; i++)
33+
printf("%d, ", array[i]);
34+
35+
printf("%d\n", array[array_size - 1]);
36+
37+
return 0;
38+
}

0 commit comments

Comments
 (0)