Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/jp/co/cis/other/Hanoi.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static void main(String[] args) {
*/
public void exec() {

move(5, "START", "GOAL", "WORK");
move(3, "START", "GOAL", "WORK");
}

/**
Expand All @@ -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);
}



}
}
18 changes: 17 additions & 1 deletion src/jp/co/cis/search/BinaryTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
10 changes: 9 additions & 1 deletion src/jp/co/cis/sort/BubbleSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +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++) {
if(ary[j] > ary[j+1]) {
int y = ary[j];
ary[j] = ary[j+1];
ary[j+1] = y;
}
}
}
return ary;
}

}
12 changes: 12 additions & 0 deletions src/jp/co/cis/sort/InsertionSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ public class InsertionSort {
*/
public int[] sort(int[] ary) {
// TODO 挿入ソートで並び替える処理を実装してください。
for(int i = 1; i < ary.length-1; 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;
}
}
}
return ary;
}

Expand Down
13 changes: 13 additions & 0 deletions src/jp/co/cis/sort/SelectionSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down