forked from yashhere/git-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubbleSort.c
44 lines (33 loc) · 807 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
/*********
Author1 Name:
Author2 Name:
FIXED THE BUGS FOR GIT-WORKSHOP
**********/
#include<stdio.h>
void bubble_sort(int[], int);
void main() {
int arr[30], num, i;
printf("Enter no of elements :");
scanf("%d", &num);
printf("Enter array elements :");
for (i = 0; i < num; i++)
scanf("%d", &arr[i]);
bubble_sort(arr, num);
printf("\n");
}
// HERE IS THE BUG, IN THE INNER LOOP, CHANGE j < num to j < num-1
void bubble_sort(int iarr[], int num) {
int i, j, k, temp;
for (i = 1; i < num; i++) {
for (j = 0; j < num-1; j++) {
if (iarr[j] > iarr[j + 1]) {
temp = iarr[j];
iarr[j] = iarr[j + 1];
iarr[j + 1] = temp;
}
}
}
for (k = 0; k < num; k++) {
printf("%5d", iarr[k]);
}
}