1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

初心者がJavaで初めての「成果物」

Posted at

Javaでソート修行してみた

Javaでの初ビッグプロジェクトはバブルソートを完成させることだった。forループを二重に使う邪道な手法は避け、Whileとforを組み合わせて完成させた超大作だ。初心者ならソートをやれば力はつくと思うが、写経はやめて自分で考えたやり方でプログラムを作るべきだ。自分の頭でソートが完成できたら、その先なんでも応用が利き、難しい壁にぶち当たっても乗り越えられる素養が得られるはずだ。

Bubble Sort in Java
  public void bubbleSort(){

       int[] numbers = {4,7,2,9,33,6,5,8,3,43,1,88,0,21};
       int iLen = numbers.length;
       int iLarge = 0;
       int totalComparison = 0;
       int iLen1 = 0;

       System.out.print("Before sort : ");

       for(int y = 0; y< iLen; y++){

           System.out.printf("%d,", numbers[y]);
   
        }
        System.out.printf("\n");
       
       while(iLen1 < iLen){

            for(int i = 0 ; i < iLen - 1 - iLen1; i ++){

                if(numbers[i] > numbers[i + 1]){

               iLarge = numbers[i];
               numbers[i] = numbers[i + 1];
               numbers[i + 1] = iLarge;

               totalComparison++;

           }

       }

       iLen1++;

       }

       System.out.printf("Total comparizon : %d \nAfter sort : ", totalComparison);
       for(int y = 0; y< iLen; y++){

       System.out.printf("%d,", numbers[y]);

    }

   }
   
1
0
0

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?