LoginSignup
0
0

More than 3 years have passed since last update.

java 挿入ソート

Posted at

java 挿入ソート

今回は挿入ソートについてコードを書いていく。
※自分のアウトプット用の記事です。間違いなどがあったら指摘してください。

InsetionSort.java
//トランプの手札の並び替えに似てる気がする。
public class InsertionSort {
    public static void sort(int[] array) {
        for(int i=1;i<array.length;i++) {
            int j=i;
            while(j>=1 && array[j-1]>array[j]) {
                int temp = array[j];
                array[j] = array[j-1];
                array[j-1] = temp;
                j--;
            }
        }
    }

    public static void main(String args[]) {
        int[] array = {3,2,4,5,1};
        sort(array);
        for(int i=0;i<array.length;i++) {
            System.out.print(array[i]);
        }
    }
}

次回はシェルソートを試してみようと思います。

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