Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@suraimu

java 挿入ソート

More than 1 year has passed since last update.

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
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
suraimu
現在(2020/02/06)大学2年生のものです。なかなか初心者を抜け出せないのでそろそろ抜け出したいです!笑

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?