-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble_sort .c
More file actions
55 lines (46 loc) · 1.05 KB
/
bubble_sort .c
File metadata and controls
55 lines (46 loc) · 1.05 KB
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
50
51
52
53
54
55
#include <stdio.h>
// sorting function prototype
int bubble_sort(int data[], int data_size);
int main(void)
{
// Get usorted data set
int data_set[] = {2, 1, 3, 10, 5, 8, 6, 7, 11, 9, 4, 0};
int data_size = 10;
printf("Before: ");
for (int i = 0; i < data_size; i++)
{
printf("%i,", data_set[i]);
}
printf("\n");
//call a sorting function
bubble_sort(data_set, data_size);
// print res - sorted data set
printf("After: ");
for (int i = 0; i < data_size; i++)
{
printf("%i,", data_set[i]);
}
printf("\n");
return 0;
}
// sorting function
int bubble_sort(int data[], int data_size)
{
int swap_count = -1;
int container = 0;
while (swap_count != 0)
{
swap_count = 0;
for (int i = 0; i < data_size - 1; i++)
{
if (data[i] > data[i+1])
{
container = data[i];
data[i] = data[i+1];
data[i+1] = container;
swap_count++;
}
}
}
return 0;
}