必考算法總結(java版)

  • 樹的遍歷
  1. 先序遍歷

    遞歸版本

        public static void preorderTraversal(List<Integer> list,TreeNode root){
            if(root==null)
                return;
            list.add(root.value);
            preorderTraversal1(list,root.left);
            preorderTraversal1(list,root.right);
        }

    非遞歸版本

        public static List<Integer> preorderTraversal(TreeNode root){
            Stack<TreeNode> stack=new Stack<>();
            List<Integer> list=new LinkedList<>();
            if(root!=null)  stack.push(root);
            while(!stack.isEmpty()){
                TreeNode cur= stack.pop();
                list.add(cur.value);
                if(cur.right!=null) stack.push(cur.right);
                if(cur.left!=null) stack.push(cur.left);
            }
            return list;
        }

  2. 中序遍歷

    遞歸版

        public static void inorderTraversal(List<Integer> list,TreeNode root){
            if(root==null)
                return;
            inorderTraversal1(list,root.left);
            list.add(root.value);
            inorderTraversal1(list,root.right);
        }
    非遞歸版
        public static List<Integer> inorderTraversal(TreeNode root){
            Stack<TreeNode> stack=new Stack<>();
            List<Integer> list=new LinkedList<>();
            while(root!=null||!stack.isEmpty()){
                if(root!=null){
                    stack.push(root);
                    root=root.left;
                }else {
                    root=stack.pop();
                    list.add(root.value);
                    root=root.right;
                }
            }
            return list;
        }

  3. 後序遍歷

    遞歸版本

        public static void postorderTraversal(List<Integer> list,TreeNode root){
            if(root==null)
                return;
            postorderTraversal1(list,root.left);
            postorderTraversal1(list,root.right);
            list.add(root.value);
        }
    非遞歸版本
        public static List<Integer> postorderTraversal(TreeNode root) {
            Stack<TreeNode> stk = new Stack<TreeNode>();
            if(root != null) stk.push(root);
            LinkedList<Integer> res = new LinkedList<Integer>();
            while(!stk.isEmpty()){
                TreeNode curr = stk.pop();
                // 先添加左後添加右,就是先訪問右後訪問左
                if(curr.left != null) stk.push(curr.left);
                if(curr.right != null) stk.push(curr.right);
                // 反向添加結果,每次加到最前面
                res.offerFirst(curr.value);
            }
            return res;
        }

  4. 層序遍歷
        public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
            ArrayList<Integer> list = new ArrayList<Integer>();
            if(root==null){
                return list;
            }
            Queue<TreeNode> queue = new LinkedList<TreeNode>();
            queue.offer(root);
            while (!queue.isEmpty()) {
                TreeNode treeNode = queue.poll();
                if (treeNode.left != null) {
                    queue.offer(treeNode.left);
                }
                if (treeNode.right != null) {
                    queue.offer(treeNode.right);
                }
                list.add(treeNode.val);
            }
            return list;
        }


  • 排序算法
  1. 快速排序
        public  static void quickSort(int[] n,int left,int right){
            int dp;
            if(left<right){
                dp=partition(n,left,right);
                quickSort(n,left,dp-1);
                quickSort(n,dp+1,right);
                System.out.println(Arrays.toString(n));
            }
        }
    
        public static int partition(int[] n,int left,int right){
            int pivot=n[left];
            while(left<right){
                while(left<right&&n[right]>pivot)
                    right--;
                if(left<right)
                    n[left++]=n[right];
                while (left < right && n[left] <= pivot)
                    left++;
                if (left < right)
                    n[right--] = n[left];
            }
            n[left]=pivot;
            return left;
        }

  2. 歸併排序
        public static void merge(int[] array,int low,int mid,int high){
            int[] temp=new int[high-low+1];
            int i=low;
            int j=mid+1;
            int k=0;
            while(i<=mid&&j<=high){
                if(array[i]<array[j]){
                    temp[k++]=array[i++];
                }else {
                    temp[k++]=array[j++];
                }
            }
            while (i <= mid) {
                temp[k++] = array[i++];
            }
            while (j <= high) {
                temp[k++] = array[j++];
            }
            for (int k2 = 0; k2 < temp.length; k2++) {
                array[k2 + low] = temp[k2];
            }
        }
        public static void mergeSort(int[] array,int low,int high){
            int mid=low+((high-low)>>1);
            if(low<high){
                 mergeSort(array,low,mid);
                mergeSort(array,mid+1,high);
                merge(array,low,mid,high);
                System.out.println(Arrays.toString(array));
            }
        }

  3. 選擇排序
        public static void selectSort(int[] array){
            for(int i=0;i<array.length;i++){
                int k=i;
                for(int j=i+1;j<array.length;j++){
                    if(array[j]<array[k]){
                        k=j;
                    }
                }
                if(i!=k){
                    int temp=array[i];
                    array[i]=array[k];
                    array[k]=temp;
                }
            }
        }

  4. 冒泡排序
        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]){
                        int temp=array[j];
                        array[j]=array[j+1];
                        array[j+1]=temp;
                    }
                }
            }
        }

  5. 插入排序
        public static void insertSort(int[] array){
            for(int i=1;i<array.length;i++){
                int target=array[i];
                int j=i;
                while (j>0&&target<array[j-1]){
                    array[j]=array[j-1];
                    j--;
                }
                array[j]=target;
            }
        }

  6. 堆排序(參考:https://blog.csdn.net/zdp072/article/details/44227317)
        private static void heapSort(int[] arr) {
            // 將待排序的序列構建成一個大頂堆
            for (int i = arr.length / 2; i >= 0; i--){
                heapAdjust(arr, i, arr.length);
            }
    
            // 逐步將每個最大值的根節點與末尾元素交換,並且再調整二叉樹,使其成爲大頂堆
            for (int i = arr.length - 1; i > 0; i--) {
                swap(arr, 0, i); // 將堆頂記錄和當前未經排序子序列的最後一個記錄交換
                heapAdjust(arr, 0, i); // 交換之後,需要重新檢查堆是否符合大頂堆,不符合則要調整
            }
        }
    
        /**
         * 構建堆的過程
         * @param arr 需要排序的數組
         * @param i 需要構建堆的根節點的序號
         * @param n 下標n之前的元素需要調整(原作者說是數組長度,有歧義)
         */
        private static void heapAdjust(int[] arr, int i, int n) {
            int child;
            int father;
            for (father = arr[i]; leftChild(i) < n; i = child) {
                child = leftChild(i);
    
                // 如果左子樹小於右子樹,則需要比較右子樹和父節點
                if (child != n - 1 && arr[child] < arr[child + 1]) {
                    child++; // 序號增1,指向右子樹
                }
    
                // 如果父節點小於孩子結點,則需要交換
                if (father < arr[child]) {
                    arr[i] = arr[child];
                } else {
                    break; // 大頂堆結構未被破壞,不需要調整
                }
            }
            arr[i] = father;
        }
    
        // 獲取到左孩子結點
        private static int leftChild(int i) {
            return 2 * i + 1;
        }
    
        // 交換元素位置
        private static void swap(int[] arr, int index1, int index2) {
            int tmp = arr[index1];
            arr[index1] = arr[index2];
            arr[index2] = tmp;
        }

發佈了28 篇原創文章 · 獲贊 9 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章