forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.c
More file actions
38 lines (31 loc) · 688 Bytes
/
BubbleSort.c
File metadata and controls
38 lines (31 loc) · 688 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
#include <stdio.h>
void swap(int array[], int j)
{
int t = array[j];
array[j] = array[j + 1];
array[j + 1] = t;
}
void bubble_sort(int array[], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (array[j] > array[j + 1])
{
swap(array, j);
}
}
}
}
int main()
{
int array_size = 9;
int array[10] = {99, 33, 22, 10, 5, 7, 9, 0, 15, 27};
bubble_sort(array, array_size);
printf("Lista ordenada:\n");
for (int i = 0; i < array_size - 1; i++)
printf("%d, ", array[i]);
printf("%d\n", array[array_size - 1]);
return 0;
}