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

Java学习(Day 22)

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

Java学习(Day 22)

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

文章目录
  • 插入排序
    • 一、描述
    • 二、具体代码
    • 三、运行截图
  • 希尔排序
    • 一、描述
    • 二、具体代码
    • 三、运行截图
  • 总结

插入排序 一、描述

插入排序像什么?没错就像斗地主或者打麻将中码牌. 每次对一个牌进行排序, 从左到右是有先后顺序的. 和这个类似的叫做冒泡排序在后面也会提及.

插入排序是简单直接的排序方式之一. 代码非常短.每次保证前 i 个数据是有序的.

先做简单的事情 (第 1 轮最多有 1 次移动), 再做麻烦的事情 (最后一轮最多有 n − 1 n - 1 n−1 次移动).

下标 0 的数据为岗哨, 与顺序查询同理. 比其它排序方式多用一个空间.

tempNode 只分配了引用 (指针) 的空间, 并未 new.

二、具体代码
	
	public void insertionSort() {
		DataNode tempNode;
		int j;
		for (int i = 2; i < length; i++) {
			tempNode = data[i];

			// Find the position to insert.
			// 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
三、运行截图

希尔排序 一、描述

希尔排序(Shell Sort)是插入排序的一种, 它是针对直接插入排序算法的改进.

希尔排序又称缩小增量排序,因 DL.Shell 于 1959 年提出而得名.

它通过比较相距一定间隔的元素来进行, 各趟比较所用的距离随着算法的进行而减小, 直到只比较相邻元素的最后一趟排序为止. 也就是说最后一趟还是要进行一次插入排序, 但是此时数组大多有序所以就不会耗费太多时间.

二、具体代码

虽然有四层循环, 但真实的希尔排序只有三层循环, 最后一层只是为了输出数组中的数字.

	
	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 time, 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
三、运行截图

总结

在编写希尔排序时对成组那部分有些绕, 然后就是对 tempJumpArray 的选择在我看来也可能对排序速度产生影响.

总而言之希尔排序就像是特殊的插入排序.

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

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

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