My favorite sort
public class BubbleSort{
public void bubbleSort(int arr[]){
for (int i = 0; i < arr.length - 1; i++){
for (int j = 0; j < arr.length - i - 1; j++){
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public void toString(int arr[]){
for(int i = 0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String[] args){
int[] ll = { 5, 1, 4, 3, 9, 7};
BubbleSort b = new BubbleSort();
b.bubbleSort(ll);
b.toString(ll);
}
}
BubbleSort.main(null);
A bubble sort basically compares each variable with each other. Although the time complexity/Big O is bad being O(n^2), I believe it to be the simplest sort to create