Skip to content

Commit

Permalink
Fixed issue diptangsu#89 in C language by breaking the outer loop if …
Browse files Browse the repository at this point in the history
…the inner loop does not swap any element
  • Loading branch information
arathyrose committed Oct 31, 2019
1 parent e7b6ef5 commit 84445fe
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions C/BubbleSort.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#include <stdio.h>

void bubblesort(int a[], int size) {
for (int i = 1; i < size; i++) {
for (int j = 0; j < i; j++) {
for (int i = 0; i < size-1; i++) {
int swapped=0;
for (int j = 0; j < n-i-1; j++) {
if (a[j] > a[j+1]){
int tmp = a[j+1];
a[j+1] = a[j];
a[j] = tmp;
swapped=1;
}
}
if(swapped==0)
break;
}
}

Expand Down Expand Up @@ -36,4 +40,4 @@ int main ()
printf("%d ", (a[i]));
}
return 0;
}
}

0 comments on commit 84445fe

Please sign in to comment.