Skip to content

Instantly share code, notes, and snippets.

@apremalal
Created February 18, 2013 06:15
Show Gist options
  • Save apremalal/4975404 to your computer and use it in GitHub Desktop.
Save apremalal/4975404 to your computer and use it in GitHub Desktop.
Bubble sort algorithm implementation in java
public class BubbleSort {
public static void main(String args[]) {
int a[] = { 2, 3, 6, 1, 9, 2, 10, 5 };
int sorted[] = buble(a);
for (int x : sorted) {
System.out.print(x + " ");
}
}
public static int[] buble(int a[]) {
boolean swapped;
int temp;
do {
swapped = false;
for (int i = 0; i < a.length - 1; i++) {
if (a[i] > a[i + 1]) {
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
swapped = true;
}
}
} while (swapped);
return a;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment