Created
June 2, 2022 09:27
-
-
Save gabrieleboccarusso/ebf83cc92a700bd28cd5e056ff986d0b to your computer and use it in GitHub Desktop.
snipper for the java version of bubble sort
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 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