zoukankan      html  css  js  c++  java
  • 完全二叉树, 最大堆 , 堆排序

    脑袋不够用,所以记录下来

    python 版本 构建 最大堆

    class Utils(object):
        @staticmethod
        def buildMaxHeap(l=None,heap_size=None):
            if heap_size is None:
                heap_size = len(l)
            for i in range(heap_size//2,0,-1):
                Utils.maxHeapify(heap_size,i,l)
                
        @staticmethod
        def maxHeapify(heap_size,rootIndex,l=None):
            left,right,largest = 2*rootIndex,2*rootIndex+1,rootIndex
            
            if left<=heap_size and l[left-1] > l[rootIndex-1]:
                largest = left
            if right<=heap_size and l[right-1] > l[largest-1]:
                largest = right
                
            if largest!=rootIndex:
                l[largest-1],l[rootIndex-1] = l[rootIndex-1],l[largest-1]
                if largest <= heap_size//2:
                    Utils.maxHeapify(heap_size,largest,l)
            print(l)
            
    if __name__ == '__main__':
        l = [10,9,3,2,4,6,5,7,8]
        print(l)
        Utils.buildMaxHeap(l)
        print(l)
    
    
    
    class Sort:
    
        def bubble_sort(self, arr=None):
            len_arr = len(arr)
            for i in range(len_arr):
                for j in range(len_arr-i-1):
                    if arr[j] > arr[j+1]:
                        arr[j], arr[j+1] = arr[j+1], arr[j]
            return arr
    
        def select_sort(self, arr=None):
            len_arr = len(arr)
            for i in range(len_arr):
                for j in range(i+1, len_arr):
                    if arr[i] > arr[j]:
                        arr[i], arr[j] = arr[j], arr[i]
            return arr
    
        def insert_sort(self, arr=None):
            len_arr = len(arr)
            for i in range(len_arr):
                for j in range(i,0,-1):
                    if arr[j] < arr[j-1]:
                        arr[j], arr[j-1] = arr[j-1], arr[j]
            return arr
    
    
    if __name__ == '__main__':
        arr = [4,3,2,1]
        print(Sort().insert_sort(arr))
    

    改为 堆排序

    class Utils(object):
        @staticmethod
        def buildMaxHeap(l=None,heap_size=None):
            if heap_size is None:
                heap_size = len(l)
            for i in range(heap_size//2,0,-1):
                Utils.maxHeapify(heap_size,i,l)
                
        @staticmethod
        def maxHeapify(heap_size,rootIndex,l=None):
            left,right,largest = 2*rootIndex,2*rootIndex+1,rootIndex
            
            if left<=heap_size and l[left-1] > l[rootIndex-1]:
                largest = left
            if right<=heap_size and l[right-1] > l[largest-1]:
                largest = right
                
            if largest!=rootIndex:
                l[largest-1],l[rootIndex-1] = l[rootIndex-1],l[largest-1]
                if largest <= heap_size//2:
                    Utils.maxHeapify(heap_size,largest,l)
        @staticmethod
        def heapSort(l=None):
            Utils.buildMaxHeap(l)
            for i in range(len(l)-1,0,-1):
                l[0],l[i] = l[i],l[0]
                Utils.buildMaxHeap(l,i)
            
    if __name__ == '__main__':
        l = [10,9,3,2,4,6,5,7,8]
        print(l)
    #     Utils.buildMaxHeap(l)
        Utils.heapSort(l)
        print(l)
    

    java 版

    package com.ghc.starter.algorithm;
    /*
    这是一个排序算法集合的工具类
    * */
    public class SortUtils {
        public static int [] array = {10,9,3,2,4,6,5,7,8};
    
        // 完全二叉树 --》最大堆--》堆排序
        public static void main(String [] args){
            printArray(array);
            buildMaxHeap(array,array.length);
            printArray(array);
            heapSort(array);
            printArray(array);
            array = new int[]{10,9,3,2,4,6,5,7,8};
            printArray(array);
            bubbleSort(array);
            printArray(array);
        }
    
        // 快速排序
        public static void quickSort(int [] array){
    
        }
        // 冒泡排序
        public static void bubbleSort(int [] array){
            for(int i=0;i<array.length;i++){
                for(int j=0;j<array.length-i-1;j++){
                    if(array[j]>array[j+1]){
                        swap(array,j,j+1);
                    }
                }
            }
        }
        // 有格式的打印数组
        public static void printArray(int [] array){
            System.out.print("[");
            for(int i=0;i<array.length;i++){
                if(i!=array.length-1)
                System.out.print(array[i]+",");
                else System.out.println(array[i]+"]");
            }
        }
        // 堆排序 基于 下面 构建 最大堆
        public static void heapSort(int [] array){
            // 如果 数组只有一个元素或者为空则不用排序
            if(array==null || array.length<=1) return;
            // 否则开始 堆排序
            // 先 构建一个 最大堆
            buildMaxHeap(array,array.length);
            for(int i=array.length-1;i>0;i--){
                // 交换 堆顶与 最后位置的值,下一次 跳过 已经排好序的 位置
                swap(array,0,i);
                // 递归 重复构建 跳过 排好序的位置后的 最大堆
                buildMaxHeap(array,i);
            }
        }
        // 根据传入的 数组 构建一个 最大堆 ,最大堆只能保证堆顶是最大值,那么堆排序就是每次取最值后然后调整堆为最大堆
        public static void buildMaxHeap(int [] array,int heapSize){
            // 从 总节点数的 1/2 之一处开始 逆序 调整 最大堆
            for(int i=heapSize/2;i>0;i--){
                maxHeapify(array,heapSize,i);
            }
        }
        public static void maxHeapify(int [] array,int heapSize,int rootIndex){
            //左叶子结点
            int l = 2*rootIndex;
            // 右叶子结点
            int r = l+1;
            // 调整需要的最大值指向
            int largest = rootIndex;
            //如果 左叶子结点比父节点要大 ,那么修改 largest 指向为 l
            if(l<=heapSize && array[l-1]>array[rootIndex-1]){
                largest = l;
            }
            //如果 右叶子结点比父节点要大 ,那么修改 largest 指向为 r
            if(r<=heapSize && array[r-1]>array[largest-1]){
                largest = r;
            }
            // 如果 最大值不是 父节点,那么交换 最大值指向与父节点的位置
            if(largest!=rootIndex){
                swap(array,largest-1,rootIndex-1);
                if(largest<=heapSize/2){
                    // 如果该父节点有子节点,那么对 子节点也递归做 最大堆调整
                    maxHeapify(array,heapSize,largest);
                }
            }
    
        }
        // 交换 数组中的值
        public static void swap(int [] array,int i,int j){
            int temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }
    }
    
    

    罗召勇数据结构与算法网易课

    排序算法

    package com.algorithms;
    
    import java.util.Arrays;
    
    /**
     * @author :Frank Li
     * @date :Created in 2019/10/21 9:35
     * @description:${description}
     * @modified By:
     * @version: $version$
     */
    public class Sorter {
    
        public static void main(String[] args) {
            int [] arr = {10,9,8,7,6,5,4,3,2,1};
            System.out.println(Arrays.toString(arr)+"
    ");
    //        quickSort(arr, 0, arr.length-1);
    //        shellSort(arr);
    //        selectSort(arr);
    //        insertSort(arr);
            bubbleSort(arr);
            System.out.println(Arrays.toString(arr));
        }
    
        public static void quickSort(int [] arr, int leftBound, int rightBound){
            if(leftBound>=rightBound){return;}
            int mid = partition(arr, leftBound, rightBound);
            quickSort(arr, leftBound, mid-1);
            quickSort(arr, mid+1, rightBound);
    
        }
    
        public static int partition(int[] arr, int leftBound, int rightBound){
            int left = leftBound;
            int base = rightBound - 1;
            int right = rightBound;
    
            while(left < right){
                while(left<=right && arr[left] < arr[base]) {left++;}
                while(left<=right && arr[right] > arr[base]) {right--;}
                if(left<right){swap(arr, left, right);}
            }
    
            // 换比较的 基准值
            if(arr[left]<arr[base]) {swap(arr, left, base);}
    
            return left;
        }
    
        public static void swap(int[] arr, int left, int right){
            int tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
        }
    
        public static void shellSort(int [] arr){
            int h = 1;
            while(h < arr.length /3){
                h = h*3 + 1;
            }
    
            for(int gap=h; gap>0; gap=(gap-1)/3){
    
                for(int i=gap; i<arr.length; i++){
                    for(int j=i; j-gap>=0; j-=gap){
                        if(arr[j] < arr[j-gap]) {
                            swap(arr, j, j-gap);
                        }
                    }
    
                }
            }
        }
    
        public static void insertSort(int [] arr){
            for(int i=1; i<arr.length; i++){
                for(int j=i;j-1>=0;j--){
                    if(arr[j] < arr[j-1]){
                        swap(arr, j, j-1);
                    }
                }
            }
        }
    
        public static void selectSort(int [] arr) {
            for (int i = 0; i < arr.length; i++) {
                for (int j = i + 1; j < arr.length; j++) {
                    if (arr[i] > arr[j]) {
                        swap(arr, i, j);
                    }
                }
    
            }
        }
    
        public static void bubbleSort(int [] arr){
    
            for(int i=0; i<arr.length; i++){
                for(int j=0; j<arr.length-i -1; j++){
                    if(arr[j] > arr[j+1]){
                        swap(arr, j, j+1);
                    }
                }
            }
        }
    
    }
    
    
    如果有来生,一个人去远行,看不同的风景,感受生命的活力。。。
  • 相关阅读:
    redis
    sqlalchemy ORM
    元类的理解
    python连接mysql
    ffmpeg去水印
    ffmpeg给视频加文字水印
    yt-seo-checklist
    ffmpeg下载直播流
    ffmpeg拼接mp4视频
    ffmpeg截取视频
  • 原文地址:https://www.cnblogs.com/Frank99/p/10107965.html
Copyright © 2011-2022 走看看