Created
February 23, 2020 07:59
-
-
Save vicegd/b6884cbcfa8e9288fe0dffb332364ef5 to your computer and use it in GitHub Desktop.
Sorting algorithm: Bubble method with sentinel
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 void sort(int[] elements) { | |
int i = 1; | |
boolean hasChange = true; | |
while (hasChange && (i < elements.length)){ | |
hasChange = false; | |
for (int j = elements.length - 1; j >= i; j--){ | |
if (elements[j-1] > elements[j]){ | |
int temp = elements[j]; | |
elements[j] = elements[j-1]; | |
elements[j-1] = temp; | |
hasChange = true; | |
} | |
} | |
i++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment