算法学习---基本数据类型的数组二分查找实现

public class OrderArray {
	// array
	private long[] a;
	// size
	private int size;

	public OrderArray(int max) {
		a = new long[max];
		size = 0;
	}

	public int getSize() {
		return size;
	}

	public void insert(long value) {
		// 找到有序數組合適的位置
		if (size == 0) {
			size++;
			a[0] = value;
			return;
		}
		int i;
		for (i = 0; i < size; i++) {
			if (a[i] > value) {
				break;
			}
		}
		for (int j = size; j > i; j--) {
			// 所有大於該值的元素向后移一位
			a[j] = a[j - 1];
		}
		// 插入當前位置
		a[i] = value;
		size++;
	}

	public boolean delete(long value) {
		int position = find(value);
		if (position == -1) {
			return false;
		} else {
			for (int i = position; i < size; i++) {
				// 大於該值的所有元素向前移一位
				a[i] = a[i + 1];
			}
			size--;
			return true;
		}

	}

	public void display() {
		for (int i = 0; i < size; i++) {
			System.out.print(a[i] + " ");
		}
	}

	/**
	 * 二分查找 查詢所給元素在集合中的位置
	 * 
	 * @param searchKey
	 * @return
	 */
	public int find(long searchKey) {
		if (getSize() == 0)
			return -1;
		int upperBound = getSize() - 1;
		int lowerBound = 0;
		int curNum;
		while (true) {
			if (lowerBound > upperBound)
				return -1;
			curNum = (upperBound + lowerBound) / 2;
			if (a[curNum] == searchKey) {
				return curNum;
			} else {
				if (searchKey > a[curNum]) {
					// 如果查詢的數字大於目前的基準值 下限移到基準位置加一
					lowerBound = curNum + 1;
				} else {
					upperBound = curNum - 1;
				}
			}
		}
	}

	/**
	 * 得到最大的那個數組元素
	 * 
	 * @return
	 */
	public long getMax() {
		long max = a[0];
		if (size == 0)
			return -1;
		for (int i = 0; i < size; i++) {
			if (max < a[i]) {
				max = a[i];
			}
		}
		return max;
	}

	/**
	 * 刪除重複元素
	 */
	public void noDup() {
		/** 記錄重複的元素個數. **/
		int count = 0;
		for (int i = 0; i < size; i++) {
			for (int j = i + 1; j < size; j++) {
				/** 將重複元素標記為-1. **/
				if (a[j] != -1 && a[i] == a[j]) {
					a[j] = -1;
					count++;
				}
			}
		}
		// 存放非重複元素的數組
		long noDupArray[] = new long[size - count];
		for (int k = 0, l = 0; k < size; k++) {
			if (a[k] != -1) {
				// 元素沒有標記證明非重複,加入數組
				noDupArray[l++] = a[k];
			}
		}
		a = noDupArray;
		size = noDupArray.length;
	}

	/**
	 * 將兩個有序數組合併為一個有序數組
	 * 
	 * @param oneOrderArray
	 * @param otherOrderArray
	 * @return
	 */
	public long[] merge(long[] oneOrderArray, long[] otherOrderArray) {
		long[] result = new long[oneOrderArray.length + otherOrderArray.length];
		if (checkSort(oneOrderArray) && checkSort(otherOrderArray)) {
			// 分別記錄兩個有序數組的下標
			int i = 0, j = 0;
			// 記錄組合后的新數組的下標
			int k = 0;
			while (i < oneOrderArray.length && j < otherOrderArray.length) {
				if (oneOrderArray[i] <= otherOrderArray[j]) {
					result[k++] = oneOrderArray[i++];
				} else {
					result[k++] = otherOrderArray[j++];
				}
			}

			/**
			 * 
			 * 后面连兩个while循环是用来保证两个数组比较完之后剩下的一个数组里的元素能顺利传入. 上面while循環跳出的條件有:
			 * ①兩個數組元素剛好比較完沒有剩餘 ②i有剩餘 ③j有剩餘
			 */
			while (i < oneOrderArray.length)
				result[k++] = oneOrderArray[i++];
			while (j < otherOrderArray.length)
				result[k++] = otherOrderArray[j++];
			return result;
		} else {
			System.out.println("非有序數組,不可排序");
			return null;
		}
	}

	/**
	 * 檢查是否是有序數組
	 * 
	 * @param array
	 * @return
	 */
	private boolean checkSort(long[] array) {
		boolean change = true;
		for (int i = 0; i < array.length - 1 && change; i++) {
			for (int j = i + 1; j < array.length; j++) {
				// 依次比較前後兩個數的大小,如果前一個數大於后一個數直接返回false,
				// 沒有變化則是有序數組設置change為false不在遍歷
				if (array[j - 1] > array[j]) {
					return false;
				} else
					change = false;
			}
		}
		return true;
	}

}


客户端使用

public class OrderArrayClient {
     public static void main(String[] args) {
		OrderArray array = new OrderArray(100);
		array.insert(1);
		array.insert(34);
		array.insert(21);
		array.insert(22);
		array.insert(22);
		array.insert(22);
		array.insert(22);
		array.insert(22);
		array.insert(3312);
		array.insert(41);
		array.insert(41);
		array.insert(41);
		array.insert(52);
		array.insert(111);
		array.insert(62);
		array.insert(1232);
		array.insert(1232);
		System.out.println("before noDup");
		array.display();
		array.noDup();
		System.out.println();
		System.out.println("after noDup");
		array.display();
		System.out.println();
		System.out.println("max is :"+array.getMax());
		long a[]= {1,12,21,22,33,45};
		long b[]= {17,18,77};
		long result[] = array.merge(a, b);
		for(long i : result){
			System.out.print(i+" ");
		}
	}
}




 

posted @ 2014-04-19 18:33  Leo的银弹  阅读(154)  评论(0编辑  收藏  举报