第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();
    }
}
sort1の処理の流れ
繰り返し処理が終わった後の配列
Index→01234567891011
icandiate6293466127111443467
0116729346612711144346
146766934212711144346
236766349212711144346
3106766343421271114496
486766343414127112496
556766343414127112496
676766343414121172496
7106766343414121192476
8106766343414121197426
9116766343414121197624
10116766343414121197642
sort2の処理の流れ
繰り返し処理が終わった後の配列
01234567891011
ij6293466127111443467
1202693466127111443467
1252693412667111443467
1262693412766111443467
1272693412711661443467
1282693412711146643467
1292693412711144663467
12102693412711144346667
11*2691271114434346667
10*2697111241434346667
9*2697114121434346667
8*2697411121434346667
7*2674911121434346667
6*2647911121434346667
5*2467911121434346667
4*2467911121434346667
3*2467911121434346667
2*2467911121434346667
search2で検索する値 = 9
startendindex0123456789101112
01262467911[12]143439666789
05224[6]791112143439666789
3542467[9]1112143439666789
search2で検索する値? = 7
startendindex0123456789101112
01262467911[12]143439666789
05224[6]791112143439666789
3542467[9]1112143439666789
443246[7]91112143439666789
search2で検索する値? = 89
startendindex0123456789101112
01262467911[12]143439666789
71292467911121434[39]666789
10121124679111214343966[67]89
1212122467911121434396667[89]
search2で検索する値? = 87
startendindex0123456789101112
01262467911[12]143439666789
71292467911121434[39]666789
10121124679111214343966[67]89
1212122467911121434396667[89]
1312X