-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble_sort.c
78 lines (62 loc) · 1.23 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// function prototypes
void print( int* list, int size);
void bubbleSort(int* list, int size);
// main
int main() {
// initialize vars
int* list = (int*)malloc(10);
int i;
srand(time(NULL));
// create list
for(i =0; i<10;i++)
{
list[i] = rand() % 10 + 1;
}
print(list, 10);
// bubble sort
bubbleSort( list, 10 );
printf( "Sorted " );
print(list, 10);
!!C
// return
return 0;
}
// fxn imp
void bubbleSort(int* list, int size){
// initialize vars
int i,j;
int temp;
int swapped;
// loop through list
for( i = 0; i < size; i++)
{
// swapped is false
swapped = 0;
// loop through list
for( j = 0; j < size - 1; j++)
{
// if smaller, swap
if( list[j+1] < list[j])
{
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
swapped = 1;
}
}
// if swapped is false, break
if( swapped == 0)
{
break;
}
}
}
void print( int* list, int size ){
int i;
printf("List is: ");
for(i =0; i < size; i++)
{
printf( "%d ", list[i] );
}
printf("\n");
}