[Java] Bubble sort using ArrayList (JAVA)

less than 1 minute read

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // declare a list of ArrayList
        ArrayList<Integer> list = new ArrayList<Integer>();
        //count is the number of repetitions
        int count = 0;
        count = scanner.nextInt();
        
        // Store the input value in list
        for (int i = 0; i <count; i++) {
            list.add(scanner.nextInt());
        }
         // Bubble sort of ArrayList
         for (int i = 0; i <list.size()-1; i++) {
            for (int j = list.size()-1; j> i; j--) {
                if (array.get(j-1)> list.get(j)) {
                    // swap
                    int tmp = list.get(j-1);
                    list.set(j -1, list.get(j));
                    list.set(j, tmp);
                }
             }
        }
        // extended for statement
        for(int a: list) {
            System.out.println(a);
        }
    }
}

Tags:

Updated: