-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubblesort.c
45 lines (39 loc) · 939 Bytes
/
bubblesort.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
#include <stdio.h>
/*
Function : displayList
Input : (pointer to integer array, size of array)
Output : Displays the whole array
*/
void displayList(int *a, int size)
{
int ctr;
printf("List is : ");
for(ctr = 0; ctr < size; ctr++)
printf("%d\t", *(a + ctr));
printf("\n");
}
int main()
{
// initialize an array of integers
int list[] = {12, 20, 11, 5, 6, 8};
// calculate size of array
int size = sizeof(list) / sizeof(int);
int i, j; // loop counters
int temp; // variable to perform swapping
displayList(list, size);
// Bubble Sort
// Pass over the list (size - 1) times
for(i = 0; i < size; i++) {
// in every pass, compare all adjacent items
// swap them if in wrong order
for(j = 0; j < size - 1; j++) {
if(list[j] > list[j + 1]) {
temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
}
displayList(list, size);
return 0;
}