Last active
July 28, 2020 10:33
-
-
Save dvt32/a122831b3b4bd9b6078ccbdf0cfd3e64 to your computer and use it in GitHub Desktop.
ALGORITHMO #14 - Bubble Sort Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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