• 堆排序


    :就是完全二叉树,注意与满二叉树的区别,完全二叉树,从左往右依次有节点,节点缺一不可

    用数组表示完全二叉树,下标对应的关系,父:(i+1)/2,左孩子 2*i+1,右孩子 2*i+2。

    大根堆:这棵树的任何子树的结点,都是父节点大。

    小根堆:这棵树的任何子树的结点,都是父节点小。

    package 左神_算法;
    
    import java.util.Arrays;
    
    public class HeapSort {
    
    	/**
    	 * 
    	 * 堆排序的细节和复杂度分析 时间复杂度O(N*logN),额外空间复杂度O(1) 堆结构非常重要 
    	 * 1,堆结构的heapInsert与heapify
    	 * 2,堆结构的增大和减少 
    	 * 3,如果只是建立堆的过程,时间复杂度为O(N) 
    	 * 4,优先级队列结构,就是堆结构
    	 *
    	 */
    //	public class Code_03_HeapSort {
    
    	public static void heapSort(int[] arr) {
    		if (arr == null || arr.length < 2) {
    			return;
    		}
    		for (int i = 0; i < arr.length; i++) {  // 生成大堆根
    			heapInsert(arr, i);
    		}
    		int heapSize = arr.length;
    		swap(arr, 0, --heapSize);
    		while (heapSize > 0) { //每次找出大根堆的堆顶元素与最后一个大根堆元素交换,并下沉heapify已换过的堆顶元素
    			heapify(arr, 0, heapSize);
    			swap(arr, 0, --heapSize);
    		}
    	}
    
    	// 构建大根堆
    	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;
    		}
    	}
    
    	// 某位置变化后堆的调整
    	public static void heapify(int[] arr, int index, int heapSize) {  
    		int left = index * 2 + 1;
    		while (left < heapSize) {
    			int largest = left + 1 < heapSize && arr[left + 1] > arr[left] ? left + 1 : left;// 取最大的孩子的下标
    			largest = arr[largest] > arr[index] ? largest : index;
    			if (largest == index) {
    				break;
    			}
    			swap(arr, largest, index);
    			index = largest;
    			left = index * 2 + 1;
    		}
    	}
    
    	public static void swap(int[] arr, int i, int j) {
    		int tmp = arr[i];
    		arr[i] = arr[j];
    		arr[j] = tmp;
    	}
    
    	// for test
    	public static void comparator(int[] arr) {
    		Arrays.sort(arr);
    	}
    
    	// for test
    	public static int[] generateRandomArray(int maxSize, int maxValue) {
    		int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
    		for (int i = 0; i < arr.length; i++) {
    			arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
    		}
    		return arr;
    	}
    
    	// for test
    	public static int[] copyArray(int[] arr) {
    		if (arr == null) {
    			return null;
    		}
    		int[] res = new int[arr.length];
    		for (int i = 0; i < arr.length; i++) {
    			res[i] = arr[i];
    		}
    		return res;
    	}
    
    	// for test
    	public static boolean isEqual(int[] arr1, int[] arr2) {
    		if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
    			return false;
    		}
    		if (arr1 == null && arr2 == null) {
    			return true;
    		}
    		if (arr1.length != arr2.length) {
    			return false;
    		}
    		for (int i = 0; i < arr1.length; i++) {
    			if (arr1[i] != arr2[i]) {
    				return false;
    			}
    		}
    		return true;
    	}
    
    	// for test
    	public static void printArray(int[] arr) {
    		if (arr == null) {
    			return;
    		}
    		for (int i = 0; i < arr.length; i++) {
    			System.out.print(arr[i] + " ");
    		}
    		System.out.println();
    	}
    
    	// for test
    	public static void main(String[] args) {
    		int testTime = 500000;
    		int maxSize = 100;
    		int maxValue = 100;
    		boolean succeed = true;
    		for (int i = 0; i < testTime; i++) {
    			int[] arr1 = generateRandomArray(maxSize, maxValue);
    			int[] arr2 = copyArray(arr1);
    			heapSort(arr1);
    			comparator(arr2);
    			if (!isEqual(arr1, arr2)) {
    				succeed = false;
    				break;
    			}
    		}
    		System.out.println(succeed ? "Nice!" : "Fucking fucked!");
    
    		int[] arr = generateRandomArray(maxSize, maxValue);
    		printArray(arr);
    		heapSort(arr);
    		printArray(arr);
    	}
    
    }
    
  • 相关阅读:
    很多人知道外包的种种不好,但还是选择去外包,这是为什么呢?
    微信聊天内容可以被监听吗
    Go 语言笔试面试题(实现原理)
    oracle中正则表达式相关函数regexp_like简介
    These 30 keyboard shortcuts are guaranteed to save you time in After Effects.
    Why is git submodule not updated automatically on git checkout?
    WPF DataBinding: Nullable Int still gets a validation error?
    How to make a dropdown list of all cultures (but no repeats)
    米象
    How To Bind a Combobox to a Dictionary in WPF C#
  • 原文地址:https://www.cnblogs.com/horken/p/10706137.html
Copyright © 2020-2023  润新知