From 425fa02f12efae5f165f71deec459927c19518f3 Mon Sep 17 00:00:00 2001 From: shodakenta <50577699+shodakenta@users.noreply.github.com> Date: Thu, 16 May 2019 08:58:51 +0900 Subject: [PATCH 1/9] update BubbleSort MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit メソッドを実装しました。 --- src/jp/co/cis/sort/BubbleSort.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/jp/co/cis/sort/BubbleSort.java b/src/jp/co/cis/sort/BubbleSort.java index 619c4da..92f76ce 100644 --- a/src/jp/co/cis/sort/BubbleSort.java +++ b/src/jp/co/cis/sort/BubbleSort.java @@ -13,6 +13,13 @@ public class BubbleSort { */ public int[] sort(int[] ary) { // TODO バブルソートで並び替える処理を実装してください。 + for(int i=1; i <= ary.length-1; i++) { //小さい数字を決める + for(int j=0; j < ary.length-i; j++) { //ary[j]とary[j+1]の大小比較 + if(ary[j] < ary[j+1]) { + // ary[j]とary[j+1]の順番を入れ替えたい + int x = ary[j+1]; + ary[j+1] = ary[j]; + ary[j] = x; return ary; } From a2f4ea615a0bd7fbd09853c4e68a735340e8d982 Mon Sep 17 00:00:00 2001 From: shodakenta <50577699+shodakenta@users.noreply.github.com> Date: Thu, 16 May 2019 17:40:02 +0900 Subject: [PATCH 2/9] Update BubbleSort MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 昇順に編集しました。 --- src/jp/co/cis/sort/BubbleSort.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/jp/co/cis/sort/BubbleSort.java b/src/jp/co/cis/sort/BubbleSort.java index 92f76ce..b4de583 100644 --- a/src/jp/co/cis/sort/BubbleSort.java +++ b/src/jp/co/cis/sort/BubbleSort.java @@ -13,14 +13,15 @@ public class BubbleSort { */ public int[] sort(int[] ary) { // TODO バブルソートで並び替える処理を実装してください。 - for(int i=1; i <= ary.length-1; i++) { //小さい数字を決める - for(int j=0; j < ary.length-i; j++) { //ary[j]とary[j+1]の大小比較 - if(ary[j] < ary[j+1]) { - // ary[j]とary[j+1]の順番を入れ替えたい - int x = ary[j+1]; - ary[j+1] = ary[j]; - ary[j] = x; + for(int i=1; i<= ary.length-1; i++) { + for(int j=0; j < ary.length-i; j++) { + if(ary[j] > ary[j+1]) { + int y = ary[j]; + ary[j] = ary[j+1]; + ary[j+1] = y; + } + } + } return ary; } - } From 222065e66155afdfde4bd84313b01c09917c6d92 Mon Sep 17 00:00:00 2001 From: shodakenta <50577699+shodakenta@users.noreply.github.com> Date: Thu, 23 May 2019 19:28:42 +0900 Subject: [PATCH 3/9] Update selectionsort MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 選択ソートのコードを記述しました。 --- src/jp/co/cis/sort/SelectionSort.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/jp/co/cis/sort/SelectionSort.java b/src/jp/co/cis/sort/SelectionSort.java index 1cbcbcb..5df39e9 100644 --- a/src/jp/co/cis/sort/SelectionSort.java +++ b/src/jp/co/cis/sort/SelectionSort.java @@ -13,6 +13,19 @@ public class SelectionSort { */ public int[] sort(int[] ary) { // TODO 選択ソートで並び替える処理を実装してください。 + for(int i = 0; i < ary.length-1; i++) { + //比較する配列のインデックス + int min = i; + //最小値を見つけ、入れ替える繰り返し + for(int j = i + 1; j < ary.length; j++) { + if(ary[min] > ary[j]) { + min = j; + } + } + int tmp = ary[i]; + ary[i] = ary[min]; + ary[min] = tmp; + } return ary; } From cbb8d2002985042a76afddb7bab91b9d40e2bf61 Mon Sep 17 00:00:00 2001 From: shodakenta <50577699+shodakenta@users.noreply.github.com> Date: Thu, 30 May 2019 08:37:27 +0900 Subject: [PATCH 4/9] InsertionSort MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit コード記述完了 --- src/jp/co/cis/sort/InsertionSort.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/jp/co/cis/sort/InsertionSort.java b/src/jp/co/cis/sort/InsertionSort.java index 58ffd8b..a39e69d 100644 --- a/src/jp/co/cis/sort/InsertionSort.java +++ b/src/jp/co/cis/sort/InsertionSort.java @@ -13,6 +13,15 @@ public class InsertionSort { */ public int[] sort(int[] ary) { // TODO 挿入ソートで並び替える処理を実装してください。 + for(int i = 1; i < ary.length-1; i++){ + int tmp = ary[i]; + for(int j = i; j > 0; j--){ + if(ary[j] > ary[j-1]){ + ary[j] = ary[j-1]; + ary[j-1] = tmp; + } + } + } return ary; } From 36b5ef7a7dc483226fef2d1a9a372a24b7fc8f73 Mon Sep 17 00:00:00 2001 From: shodakenta <50577699+shodakenta@users.noreply.github.com> Date: Thu, 30 May 2019 18:32:40 +0900 Subject: [PATCH 5/9] Update InsertionSort.java --- src/jp/co/cis/sort/InsertionSort.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/jp/co/cis/sort/InsertionSort.java b/src/jp/co/cis/sort/InsertionSort.java index a39e69d..117283b 100644 --- a/src/jp/co/cis/sort/InsertionSort.java +++ b/src/jp/co/cis/sort/InsertionSort.java @@ -13,9 +13,12 @@ public class InsertionSort { */ public int[] sort(int[] ary) { // TODO 挿入ソートで並び替える処理を実装してください。 + // 配列のインデックスをiとする for(int i = 1; i < ary.length-1; i++){ int tmp = ary[i]; + // ary[0]まで比較を繰り返す for(int j = i; j > 0; j--){ + //左辺 > 右辺のとき、入れ替える if(ary[j] > ary[j-1]){ ary[j] = ary[j-1]; ary[j-1] = tmp; From 7dcadd6a4c9ff10cdda8f5c4e5ec01c031edae3f Mon Sep 17 00:00:00 2001 From: shodakenta <50577699+shodakenta@users.noreply.github.com> Date: Thu, 30 May 2019 18:34:57 +0900 Subject: [PATCH 6/9] Update InsertionSort.java --- src/jp/co/cis/sort/InsertionSort.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/jp/co/cis/sort/InsertionSort.java b/src/jp/co/cis/sort/InsertionSort.java index 117283b..ff9a758 100644 --- a/src/jp/co/cis/sort/InsertionSort.java +++ b/src/jp/co/cis/sort/InsertionSort.java @@ -13,8 +13,9 @@ public class InsertionSort { */ public int[] sort(int[] ary) { // TODO 挿入ソートで並び替える処理を実装してください。 - // 配列のインデックスをiとする for(int i = 1; i < ary.length-1; i++){ + // 比較する配列のインデックス + int tmp = i int tmp = ary[i]; // ary[0]まで比較を繰り返す for(int j = i; j > 0; j--){ From c77f4be8e56165cea19bce47296713b096e3b729 Mon Sep 17 00:00:00 2001 From: shodakenta <50577699+shodakenta@users.noreply.github.com> Date: Thu, 30 May 2019 18:36:15 +0900 Subject: [PATCH 7/9] Update InsertionSort.java --- src/jp/co/cis/sort/InsertionSort.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/jp/co/cis/sort/InsertionSort.java b/src/jp/co/cis/sort/InsertionSort.java index ff9a758..e3aebc4 100644 --- a/src/jp/co/cis/sort/InsertionSort.java +++ b/src/jp/co/cis/sort/InsertionSort.java @@ -14,12 +14,11 @@ public class InsertionSort { public int[] sort(int[] ary) { // TODO 挿入ソートで並び替える処理を実装してください。 for(int i = 1; i < ary.length-1; i++){ - // 比較する配列のインデックス - int tmp = i + // 比較する配列のインデックスをiとする int tmp = ary[i]; // ary[0]まで比較を繰り返す for(int j = i; j > 0; j--){ - //左辺 > 右辺のとき、入れ替える + //(左辺 > 右辺)のとき、入れ替える if(ary[j] > ary[j-1]){ ary[j] = ary[j-1]; ary[j-1] = tmp; From 8811641f4a8cc56001cd0c82acfe546343081c47 Mon Sep 17 00:00:00 2001 From: shodakenta <50577699+shodakenta@users.noreply.github.com> Date: Mon, 10 Jun 2019 19:52:19 +0900 Subject: [PATCH 8/9] Update BinaryTree.java --- src/jp/co/cis/search/BinaryTree.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/jp/co/cis/search/BinaryTree.java b/src/jp/co/cis/search/BinaryTree.java index 5a462b2..996c493 100644 --- a/src/jp/co/cis/search/BinaryTree.java +++ b/src/jp/co/cis/search/BinaryTree.java @@ -13,7 +13,23 @@ public class BinaryTree { * @return */ public int search(int[] ary, int search) { - // TODO 二分木で探索する処理を実装してください。 + // 最小値のインデックス番号 + int low = 0; + // 最大値のインデックス番号 + int high = ary.length-1; + while(low <= high) { + //配列の中間の値のインデックス番号 + int mid = (low + high)/2; + //中間の要素 + int tmp = ary[mid]; + if(tmp == search) { + return mid; + }else if(tmp > search) { + high = mid - 1; + }else { + low = mid + 1; + } + } return 0; } From 12e33c19f4d8883af79fee83392d0dd369669d62 Mon Sep 17 00:00:00 2001 From: shodakenta <50577699+shodakenta@users.noreply.github.com> Date: Thu, 20 Jun 2019 19:24:49 +0900 Subject: [PATCH 9/9] Update Hanoi.java --- src/jp/co/cis/other/Hanoi.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/jp/co/cis/other/Hanoi.java b/src/jp/co/cis/other/Hanoi.java index 7bff639..d3c2a60 100644 --- a/src/jp/co/cis/other/Hanoi.java +++ b/src/jp/co/cis/other/Hanoi.java @@ -12,7 +12,7 @@ public static void main(String[] args) { */ public void exec() { - move(5, "START", "GOAL", "WORK"); + move(3, "START", "GOAL", "WORK"); } /** @@ -28,16 +28,13 @@ private void move(int enban, String from, String to, String work) { // 一番下でないなら続ける // (1)一番下以外を作業用の棒に移動させる - + move(enban-1, from, work, to); // (2)一番下をGOALに移動させる System.out.println(enban + "番の円盤を" + from + "から" + to + "に移動させる"); // (3)(1)で移動させた円盤を棒を入れ替えて移動させる - + move(enban-1, work, to, from); } - - - } }