Skip to content

Instantly share code, notes, and snippets.

@gabrieleboccarusso
Created June 2, 2022 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrieleboccarusso/ebf83cc92a700bd28cd5e056ff986d0b to your computer and use it in GitHub Desktop.
Save gabrieleboccarusso/ebf83cc92a700bd28cd5e056ff986d0b to your computer and use it in GitHub Desktop.
snipper for the java version of bubble sort
public class Main
{
public static void main(String[] args) {
int[] arr = {1,7,56,0,34,999};
boolean swapped = true;
int i;
while (swapped) {
swapped = false;
for (i = 0; i < arr.length - 1; ++i) {
if (arr[i] > arr[i + 1]) {
swapped = true;
arr[i] = arr[i] + arr[i + 1];
arr[i + 1] = arr[i] - arr[i + 1];
arr[i] = arr[i] - arr[i + 1];
}
}
}
for (int n : arr) {
System.out.print(n + " ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment