Skip to content

Instantly share code, notes, and snippets.

@dvt32
Last active July 28, 2020 10:33
Show Gist options
  • Save dvt32/a122831b3b4bd9b6078ccbdf0cfd3e64 to your computer and use it in GitHub Desktop.
Save dvt32/a122831b3b4bd9b6078ccbdf0cfd3e64 to your computer and use it in GitHub Desktop.
ALGORITHMO #14 - Bubble Sort Implementation
public class BubbleSort {
public static void bubbleSort(int[] arr) {
boolean stillSwappingElements = true;
int j = 0;
while (stillSwappingElements) {
stillSwappingElements = false;
j++;
for (int i = 0; i < (arr.length - j); ++i) {
if (arr[i] > arr[i+1]) {
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
stillSwappingElements = true;
}
}
}
}
public static void main(String[] args) {
int[] arr = new int[] { 44, 55, 12, 42, 94, 18 };
bubbleSort(arr);
for (int element : arr) {
System.out.print(element + " ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment