LoginSignup
0
0

More than 3 years have passed since last update.

ArrayListを使ったバブルソート(JAVA)

Last updated at Posted at 2020-07-08
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //ArrayListのlistを宣言
        ArrayList<Integer> list = new ArrayList<Integer>();
        //countは繰り返し回数
        int count = 0;
        count = scanner.nextInt();

        //listに入力値を格納
        for (int i = 0; i < count; i++) {
            list.add(scanner.nextInt());
        }
         //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)) {
                    // 入れ替え
                    int tmp = list.get(j - 1);
                    list.set(j -1, list.get(j));
                    list.set(j, tmp);
                }
             }
        }
        //拡張for文
        for(int a: list) {
            System.out.println(a);
        }      
    }
}
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0