柴田先生...分かりません。

phpを書かなきゃだけど。
こっちもやらなきゃやらないですから。
ああああ!もう!
ともかく、バブルソートから。ここから。
なんかもう、自分のレベルが低すぎてイヤになるけどな!

明解 Javaによるアルゴリズムとデータ構造

明解 Javaによるアルゴリズムとデータ構造


をすすめてみる。基礎辺。

/**
 *
 */
package buble;

import java.util.Random;
/**
 * @author seijiro
 *
 */
public class BubbleSort {

	/**
	 * 入れ替えます。
	 *
	 * @param a
	 * @param idx1
	 * @param idx2
	 */
	static void swap(int[] a, int idx1, int idx2) {
		int t = a[idx1];
		a[idx1] = a[idx2];
		a[idx2] = t;
	}

	/**
	 * 配列を表示する。
	 *
	 * @param a
	 */
	static void printArray(int[] a) {
		for (int i : a) {
			System.out.print(i + "\t");
		}
		System.out.println("");
	}

	/**
	 * ランダムで配列作る。
	 * 
	 * @param count
	 * @return
	 */
	static int[] makeRandArray(int count) {
		Random rand = new Random();
		int[] a = new int[count];
		for (int i = 0; count > i; i++) {
			a[i] = rand.nextInt(count);
		}
		// System.out.println("格納完了");
		return a.clone();
	}

	/**
	 * バブルソート1
	 * @param a
	 * @return
	 */
	static int bubbleSort1(int[] a) {
		int count = 0;
		for (int i = 0; a.length - 1 > i; i++) {
			// System.out.print("i:" + i + ":");
			// System.out.print(i + "\t");
			for (int j = a.length - 1; j > i; j--) {
				// System.out.print("j:" + j + "\t");
				if (a[j - 1] > a[j]) {
					swap(a, j - 1, j);
					count++;
				}
			}
			// System.out.println();
		}
		// System.out.println("交換回数:" + count + "\t");
		return count;
	}

	/**
	 * バブルソート2 ブレーク付き
	 * 
	 * @param a
	 * @return
	 */
	static int bubbleSort2(int[] a) {
		int count = 0;
		for (int i = 0; a.length - 1 > i; i++) {
			//System.out.print("i:" + i + ":");
			int exchange = 0;
			// System.out.print(i + "\t");
			for (int j = a.length - 1; j > i; j--) {
//				System.out.print("j:" + j + "\t");
				if (a[j - 1] > a[j]) {
					swap(a, j - 1, j);
					count++;
					exchange++;
				}
			}
			if (exchange == 0) {
				// System.out.print("break! ");
				break;
			}
			//System.out.println();
		}
		// System.out.println("交換回数:" + count + "\t");
		return count;
	}

	/**
	 * バブルソート3
	 * 
	 * @param a
	 * @return
	 */
	static int bubbleSort3(int[] a) {
		int count = 0;
		int k = 0;
		while (a.length - 1 > k) {
			int last = a.length - 1;
			for (int j = a.length - 1; j > k; j--) {
				if (a[j - 1] > a[j]) {
					swap(a, j - 1, j);
					count++;
					last = j;
				}
			}
			k = last;
		}
		// System.out.println("交換回数:" + count + "\t");
		return count;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("バブルソート1");
		int sum1 = 0;
		int sum2 = 0;
		int sum3 = 0;
		int kaisuu = 200;//ループ回数
		int limit = 177;//ランダム種
		// ループしてみよう
		for (int i = 0; kaisuu > i; i++) {
			int[] ret = doBubbleSort(limit);
			sum1 += ret[0];
			sum2 += ret[1];
			sum3 += ret[2];
		}
		System.out.println("平均(ブレーク無し):" + sum1 / (double) kaisuu);
		System.out.println("平均(ブレーク付き):" + sum2 / (double) kaisuu);
		System.out.println("平均(改良版):" + sum3 / (double) kaisuu);
	}

	/**
	 * バブルソートをキックする
	 * 
	 * @param count
	 * @return
	 */
	static int[] doBubbleSort(int count) {
		int[] t1 = makeRandArray(count);
		int[] t2 = t1.clone();
		int[] t3 = t1.clone();
		if (t2.equals(t3))
			System.out.println("コレはおかしい");
		// 結果用
		int b = 0;
		int c = 0;
		int d = 0;
		b = bubbleSort1(t1);// 単純
		c = bubbleSort2(t2);// ブレーク
		d = bubbleSort3(t3);// 改良版
		System.out.println("b:c:d" + b + " " + c + " " + d);
		// 配列で返しましょう
		int[] ret = { b, c, d };
		return ret.clone();
	}
}

平均(ブレーク無し):7753.285
平均(ブレーク付き):7753.285
平均(改良版):7753.285

結果が変わらない。。。
なぜ?
分かってないから分からないんだろうなぁ。
悔しいなぁ

単純選択ソートがしっくり

わかりやすい。端緒!端緒!

/**
 *
 */
package sort;

import java.util.Random;

/**
 * @author seijiro
 *
 */
public class SortTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("走査回数をチェックする");
		System.out.println("単純交換ソートStraightExchangeSort");
		System.out.println("単純選択ソートStraightSelectionSort");
		System.out.println("単純選択ソートShuttleSort(insertionSort)");
		int sum1 = 0;
		int sum2 = 0;
		int sum3 = 0;
		int kaisuu = 200;// ループ回数
		int limit = 177;// ランダム種
		// ループしてみよう
		for (int i = 0; kaisuu > i; i++) {
			int[] ret = doBubbleSort(limit);
			sum1 += ret[0];
			sum2 += ret[1];
			sum3 += ret[2];
		}
		System.out.println("平均StraightExchangeSort:" + sum1 / (double) kaisuu);
		System.out.println("平均StraightSelectionSort:" + sum2 / (double) kaisuu);
		System.out.println("平均ShuttleSort(カウントで判断するのは意味がないが..):" + sum3 / (double) kaisuu);

	}

	/**
	 * ソートをキックする
	 * 
	 * @param count
	 * @return
	 */
	static int[] doBubbleSort(int count) {
		int[] t1 = makeRandArray(count);
		int[] t2 = t1.clone();
		int[] t3 = t1.clone();
		if (t2.equals(t3))
			System.out.println("コレはおかしい");
		// 結果用
		int b = 0;
		int c = 0;
		int d = 0;
		b = bubbleSort1(t1);
		c = selectionSort(t2);
		d = shuttleSort(t3);
		// System.out.println("b:c:d" + b + " " + c + " " + d);
		// 配列で返しましょう
		int[] ret = { b, c, d };
		return ret.clone();
	}

	/**
	 * 配列を表示する。
	 * 
	 * @param a
	 */
	static void printArray(int[] a) {
		for (int i : a) {
			System.out.print(i + "\t");
		}
		System.out.println("");
	}

	/**
	 * ランダムで配列作る。
	 * 
	 * @param count
	 * @return
	 */
	static int[] makeRandArray(int count) {
		Random rand = new Random();
		int[] a = new int[count];
		for (int i = 0; count > i; i++) {
			a[i] = rand.nextInt(count);
		}
		// System.out.println("格納完了");
		return a.clone();
	}

	/**
	 * 入れ替えます。
	 * 
	 * @param a
	 * @param idx1
	 * @param idx2
	 */
	static void swap(int[] a, int idx1, int idx2) {
		int t = a[idx1];
		a[idx1] = a[idx2];
		a[idx2] = t;
	}

	/**
	 * バブルソート1
	 * 
	 * @param a
	 * @return
	 */
	static int bubbleSort1(int[] a) {
		int count = 0;
		for (int i = 0; a.length - 1 > i; i++) {
			// System.out.print("i:" + i + ":");
			// System.out.print(i + "\t");
			for (int j = a.length - 1; j > i; j--) {
				count++;
				// System.out.print("j:" + j + "\t");
				if (a[j - 1] > a[j]) {
					swap(a, j - 1, j);
			
				}
			}
			// System.out.println();
		}
		// System.out.println("交換回数:" + count + "\t");
		return count;
	}

	/**
	 * セレクションソート
	 * 
	 * @param a
	 * @return
	 */
	static int selectionSort(int[] a) {
		int count = 0;
		for (int i = 0; a.length - 1 > i; i++) {
			int min = i;
			for (int j = i + 1; a.length > j; j++) {
				count++;
				if (a[j] < a[min]) {
					min = j;
				}
			}
			swap(a, i, min);	
		}
		//printArray(a);
		return count;
	}

	
	/**
	 * シャトルソート
	 * @param a
	 * @return
	 */
	static int shuttleSort(int[] a) {
		int count = 0;
		for (int i = 1; a.length > i; i++) {
			int j;
			int tmp = a[i];
			for (j = i; j > 0 && a[j - 1] > tmp; j--) {
				count++;
				a[j] = a[j - 1];
			}
			a[j] = tmp;
		}
		return count;
	}


}

さっきのはもはや諦めて(書けるだろ?評価は出来ないけど)
次に進む。シャトルソートを理解して今日は寝るべさ