快排和堆排

快排---一次只搞定一个数字---就是选中的记录点

	public static void quickSort1(int[] arr, int left, int right) {
		if (left >= right) {
			return;
		}
		int key = (int) Math.random() * (right - left + 1);
		int i = left;
		int j = right;
		while (i < j) {
			while (arr[j] >= arr[key] && i < j) {
				j--;
			}

			while (arr[i] <= arr[key] && i < j) {
				i++;
			}
			swap(arr, i, j);
		}
		arr[left]=arr[i];
		arr[i]=arr[key];
		quickSort1(arr, left, i - 1);
		quickSort1(arr, i + 1, right);
	}

使用荷兰国旗问题思路来加速快排过程------三路快排-----一次搞定多个数字--所有和记录点相等的数字

public static void quickSort(int[] arr) {
		if (arr == null || arr.length < 2) {
			return;
		}
		quickSort(arr, 0, arr.length - 1);
	}

	public static void quickSort(int[] arr, int l, int r) {
		if (l < r) {
			swap(arr, l + (int) (Math.random() * (r - l + 1)), r);
			int[] p = partition(arr, l, r);
			quickSort(arr, l, p[0] - 1);
			quickSort(arr, p[1] + 1, r);
		}
	}

	public static int[] partition(int[] arr, int l, int r) {
		int less = l - 1;
		int more = r;
		while (l < more) {
			if (arr[l] < arr[r]) {
				swap(arr, ++less, l++);
			} else if (arr[l] > arr[r]) {
				swap(arr, --more, l);
			} else {
				l++;
			}
		}
		swap(arr, more, r);
		return new int[] { less + 1, more };
	}

构建堆以及堆排序-------大根堆

public static void heapSort(int[] arr) {
		if (arr.length < 2 || arr == null) {
			return;
		}
		// 构建堆
		for (int i = 0; i < arr.length; i++) {
			heapInsert(arr, i);
		}

		int size = arr.length-1; // 堆的大小
		swap(arr, 0, size); // 和最后交换 弹出堆顶
		while (size > 0) { // 交换之后 要进行调整堆
			heapify(arr, 0, size);
			swap(arr, 0, --size);
		}
	}

	// 堆是一种完全二叉树 从左到右填充
	// 构建大根堆 O(logN) index为数组当前位置
	public static void heapInsert(int[] arr, int index) {
		while (arr[index] > arr[(index - 1) / 2]) {// 和父亲比较 若比父大
			swap(arr, index, (index - 1) / 2);
			index = (index - 1) / 2; // index来到父亲位置
		}
	}

	// 调整堆 某个值变小了(index) 将其往下沉
	private static void heapify(int[] arr, int index, int size) {
		int left = index * 2 + 1; // 左孩子
		while (left < size) {
			// 左右孩子谁大返回谁的索引
			int largest = left + 1 < size && arr[left + 1] > arr[left] ? left + 1 : left;
			// 和父亲比较 谁大返回谁的索引
			largest = arr[largest] > arr[index] ? largest : index;
			if (largest == index) {
				break;// 不用调整
			}

			// 进行到此说明孩子要调整 和孩子交换
			swap(arr, index, largest);
			index=largest;   //当前节点变成孩子之一
			left = index * 2 + 1;
		}
	}

	private static void swap(int[] arr, int i, int j) {
		int temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
	}

优先队列是小根堆

public void test() {
		//优先队列是个小根堆
		 PriorityQueue<Integer> minHeap=new PriorityQueue<>();
		 PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(new Comparator<Integer>() {
			 
	            @Override
	            public int compare(Integer o1, Integer o2) {
	                return o2.compareTo(o1);
	            }
	        });
	
	}

猜你喜欢

转载自blog.csdn.net/qq_41864967/article/details/89006317