java学习第47天,选择排序

  1. 选择排序是一个基础的排序算法。
  2. 与插入排序不同, 先做最麻烦的,要进行n−1次比较才能获得最小的数据。
  3. 数据一旦被选择并确定位置,就不会再改变。
  4. 只需要两个额外的空间来存放最小数据的引用与下标,因此空间复杂度为O(1)。

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);
	}
}

第47天选择排序调用代码:

package java41to50;

public class D47_selectionSort {
	public static void main(String[] args) {
		System.out.println("\r\n-------选择排序测试-------");
		D41_DataArray.selectionSortTest();
	}
}

结果输出:

-------选择排序测试-------
数据数组的项目个数:7 .
(5, if)  (3, then)  (6, else)  (10, switch)  (7, case)  (1, for)  (9, while)  
结果
数据数组的项目个数:7 .
(1, for)  (3, then)  (5, if)  (6, else)  (7, case)  (9, while)  (10, switch)  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是 Java 中的快速排序代码示例: ``` import java.util.Arrays; public class QuickSort { public static void main(String[] args) { int[] arr = {3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48}; quickSort(arr, 0, arr.length - 1); System.out.println(Arrays.toString(arr)); } public static void quickSort(int[] arr, int low, int high) { if (arr == null || arr.length == 0) return; if (low >= high) return; // 找到基准数 int middle = low + (high - low) / 2; int pivot = arr[middle]; // 分成两部分 int i = low, j = high; while (i <= j) { while (arr[i] < pivot) { i++; } while (arr[j] > pivot) { j--; } if (i <= j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } // 递归排序两边的数组 if (low < j) quickSort(arr, low, j); if (high > i) quickSort(arr, i, high); } } ``` ### 回答2: 以下是一个使用Java实现的快速排序算法的示例代码: ```java public class QuickSort { public static void main(String[] args) { // 创建一个待排序的数组 int[] arr = {9, 2, 5, 1, 7, 4, 8, 6, 3}; // 调用快速排序函数并打印排序结果 quickSort(arr, 0, arr.length - 1); System.out.println("排序后的数组:"); for (int num : arr) { System.out.print(num + " "); } } public static void quickSort(int[] arr, int low, int high) { if (low < high) { // 划分数组并获取划分点的索引 int partitionIndex = partition(arr, low, high); // 对划分点左边的子数组进行快速排序 quickSort(arr, low, partitionIndex - 1); // 对划分点右边的子数组进行快速排序 quickSort(arr, partitionIndex + 1, high); } } public static int partition(int[] arr, int low, int high) { // 将第一个元素作为基准值 int pivot = arr[low]; int i = low, j = high; while (i < j) { // 从右侧找到第一个小于基准值的元素 while (i < j && arr[j] >= pivot) { j--; } if (i < j) { arr[i++] = arr[j]; } // 从左侧找到第一个大于基准值的元素 while (i < j && arr[i] <= pivot) { i++; } if (i < j) { arr[j--] = arr[i]; } } // 将基准值放到正确的位置 arr[i] = pivot; return i; } } ``` 以上代码展示了如何使用快速排序算法对一个整数数组进行排序。快速排序算法采用了分治的思想,通过选择一个基准值,将数组划分为两个子数组,然后对子数组进行递归排序,最终得到有序数组。其中`quickSort`方法是快速排序的入口,`partition`方法用于划分数组并获取划分点的索引。 ### 回答3: 快速排序是一种高效的排序算法,它使用了分治的思想。下面是一个用Java语言实现的快速排序的示例代码: ``` public class QuickSort { public static void quickSort(int[] arr, int low, int high) { if (low < high) { int pivot = partition(arr, low, high); // 将数组划分为两部分 quickSort(arr, low, pivot - 1); // 对左子数组进行快速排序 quickSort(arr, pivot + 1, high); // 对右子数组进行快速排序 } } private static int partition(int[] arr, int low, int high) { int pivot = arr[low]; // 选择第一个元素作为基准值 int i = low + 1; // 从基准值的下一个元素开始 for (int j = low + 1; j <= high; j++) { if (arr[j] < pivot) { swap(arr, i, j); // 比基准值小的元素放到左侧 i++; } } swap(arr, low, i - 1); // 将基准值放到最终位置 return i - 1; // 返回基准值的索引 } private static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static void main(String[] args) { int[] arr = {8, 5, 2, 9, 1, 7, 10, 6, 3, 4}; quickSort(arr, 0, arr.length - 1); for (int num : arr) { System.out.print(num + " "); } } } ``` 这段代码实现了快速排序算法。首先,定义了一个 `quickSort` 方法,该方法用于递归调用的快速排序。在 `quickSort` 方法中,选择一个基准值,并通过 `partition` 方法将数组划分为两个子数组,并分别对它们进行快速排序。 `partition` 方法通过基准值将数组划分为两部分,并将比基准值小的元素放在左侧,比基准值大的元素放在右侧。最后,将基准值放到最终位置。 `swap` 方法用于交换数组中的两个元素。 在 `main` 方法中,定义了一个示例数组 `arr`,并调用 `quickSort` 方法对它进行快速排序。最后,输出排序后的数组。运行代码,输出结果为 1 2 3 4 5 6 7 8 9 10。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值