必考算法总结(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;
        }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值