Quick actions

cmd+k|ctrl+k

Navigation

Languages

0318JavaBubbleSort

Snippet info

Language

Java

Visibility

public

Author

mhcrnl

Created

2018-09-27T09:36:56Z

Updated

2018-09-27T14:00:20Z

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World!");
        
        int[] arr ={3,1,56,89,94,25,34};
        
        int arrdim= arr.length;
        System.out.println(arrdim);
        
        BubbleSort bs = new BubbleSort();
        bs.printArray(arr);
        
        bs.bubbleSortCresc(arr);
        bs.printArray(arr);
    }
}

class BubbleSort {
    public void bubbleSortCresc(int[] arr) {
        boolean swapped = true;
        for (int i=0; i < arr.length-1; i++){
            swapped = false;
            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;
                    swapped = true;
                }
            }
            if (swapped == false) break ;
        }
    }
    
    public void printArray(int[] arr){
        for(int i=0; i<arr.length; i++){
            System.out.print(arr[i]+" ");
        }
        System.out.println();
    }
} 
INFO