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

Java学习(Day 23)

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

Java学习(Day 23)

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

文章目录
  • 冒泡排序
    • 一、描述
    • 二、具体代码
    • 三、运行截图
  • 快速排序
    • 一、描述
    • 二、具体代码
    • 三、运行截图
  • 总结

冒泡排序 一、描述

冒泡排序(Bubble Sort)也是一种简单直观的排序算法.

它重复地走访过要排序的数列, 一次比较两个元素, 如果他们的顺序错误就把他们交换过来.

走访数列的工作是重复地进行直到没有再需要交换, 也就是说该数列已经排序完成.

这个算法的名字由来是因为越小的元素会经由交换慢慢 “浮” 到数列的顶端.

冒泡排序还有一种优化算法, 就是立一个 flag , 当在一趟序列遍历中元素没有发生交换, 则证明该序列已经有序. 但这种改进对于提升性能来说并没有什么太大作用.

二、具体代码
	
	public void bubbleSort() {
		boolean tempSwapped;
		DataNode tempNode;
		for (int i = length - 1; i > 0; 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;
				} // Of if
			} // Of for j

			// No swap in this round. The data are already sorted.
			if (!tempSwapped) {
				System.out.println("Premature");
				break;
			} // Of if

			System.out.println("Round " + (length - i));
			System.out.println(this);
		} // Of for i
	}// Of bubbleSort

	
	public static void bubbleSortTest() {
		int[] tempUnsortedKeys = { 1, 3, 6, 10, 7, 5, 9 };
		String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };
		DataArray tempDataArray = new DataArray(tempUnsortedKeys, tempContents);

		System.out.println(tempDataArray);

		tempDataArray.bubbleSort();
		System.out.println("Resultrn" + tempDataArray);
	}// Of bubbleSortTest
三、运行截图

快速排序 一、描述

从数列中挑出一个元素,称为 “基准” (pivot)

重新排序数列, 所有元素比基准值小的摆放在基准前面, 所有元素比基准值大的摆在基准的后面 (相同的数可以到任一边). 在这个分区退出之后, 该基准就处于数列的中间位置.这个称为分区 (partition) 操作

递归地 (recursive) 把小于基准值元素的子数列和大于基准值元素的子数列排序

平均时间复杂度为 O ( n log ⁡ n ) O(nlog{n}) O(nlogn), 但最坏情况还是 O ( n 2 ) O(n^2) O(n2)

二、具体代码
	
	public void quickSortRecursive(int paraStart, int paraEnd) {
		// Nothing to sort.
		if (paraStart >= paraEnd) {
			return;
		} // Of if

		int tempPivot = data[paraEnd].key;
		DataNode tempNodeForSwap;

		int tempLeft = paraStart;
		int tempRight = paraEnd - 1;

		// Find the position for the pivot.
		// At the same time move smaller elements to the left and bigger one to the
		// right.
		while (true) {
			while ((data[tempLeft].key < tempPivot) && (tempLeft < tempRight)) {
				tempLeft++;
			} // Of while

			while ((data[tempRight].key >= tempPivot) && (tempLeft < tempRight)) {
				tempRight--;
			} // Of while

			if (tempLeft < tempRight) {
				// Swap.
				System.out.println("Swapping " + tempLeft + " and " + tempRight);
				tempNodeForSwap = data[tempLeft];
				data[tempLeft] = data[tempRight];
				data[tempRight] = tempNodeForSwap;
			} else {
				break;
			} // Of if
		} // Of while

		// Swap
		if (data[tempLeft].key > tempPivot) {
			tempNodeForSwap = data[paraEnd];
			data[paraEnd] = data[tempLeft];
			data[tempLeft] = tempNodeForSwap;
		} else {
			tempLeft++;
		} // Of if

		System.out.print("From " + paraStart + " to " + paraEnd + ": ");
		System.out.println(this);

		quickSortRecursive(paraStart, tempLeft - 1);
		quickSortRecursive(tempLeft + 1, paraEnd);
	}// Of quickSortRecursive

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

	
	public static void quickSortTest() {
		int[] tempUnsortedKeys = { 1, 3, 12, 10, 5, 7, 9 };
		String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };
		DataArray tempDataArray = new DataArray(tempUnsortedKeys, tempContents);

		System.out.println(tempDataArray);

		tempDataArray.quickSort();
		System.out.println("Resultrn" + tempDataArray);
	}// Of quickSortTest
三、运行截图

总结

作为最简单的排序算法之一, 冒泡排序给我的感觉就像 Abandon 在单词书里出现的感觉一样, 每次都在第一页第一位, 所以最熟悉.

然后就是快排, 这是许多语言内置排序的默认算法. 然后我惊奇地发现 Java 的默认排序算法竟然也是快排序.

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

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

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