java学习第49天,归并排序

  1. logn轮,每轮O(n)次拷贝。因此时间复杂度为O(nlogn)。
  2. 空间复杂度为O(n),只需要一行辅助空间。
  3. 全都是在拷贝引用,而不是数据本身。这是java的特性。
  4. 归并两个有序小组的时候,用了三个并列的循环。
  5. 涉及分组后尾巴的各种情况,所以需要相应的if语句。

java代码:

package java41to50;

import java41to50.D41_DataArray.DataNode;

public class D41_DataArray {
	class DataNode {
		int key;
		String content;

		DataNode(int paraKey, String paraContent) {
			key = paraKey;
			content = paraContent;
		}

		public String toString() {
			return "(" + key + ", " + content + ") ";
		}
	}

	DataNode[] data;
	int length;

	public static void main(String[] args) {
		System.out.println("\r\n-------顺序搜索测试-------");
		sequentialSearchTest();
		System.out.println("\r\n-------二进制搜索测试-------");
		binarySearchTest();
	}

	public D41_DataArray(int[] keyArray, String[] contentArray) {
		length = keyArray.length;
		data = new DataNode[length];
		for (int i = 0; i < length; i++) {
			data[i] = new DataNode(keyArray[i], contentArray[i]);
		}
	}

	public D41_DataArray(int[] paraKeyArray, String[] paraContentArray, int paraLength) {
		length = paraLength;
		data = new DataNode[length];
		for (int i = 0; i < length; i++) {
			data[i] = null;
		}
		int tempPosition;
		for (int i = 0; i < paraKeyArray.length; i++) {
			tempPosition = paraKeyArray[i] % paraLength;
			while (data[tempPosition] != null) {
				tempPosition = (tempPosition + 1) % paraLength;
				System.out.println("碰撞,向前移动 " + paraKeyArray[i]);
			}
			data[tempPosition] = new DataNode(paraKeyArray[i], paraContentArray[i]);
		}
	}

	public String hashSearch(int paraKey) {
		int tempPosition = paraKey % length;
		while (data[tempPosition] != null) {
			if (data[tempPosition].key == paraKey) {
				return data[tempPosition].content;
			}
			System.out.println("Not this one for " + paraKey);
			tempPosition = (tempPosition + 1) % length;
		}
		return "null";
	}

	public static void hashSearchTest() {
		int[] tempUnsortedKeys = { 16, 33, 38, 69, 57, 95, 86 };
		String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };
		D41_DataArray tempDataArray = new D41_DataArray(tempUnsortedKeys, tempContents, 19);
		System.out.println(tempDataArray);
		System.out.println("Search result of 95 is: " + tempDataArray.hashSearch(95));
		System.out.println("Search result of 38 is: " + tempDataArray.hashSearch(38));
		System.out.println("Search result of 57 is: " + tempDataArray.hashSearch(57));
		System.out.println("Search result of 4 is: " + tempDataArray.hashSearch(4));
	}

	public String toString() {
		String resultString = "数据数组的项目个数:" + length + " .\r\n";
		for (int i = 0; i < length; i++) {
			resultString += data[i] + " ";
		}
		return resultString;
	}

	public String sequentialSearch(int key) {
		data[0].key = key;
		int i;
		for (i = length - 1; data[i].key != key; i--) {
			;
		}
		return data[i].content;
	}

	public static void sequentialSearchTest() {
		int[] unsortedKeys = { -1, 5, 3, 6, 10, 7, 1, 9 };
		String[] contents = { "null", "if", "then", "else", "switch", "case", "for", "while" };
		D41_DataArray dataArray = new D41_DataArray(unsortedKeys, contents);
		System.out.println(dataArray);
		System.out.println("Search result of 10 is: " + dataArray.sequentialSearch(10));
		System.out.println("Search result of 5 is: " + dataArray.sequentialSearch(5));
		System.out.println("Search result of 4 is: " + dataArray.sequentialSearch(4));
	}

	public String binarySearch(int key) {
		int left = 0;
		int right = length - 1;
		int middle = (left + right) / 2;

		while (left <= right) {
			middle = (left + right) / 2;
			if (data[middle].key == key) {
				return data[middle].content;
			} else if (data[middle].key <= key) {
				left = middle + 1;
			} else {
				right = middle - 1;
			}
		}
		return "null";
	}

	public static void binarySearchTest() {
		int[] sortedKeys = { 1, 3, 5, 6, 7, 9, 10 };
		String[] contents = { "if", "then", "else", "switch", "case", "for", "while" };
		D41_DataArray dataArray = new D41_DataArray(sortedKeys, contents);
		System.out.println(dataArray);
		System.out.println("Search result of 10 is: " + dataArray.binarySearch(10));
		System.out.println("Search result of 5 is: " + dataArray.binarySearch(5));
		System.out.println("Search result of 4 is: " + dataArray.binarySearch(4));
	}

	public void insertionSort() {
		DataNode tempNode;
		int j;
		for (int i = 2; i < length; i++) {
			tempNode = data[i];
			for (j = i - 1; data[j].key > tempNode.key; j--) {
				data[j + 1] = data[j];
			}
			data[j + 1] = tempNode;
			System.out.println("Round " + (i - 1));
			System.out.println(this);
		}
	}

	public static void insertionSortTest() {
		int[] tempUnsortedKeys = { -100, 5, 3, 6, 10, 7, 1, 9 };
		String[] tempContents = { "null", "if", "then", "else", "switch", "case", "for", "while" };
		D41_DataArray tempDataArray = new D41_DataArray(tempUnsortedKeys, tempContents);
		System.out.println(tempDataArray);
		tempDataArray.insertionSort();
		System.out.println("结果\r\n" + tempDataArray);
	}

	public void shellSort() {
		DataNode tempNode;
		int[] tempJumpArray = { 5, 3, 1 };
		int tempJump;
		int p;
		for (int i = 0; i < tempJumpArray.length; i++) {
			tempJump = tempJumpArray[i];
			for (int j = 0; j < tempJump; j++) {
				for (int k = j + tempJump; k < length; k += tempJump) {
					tempNode = data[k];
					for (p = k - tempJump; p >= 0; p -= tempJump) {
						if (data[p].key > tempNode.key) {
							data[p + tempJump] = data[p];
						} else {
							break;
						}
					}
					data[p + tempJump] = tempNode;
				}
			}
			System.out.println("Round " + i);
			System.out.println(this);
		}
	}

	public static void shellSortTest() {
		int[] tempUnsortedKeys = { 5, 3, 6, 10, 7, 1, 9, 12, 8, 4 };
		String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while", "throw", "until", "do" };
		D41_DataArray tempDataArray = new D41_DataArray(tempUnsortedKeys, tempContents);
		System.out.println(tempDataArray);
		tempDataArray.shellSort();
		System.out.println("结果\r\n" + tempDataArray);
	}

	public void bubbleSort() {
		boolean tempSwapped;
		DataNode tempNode;
		for (int i = length - 1; i > 1; i--) {
			tempSwapped = false;
			for (int j = 0; j < i; j++) {
				if (data[j].key > data[j + 1].key) {
					// Swap.
					tempNode = data[j + 1];
					data[j + 1] = data[j];
					data[j] = tempNode;

					tempSwapped = true;
				}
			}
			if (!tempSwapped) {
				System.out.println("Premature");
				break;
			}
			System.out.println("第" + (length - i) + "回");
			System.out.println(this);
		}
	}

	public static void bubbleSortTest() {
		int[] tempUnsortedKeys = { 1, 3, 6, 10, 7, 5, 9 };
		String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };
		D41_DataArray tempDataArray = new D41_DataArray(tempUnsortedKeys, tempContents);
		System.out.println(tempDataArray);
		tempDataArray.bubbleSort();
		System.out.println("结果\r\n" + tempDataArray);
	}

	public void quickSortRecursive(int paraStart, int paraEnd) {
		if (paraStart >= paraEnd) {
			return;
		}
		int tempPivot = data[paraEnd].key;
		DataNode tempNodeForSwap;
		int tempLeft = paraStart;
		int tempRight = paraEnd - 1;
		while (true) {
			while ((data[tempLeft].key < tempPivot) && (tempLeft < tempRight)) {
				tempLeft++;
			}
			while ((data[tempRight].key >= tempPivot) && (tempLeft < tempRight)) {
				tempRight--;
			}
			if (tempLeft < tempRight) {
				System.out.println(tempLeft + " 和 " + tempRight + " 交换");
				tempNodeForSwap = data[tempLeft];
				data[tempLeft] = data[tempRight];
				data[tempRight] = tempNodeForSwap;
			} else {
				break;
			}
		}
		if (data[tempLeft].key > tempPivot) {
			tempNodeForSwap = data[paraEnd];
			data[paraEnd] = data[tempLeft];
			data[tempLeft] = tempNodeForSwap;
		} else {
			tempLeft++;
		}
		System.out.print("从 " + paraStart + " 到 " + paraEnd + ": ");
		System.out.println(this);
		quickSortRecursive(paraStart, tempLeft - 1);
		quickSortRecursive(tempLeft + 1, paraEnd);
	}

	public void quickSort() {
		quickSortRecursive(0, length - 1);
	}

	public static void quickSortTest() {
		int[] tempUnsortedKeys = { 1, 3, 12, 10, 5, 7, 9 };
		String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };
		D41_DataArray tempDataArray = new D41_DataArray(tempUnsortedKeys, tempContents);
		System.out.println(tempDataArray);
		tempDataArray.quickSort();
		System.out.println("结果\r\n" + tempDataArray);
	}

	public void selectionSort() {
		DataNode tempNode;
		int tempIndexForSmallest;
		for (int i = 0; i < length - 1; i++) {
			tempNode = data[i];
			tempIndexForSmallest = i;
			for (int j = i + 1; j < length; j++) {
				if (data[j].key < tempNode.key) {
					tempNode = data[j];
					tempIndexForSmallest = j;
				}
			}
			data[tempIndexForSmallest] = data[i];
			data[i] = tempNode;
		}
	}

	public static void selectionSortTest() {
		int[] tempUnsortedKeys = { 5, 3, 6, 10, 7, 1, 9 };
		String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };
		D41_DataArray tempDataArray = new D41_DataArray(tempUnsortedKeys, tempContents);
		System.out.println(tempDataArray);
		tempDataArray.selectionSort();
		System.out.println("结果\r\n" + tempDataArray);
	}

	public void heapSort() {
		DataNode tempNode;
		for (int i = length / 2 - 1; i >= 0; i--) {
			adjustHeap(i, length);
		}
		System.out.println("初始堆: " + this + "\r\n");
		for (int i = length - 1; i > 0; i--) {
			tempNode = data[0];
			data[0] = data[i];
			data[i] = tempNode;
			adjustHeap(0, i);
			System.out.println("阶段 " + (length - i) + ": " + this);
		}
	}

	public void adjustHeap(int paraStart, int paraLength) {
		DataNode tempNode = data[paraStart];
		int tempParent = paraStart;
		int tempKey = data[paraStart].key;
		for (int tempChild = paraStart * 2 + 1; tempChild < paraLength; tempChild = tempChild * 2 + 1) {
			if (tempChild + 1 < paraLength) {
				if (data[tempChild].key < data[tempChild + 1].key) {
					tempChild++;
				}
			}
			System.out.println("父节点位置 " + tempParent + " 并且孩子节点是 " + tempChild);
			if (tempKey < data[tempChild].key) {
				data[tempParent] = data[tempChild];
				System.out.println("移动 " + data[tempChild].key + " 到位置 " + tempParent);
				tempParent = tempChild;
			} else {
				break;
			}
		}
		data[tempParent] = tempNode;
		System.out.println("调节 " + paraStart + " 到 " + paraLength + ": " + this);
	}

	public static void heapSortTest() {
		int[] tempUnsortedKeys = { 5, 3, 6, 10, 7, 1, 9 };
		String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };
		D41_DataArray tempDataArray = new D41_DataArray(tempUnsortedKeys, tempContents);
		System.out.println(tempDataArray);
		tempDataArray.heapSort();
		System.out.println("结果\r\n" + tempDataArray);
	}

	public void mergeSort() {
		int tempRow;
		int tempGroups;
		int tempActualRow;
		int tempNextRow = 0;
		int tempGroupNumber;
		int tempFirstStart, tempSecondStart, tempSecondEnd;
		int tempFirstIndex, tempSecondIndex;
		int tempNumCopied;
		for (int i = 0; i < length; i++) {
			System.out.print(data[i]);
		}
		System.out.println();
		DataNode[][] tempMatrix = new DataNode[2][length];
		for (int i = 0; i < length; i++) {
			tempMatrix[0][i] = data[i];
		}
		tempRow = -1;
		for (int tempSize = 1; tempSize <= length; tempSize *= 2) {
			tempRow++;
			System.out.println("当前行 = " + tempRow);
			tempActualRow = tempRow % 2;
			tempNextRow = (tempRow + 1) % 2;
			tempGroups = length / (tempSize * 2);
			if (length % (tempSize * 2) != 0) {
				tempGroups++;
			}
			System.out.println("大小 = " + tempSize + ", 组 = " + tempGroups);
			for (tempGroupNumber = 0; tempGroupNumber < tempGroups; tempGroupNumber++) {
				tempFirstStart = tempGroupNumber * tempSize * 2;
				tempSecondStart = tempGroupNumber * tempSize * 2 + tempSize;
				if (tempSecondStart > length - 1) {
					for (int i = tempFirstStart; i < length; i++) {
						tempMatrix[tempNextRow][i] = tempMatrix[tempActualRow][i];
					}
					continue;
				}
				tempSecondEnd = tempGroupNumber * tempSize * 2 + tempSize * 2 - 1;
				if (tempSecondEnd > length - 1) {
					tempSecondEnd = length - 1;
				}
				System.out.println("尝试归并 [" + tempFirstStart + ", " + (tempSecondStart - 1) + "] 和 [" + tempSecondStart
						+ ", " + tempSecondEnd + "]");
				tempFirstIndex = tempFirstStart;
				tempSecondIndex = tempSecondStart;
				tempNumCopied = 0;
				while ((tempFirstIndex <= tempSecondStart - 1) && (tempSecondIndex <= tempSecondEnd)) {
					if (tempMatrix[tempActualRow][tempFirstIndex].key <= tempMatrix[tempActualRow][tempSecondIndex].key) {
						tempMatrix[tempNextRow][tempFirstStart
								+ tempNumCopied] = tempMatrix[tempActualRow][tempFirstIndex];
						tempFirstIndex++;
						System.out.println("复制 " + tempMatrix[tempActualRow][tempFirstIndex]);
					} else {
						tempMatrix[tempNextRow][tempFirstStart
								+ tempNumCopied] = tempMatrix[tempActualRow][tempSecondIndex];
						System.out.println("复制 " + tempMatrix[tempActualRow][tempSecondIndex]);
						tempSecondIndex++;
					}
					tempNumCopied++;
				}
				while (tempFirstIndex <= tempSecondStart - 1) {
					tempMatrix[tempNextRow][tempFirstStart + tempNumCopied] = tempMatrix[tempActualRow][tempFirstIndex];
					tempFirstIndex++;
					tempNumCopied++;
				}
				while (tempSecondIndex <= tempSecondEnd) {
					tempMatrix[tempNextRow][tempFirstStart
							+ tempNumCopied] = tempMatrix[tempActualRow][tempSecondIndex];
					tempSecondIndex++;
					tempNumCopied++;
				}
			}
			System.out.println("第" + tempRow + "回");
			for (int i = 0; i < length; i++) {
				System.out.print(tempMatrix[tempNextRow][i] + " ");
			}
			System.out.println();
		}
		data = tempMatrix[tempNextRow];
	}

	public static void mergeSortTest() {
		int[] tempUnsortedKeys = { 5, 3, 6, 10, 7, 1, 9 };
		String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };
		D41_DataArray tempDataArray = new D41_DataArray(tempUnsortedKeys, tempContents);
		System.out.println(tempDataArray);
		tempDataArray.mergeSort();
		System.out.println(tempDataArray);
	}
}

归并排序代码引用:

package java41to50;

public class D49_mergeSort {
	public static void main(String[] args) {
		System.out.println("\r\n-------归并排序测试-------");
		D41_DataArray.mergeSortTest();
	}
}

结果输出:


-------归并排序测试-------
数据数组的项目个数:7 .
(5, if)  (3, then)  (6, else)  (10, switch)  (7, case)  (1, for)  (9, while)  
(5, if) (3, then) (6, else) (10, switch) (7, case) (1, for) (9, while) 
当前行 = 0
大小 = 1,= 4
尝试归并 [0, 0][1, 1]
复制 (3, then) 
尝试归并 [2, 2][3, 3]
复制 (10, switch) 
尝试归并 [4, 4][5, 5]
复制 (1, for)0(3, then)  (5, if)  (6, else)  (10, switch)  (1, for)  (7, case)  (9, while)  
当前行 = 1
大小 = 2,= 2
尝试归并 [0, 1][2, 3]
复制 (5, if) 
复制 (6, else) 
尝试归并 [4, 5][6, 6]
复制 (7, case) 
复制 (9, while)1(3, then)  (5, if)  (6, else)  (10, switch)  (1, for)  (7, case)  (9, while)  
当前行 = 2
大小 = 4,= 1
尝试归并 [0, 3][4, 6]
复制 (1, for) 
复制 (5, if) 
复制 (6, else) 
复制 (10, switch) 
复制 (7, case) 
复制 (9, while)2(1, for)  (3, then)  (5, if)  (6, else)  (7, case)  (9, while)  (10, switch)  
数据数组的项目个数:7 .
(1, for)  (3, then)  (5, if)  (6, else)  (7, case)  (9, while)  (10, switch)  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值