第9回 - 2012/11/16
以下のプログラムを入力して実行できるようにすること。プロジェクト名はなんでもいい。ソースファイルを用意するときには、 パッケージ名はweek9、クラス名はAlgorithmという名称にしておくこと。
package week9;
public class Algorithm {
public static void main(String[] args) {
int c[] = { 6, 2, 9, 34, 66, 12, 7, 11, 14, 4, 34, 67 };
printArray(sort1(c));
int e[] = { 6, 2, 9, 34, 66, 12, 7, 11, 14, 4, 34, 67 };
printArray(sort2(e));
int d[] = { 2, 4, 6, 7, 9, 11, 12, 14, 34, 39, 66, 67, 89 };
System.out.println(search2(d, 2));
System.out.println(search2(d, 4));
System.out.println(search2(d, 6));
System.out.println(search2(d, 7));
System.out.println(search2(d, 9));
System.out.println(search2(d, 11));
System.out.println(search2(d, 12));
System.out.println(search2(d, 14));
System.out.println(search2(d, 34));
System.out.println(search2(d, 66));
System.out.println(search2(d, 67));
System.out.println(search2(d, 5));
System.out.println(search2(d, 21));
System.out.println(search2(d, 167));
}
static int[] sort1(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
int candidate = i;
for (int j = i + 1; j < array.length; j++) {
if (array[candidate] < array[j]) {
candidate = j;
}
}
int temp = array[candidate];
array[candidate] = array[i];
array[i] = temp;
}
return array;
}
static int[] sort2(int[] array) {
for (int i = array.length ; i > 1; i--) {
for (int j = 0; j < i - 1; j++) {
if (array[j+1] < array[j]) {
int temp = array[j+1];
array[j+1] = array[j];
array[j] = temp;
}
}
}
return array;
}
static int search1(int[] array, int q) {
for (int i = 0; i < array.length; i++) {
if (array[i] == q) {
return i;
}
}
return -1;
}
static int search2(int[] array, int q) {
int index;
int start = 0, end = array.length - 1;
while (start <= end) {
index = ( start + end ) / 2;
if (array[index] == q) {
return index;
} else if (array[index] < q) {
start = index + 1;
} else {
end = index - 1;
}
}
return -1;
}
static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0) {
System.out.print(" ,");
}
System.out.print(array[i]);
}
System.out.println();
}
}
繰り返し処理が終わった後の配列 | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Index→ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | |
i | candiate | 6 | 2 | 9 | 34 | 66 | 12 | 7 | 11 | 14 | 4 | 34 | 67 |
0 | 11 | 67 | 2 | 9 | 34 | 66 | 12 | 7 | 11 | 14 | 4 | 34 | 6 |
1 | 4 | 67 | 66 | 9 | 34 | 2 | 12 | 7 | 11 | 14 | 4 | 34 | 6 |
2 | 3 | 67 | 66 | 34 | 9 | 2 | 12 | 7 | 11 | 14 | 4 | 34 | 6 |
3 | 10 | 67 | 66 | 34 | 34 | 2 | 12 | 7 | 11 | 14 | 4 | 9 | 6 |
4 | 8 | 67 | 66 | 34 | 34 | 14 | 12 | 7 | 11 | 2 | 4 | 9 | 6 |
5 | 5 | 67 | 66 | 34 | 34 | 14 | 12 | 7 | 11 | 2 | 4 | 9 | 6 |
6 | 7 | 67 | 66 | 34 | 34 | 14 | 12 | 11 | 7 | 2 | 4 | 9 | 6 |
7 | 10 | 67 | 66 | 34 | 34 | 14 | 12 | 11 | 9 | 2 | 4 | 7 | 6 |
8 | 10 | 67 | 66 | 34 | 34 | 14 | 12 | 11 | 9 | 7 | 4 | 2 | 6 |
9 | 11 | 67 | 66 | 34 | 34 | 14 | 12 | 11 | 9 | 7 | 6 | 2 | 4 |
10 | 11 | 67 | 66 | 34 | 34 | 14 | 12 | 11 | 9 | 7 | 6 | 4 | 2 |
繰り返し処理が終わった後の配列 | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | ||
i | j | 6 | 2 | 9 | 34 | 66 | 12 | 7 | 11 | 14 | 4 | 34 | 67 |
12 | 0 | 2 | 6 | 9 | 34 | 66 | 12 | 7 | 11 | 14 | 4 | 34 | 67 |
12 | 5 | 2 | 6 | 9 | 34 | 12 | 66 | 7 | 11 | 14 | 4 | 34 | 67 |
12 | 6 | 2 | 6 | 9 | 34 | 12 | 7 | 66 | 11 | 14 | 4 | 34 | 67 |
12 | 7 | 2 | 6 | 9 | 34 | 12 | 7 | 11 | 66 | 14 | 4 | 34 | 67 |
12 | 8 | 2 | 6 | 9 | 34 | 12 | 7 | 11 | 14 | 66 | 4 | 34 | 67 |
12 | 9 | 2 | 6 | 9 | 34 | 12 | 7 | 11 | 14 | 4 | 66 | 34 | 67 |
12 | 10 | 2 | 6 | 9 | 34 | 12 | 7 | 11 | 14 | 4 | 34 | 66 | 67 |
11 | * | 2 | 6 | 9 | 12 | 7 | 11 | 14 | 4 | 34 | 34 | 66 | 67 |
10 | * | 2 | 6 | 9 | 7 | 11 | 12 | 4 | 14 | 34 | 34 | 66 | 67 |
9 | * | 2 | 6 | 9 | 7 | 11 | 4 | 12 | 14 | 34 | 34 | 66 | 67 |
8 | * | 2 | 6 | 9 | 7 | 4 | 11 | 12 | 14 | 34 | 34 | 66 | 67 |
7 | * | 2 | 6 | 7 | 4 | 9 | 11 | 12 | 14 | 34 | 34 | 66 | 67 |
6 | * | 2 | 6 | 4 | 7 | 9 | 11 | 12 | 14 | 34 | 34 | 66 | 67 |
5 | * | 2 | 4 | 6 | 7 | 9 | 11 | 12 | 14 | 34 | 34 | 66 | 67 |
4 | * | 2 | 4 | 6 | 7 | 9 | 11 | 12 | 14 | 34 | 34 | 66 | 67 |
3 | * | 2 | 4 | 6 | 7 | 9 | 11 | 12 | 14 | 34 | 34 | 66 | 67 |
2 | * | 2 | 4 | 6 | 7 | 9 | 11 | 12 | 14 | 34 | 34 | 66 | 67 |
start | end | index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 12 | 6 | 2 | 4 | 6 | 7 | 9 | 11 | [12] | 14 | 34 | 39 | 66 | 67 | 89 |
0 | 5 | 2 | 2 | 4 | [6] | 7 | 9 | 11 | 12 | 14 | 34 | 39 | 66 | 67 | 89 |
3 | 5 | 4 | 2 | 4 | 6 | 7 | [9] | 11 | 12 | 14 | 34 | 39 | 66 | 67 | 89 |
start | end | index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 12 | 6 | 2 | 4 | 6 | 7 | 9 | 11 | [12] | 14 | 34 | 39 | 66 | 67 | 89 |
0 | 5 | 2 | 2 | 4 | [6] | 7 | 9 | 11 | 12 | 14 | 34 | 39 | 66 | 67 | 89 |
3 | 5 | 4 | 2 | 4 | 6 | 7 | [9] | 11 | 12 | 14 | 34 | 39 | 66 | 67 | 89 |
4 | 4 | 3 | 2 | 4 | 6 | [7] | 9 | 11 | 12 | 14 | 34 | 39 | 66 | 67 | 89 |
start | end | index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 12 | 6 | 2 | 4 | 6 | 7 | 9 | 11 | [12] | 14 | 34 | 39 | 66 | 67 | 89 |
7 | 12 | 9 | 2 | 4 | 6 | 7 | 9 | 11 | 12 | 14 | 34 | [39] | 66 | 67 | 89 |
10 | 12 | 11 | 2 | 4 | 6 | 7 | 9 | 11 | 12 | 14 | 34 | 39 | 66 | [67] | 89 |
12 | 12 | 12 | 2 | 4 | 6 | 7 | 9 | 11 | 12 | 14 | 34 | 39 | 66 | 67 | [89] |
start | end | index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 12 | 6 | 2 | 4 | 6 | 7 | 9 | 11 | [12] | 14 | 34 | 39 | 66 | 67 | 89 |
7 | 12 | 9 | 2 | 4 | 6 | 7 | 9 | 11 | 12 | 14 | 34 | [39] | 66 | 67 | 89 |
10 | 12 | 11 | 2 | 4 | 6 | 7 | 9 | 11 | 12 | 14 | 34 | 39 | 66 | [67] | 89 |
12 | 12 | 12 | 2 | 4 | 6 | 7 | 9 | 11 | 12 | 14 | 34 | 39 | 66 | 67 | [89] |
13 | 12 | X |