From 8504ed82e254f020f1b5b07eacb8530437f14324 Mon Sep 17 00:00:00 2001 From: "kou.ukai" Date: Fri, 17 May 2019 18:11:42 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=E9=81=B8=E6=8A=9E=E3=82=BD=E3=83=BC?= =?UTF-8?q?=E3=83=88=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/jp/co/cis/Main.java | 12 ++++++++---- src/jp/co/cis/sort/SelectionSort.java | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 src/jp/co/cis/sort/SelectionSort.java diff --git a/src/jp/co/cis/Main.java b/src/jp/co/cis/Main.java index 4cbf276..3009320 100644 --- a/src/jp/co/cis/Main.java +++ b/src/jp/co/cis/Main.java @@ -1,6 +1,6 @@ package jp.co.cis; -import jp.co.cis.sort.BubbleSort; +import jp.co.cis.sort.SelectionSort; public class Main { @@ -9,7 +9,6 @@ public class Main { * @param args */ public static void main(String[] args) { - BubbleSort bubbleSort = new BubbleSort(); // 並び替え対象配列生成 int in[] = createArray(); @@ -18,8 +17,13 @@ public static void main(String[] args) { System.out.println("before---------"); print(in); - // 並び替え - int[] out = bubbleSort.sort(in); + // バブルソート +// BubbleSort bubbleSort = new BubbleSort(); +// int[] out = bubbleSort.sort(in); + + // 選択ソート + SelectionSort sort = new SelectionSort(); + int[] out = sort.sort(in); // 変更後表示 System.out.println("after---------"); diff --git a/src/jp/co/cis/sort/SelectionSort.java b/src/jp/co/cis/sort/SelectionSort.java new file mode 100644 index 0000000..1cbcbcb --- /dev/null +++ b/src/jp/co/cis/sort/SelectionSort.java @@ -0,0 +1,19 @@ +package jp.co.cis.sort; + +/** + * 選択ソートで配列の値を並び替えるクラスです。 + * + */ +public class SelectionSort { + + /** + * 配列の値を小さい順に並び替えた結果を返却します。 + * @param ary + * @return + */ + public int[] sort(int[] ary) { + // TODO 選択ソートで並び替える処理を実装してください。 + return ary; + } + +} From ac19af2577de4a72777b087376631c984c3c07cc Mon Sep 17 00:00:00 2001 From: AotoTakeyama <50577559+AotoTakeyama@users.noreply.github.com> Date: Fri, 24 May 2019 10:31:00 +0900 Subject: [PATCH 2/3] update SelectionSort.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sortメソッドを実装 --- src/jp/co/cis/sort/BubbleSort.java | 47 ++++++++++++++++++------------ 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/src/jp/co/cis/sort/BubbleSort.java b/src/jp/co/cis/sort/BubbleSort.java index e57fd39..c69f478 100644 --- a/src/jp/co/cis/sort/BubbleSort.java +++ b/src/jp/co/cis/sort/BubbleSort.java @@ -1,10 +1,10 @@ package jp.co.cis.sort; /** - * バブルソートで並び替えるクラスです。 + * 選択ソートで配列の値を並び替えるクラスです。 * */ -public class BubbleSort { +public class SelectionSort { /** * 配列の値を小さい順に並び替えた結果を返却します。 @@ -12,23 +12,34 @@ public class BubbleSort { * @return */ public int[] sort(int[] ary) { - - /** - * aryLengthで配列の長さを取得 - * changeNumは配列の入れ替えをする回数を表します - */ - int aryLength = ary.length; - int changeNum = aryLength - 1; - for(int i=changeNum; i>0; i--){ - for(int j=0; jary[j+1]){ - int temNum = ary[j+1]; - ary[j+1] = ary[j]; - ary[j] = temNum; - } + //受け取る配列の長さ + int aryLength = ary.length; + //最大値を探す処理をする回数 + int changeNum = aryLength - 1; + + for (int i = 1; i <= changeNum; i++) { + //左端(最大値を入れる場所)のindex番号 + int tempAryIndex = i - 1; + //最大値があるindex番号を受け取る + int maxAryIndex = i - 1; + //最大値を受け取る + int maxAryNum = ary[i - 1]; + + //最大値をもつindex番号を調べる + for (int j = tempAryIndex; j <= changeNum; j++) { + if (maxAryNum < ary[j]) { + maxAryNum = ary[j]; + maxAryIndex = j; + } + } + + //左端と最大値を持つ配列要素を入れ替える + int tempNum = ary[tempAryIndex]; + ary[tempAryIndex] = ary[maxAryIndex]; + ary[maxAryIndex] = tempNum; } - } - return ary; + + return ary; } } From 071cbb4214b300fb382ec1396efb18c20d5384a8 Mon Sep 17 00:00:00 2001 From: AotoTakeyama <50577559+AotoTakeyama@users.noreply.github.com> Date: Fri, 24 May 2019 10:32:49 +0900 Subject: [PATCH 3/3] Update SelectionSort.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sortメソッドの実装 --- src/jp/co/cis/sort/SelectionSort.java | 28 ++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/jp/co/cis/sort/SelectionSort.java b/src/jp/co/cis/sort/SelectionSort.java index 1cbcbcb..c69f478 100644 --- a/src/jp/co/cis/sort/SelectionSort.java +++ b/src/jp/co/cis/sort/SelectionSort.java @@ -12,7 +12,33 @@ public class SelectionSort { * @return */ public int[] sort(int[] ary) { - // TODO 選択ソートで並び替える処理を実装してください。 + //受け取る配列の長さ + int aryLength = ary.length; + //最大値を探す処理をする回数 + int changeNum = aryLength - 1; + + for (int i = 1; i <= changeNum; i++) { + //左端(最大値を入れる場所)のindex番号 + int tempAryIndex = i - 1; + //最大値があるindex番号を受け取る + int maxAryIndex = i - 1; + //最大値を受け取る + int maxAryNum = ary[i - 1]; + + //最大値をもつindex番号を調べる + for (int j = tempAryIndex; j <= changeNum; j++) { + if (maxAryNum < ary[j]) { + maxAryNum = ary[j]; + maxAryIndex = j; + } + } + + //左端と最大値を持つ配列要素を入れ替える + int tempNum = ary[tempAryIndex]; + ary[tempAryIndex] = ary[maxAryIndex]; + ary[maxAryIndex] = tempNum; + } + return ary; }