Skip to content

Instantly share code, notes, and snippets.

@vicegd
Created February 23, 2020 07:59
Show Gist options
  • Save vicegd/b6884cbcfa8e9288fe0dffb332364ef5 to your computer and use it in GitHub Desktop.
Save vicegd/b6884cbcfa8e9288fe0dffb332364ef5 to your computer and use it in GitHub Desktop.
Sorting algorithm: Bubble method with sentinel
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