Skip to content

Instantly share code, notes, and snippets.

@thmain
Created August 11, 2018 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thmain/27cbb20921da6fdf762f1fb3ceffb1a7 to your computer and use it in GitHub Desktop.
Save thmain/27cbb20921da6fdf762f1fb3ceffb1a7 to your computer and use it in GitHub Desktop.
import java.util.Arrays;
public class BubbleSort {
static void sort(int [] arrA){
if(arrA==null || arrA.length==0)
return;
System.out.println("Original Array: " + Arrays.toString(arrA));
int size = arrA.length;
for (int i = 0; i <size-1 ; i++) {
for (int j = 0; j <size-i-1 ; j++) {
//check the adjacent elements
if(arrA[j]>arrA[j+1]){
//swap the elements
int temp = arrA[j];
arrA[j] = arrA[j+1];
arrA[j+1] = temp;
}
}
}
System.out.println("Sorted Array: " + Arrays.toString(arrA));
}
public static void main(String[] args) {
int [] arrA = {5, 1, 9, 3, 2, 10};
sort(arrA);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment