栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java-13

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java-13

学习来源:日撸 Java 三百行(41-50天,查找与排序)_闵帆的博客-CSDN博客 42 哈希表

42.1 使用 (最简单的) 除数取余法获得数据存放地址 (下标)。

42.2 使用 (最简单的) 顺移位置法解决冲突。

代码:

	
	public DataArray(int[] paraKeyArray, String[] paraContentArray, int paraLength) {
		// Step 1. Initialize.
		length = paraLength;
		data = new DataNode[length];
		
		for (int i = 0; i < length; i++) {
			data[i] = null;
		} // Of for i
		
		// Step 2. Fill the data.
		int tempPosition;
		for (int i = 0; i < paraKeyArray.length; i++) {
			// Hash.
			tempPosition = paraKeyArray[i] % paraLength;
			
			// Find an empty position.
			while (data[tempPosition] != null) {
				tempPosition = (tempPosition + 1) % paraLength;
				System.out.println("Collision, move forward for key " + paraKeyArray[i]);
			} // Of while
			
			data[tempPosition] = new DataNode(paraKeyArray[i], paraContentArray[i]);
		} // Of for i
	} // Of the second constructor
	
	
	public String hashSearch(int paraKey) {
		int tempPosition = paraKey % length;
		while (data[tempPosition] != null) {
			if (data[tempPosition].key == paraKey) {
				return data[tempPosition].content;
			} // Of if
			System.out.println("Not this one for " +paraKey);
			tempPosition = (tempPosition + 1) % length;
 		} // Of while
		
		return "null";
	} // Of hashSearch
	
	
	public static void hashSearchTest() {
		int[] tempUnsortedKeys = { 16, 33, 38, 69, 57, 95, 86 };
		String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };
		DataArray tempDataArray = new 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));

	} // Of hashSearchTest
	
	
	public static void main(String[] args) {
		System.out.println("rn-------------sequentialSearchTest-------------");
		sequentialSearchTest();
		
		System.out.println("rn-------------binarySearchTest-------------");
		binarySearchTest();
		
		System.out.println("rn-------------hashSearchTest-------------");
		hashSearchTest();
		
	} // Of main
	

截图:

43 插入排序

43.1 插入排序是简单直接的排序方式之一。

43.2 每次保证前 i 个数据是有序的。

43.3 下标 0 的数据为岗哨。

代码:

	
	
	public void insertionSort() {
		DataNode tempNode;
		int j;
		for (int i = 2; i < length; i++) {
			tempNode = data[i];
			
			// Find the position to inset.
			// At the same time, move other nodes.
			for (j = i - 1; data[j].key > tempNode.key; j--) {
				data[j + 1] = data[j];
			} // Of for j
			
			// Insert.
			data[j + 1] = tempNode;
			
			System.out.println("Round " + (i - 1));
			System.out.println(this);
		} // Of for i
	} // Of insertionSort
	
	
	public static void insertionSortTest() {
		int[] tempUnsortedKeys = { -100, 5, 3, 6, 10, 7, 1, 9 };
		String[] tempContents = { "null", "if", "then", "else", "switch", "case", "for", "while" };
		DataArray tempDataArray = new DataArray(tempUnsortedKeys, tempContents);
		
		System.out.println(tempDataArray);
		
		tempDataArray.insertionSort();
		System.out.println("Resultrn" + tempDataArray);
		
	} // Of insertionSortTest
	
	
	
	public static void main(String[] args) {
		System.out.println("rn-------------sequentialSearchTest-------------");
		sequentialSearchTest();
		
		System.out.println("rn-------------binarySearchTest-------------");
		binarySearchTest();
		
		System.out.println("rn-------------hashSearchTest-------------");
		hashSearchTest();
		
		System.out.println("rn-------------insertionSort-------------");
		insertionSortTest();
		
	} // Of main
	
} // Of class DataArray

截图:

44 希尔排序

44.1 达 4 重循环, 但时间复杂度只有O(n^2)。

44.2 岗哨的个数与最初的步长相关。

代码:

	
	
	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];
					// Find the position to insert.
					// At the same tiome, move other nodes.
					for (p = k - tempJump; p >= 0; p -= tempJump) {
						if (data[p].key > tempNode.key) {
							data[p + tempJump] = data[p];
						} else {
							break;
						} // Of if
					} // Of for p
					
					// Insert.
					data[p + tempJump] = tempNode;
				} // Of for k
				
			} // Of for j
			System.out.println("Round " + i);
			System.out.println(this);
		} // Of for i
	} // Of shellSort
	
	
	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" };
		DataArray tempDataArray = new DataArray(tempUnsortedKeys, tempContents);
		
		System.out.println(tempDataArray);
		
		tempDataArray.shellSort();
		System.out.println("Resultrn" + tempDataArray);
		
	} // Of shellSortTest
	
	
	
	public static void main(String[] args) {
		System.out.println("rn-------------sequentialSearchTest-------------");
		sequentialSearchTest();
		
		System.out.println("rn-------------binarySearchTest-------------");
		binarySearchTest();
		
		System.out.println("rn-------------hashSearchTest-------------");
		hashSearchTest();
		
		System.out.println("rn-------------insertionSort-------------");
		insertionSortTest();
		
		System.out.println("rn-------------shellSort-------------");
		shellSortTest();
	} // Of main
	

截图:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/838352.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号