黑马学习笔记_常用算法复习

  ——- android培训java培训、期待与您交流! ———-
  
快速排序

public class QuickSort {
    // 划分数组
    public static int partition(long arr[], int left, int right, long point) {
        int leftPtr = left - 1;
        int rightPtr = right;
        while (true) {
            //向后移动索引
            while (leftPtr < rightPtr && arr[++leftPtr] < point)
                ;
            //向前移动索引
            while (leftPtr < rightPtr && arr[--rightPtr] > point)
                ;
            if (leftPtr >= rightPtr) {
                break;
            } else {
                long temp = arr[leftPtr];
                arr[leftPtr] = arr[rightPtr];
                arr[rightPtr] = temp;
            }
        }
        long temp = arr[leftPtr];
        arr[leftPtr] = arr[right];
        arr[right] = temp;
        return leftPtr;
    }

    // 递归实现快速排序
    public static void sort(long[] arr, int left, int right) {
        if (right <= left) {
            return;
        } else {
            long point = arr[right];
            int partition = partition(arr, left, right, point);
            sort(arr, left, partition - 1);
            sort(arr, partition + 1, right);
        }
    }
    //显示数组
    public static void display(long[] arr) {
        for (long num : arr) {
            System.out.print(num + " ");
        }
        System.out.println();
    }
}
测试类

public class TestSort {
    public static void main(String[] args) {
        long[] arr=new long[4];
        arr[0]=5;
        arr[1]=4;
        arr[2]=3;
        arr[3]=2;
        for(long num : arr){
            System.out.print(num + " ");
        }
        //调用快速排序
        BubbleSort.sort(arr);
        System.out.println();
        //遍历显示数组内容
        for(long num : arr){
            System.out.print(num + " ");
        }
    }
}

二叉树的基本操作

public class Tree {
    // 跟节点
    public Node root;

    // 插入节点
    public void insert(long value, String sValue) {
        // 封装节点
        Node newNode = new Node(value, sValue);
        // 引用当前节点
        Node current = root;
        Node parent = null;
        // 判断root为null
        if (root == null) {
            root = newNode;
            return;
        } else {
            while (true) {
                parent = current;
                if (value < current.data) {
                    current = current.leftNode;
                    if (current == null) {
                        parent.leftNode = newNode;
                        return;
                    }
                } else {
                    current = current.rightNode;
                    if (current == null) {
                        parent.rightNode = newNode;
                        return;
                    }
                }
            }
        }
    }

    // 查找节点
    public Node find(long value) {
        Node current = root;
        while (current.data != value) {
            if (value < current.data) {
                current = current.leftNode;
            } else {
                current = current.rightNode;
            }
            if (current == null) {
                return null;
            }
        }
        return current;
    }

    // 前序遍历
    public void frontOrder(Node localnode) {
        if (localnode != null) {
            System.out.println(localnode.data + "---" + localnode.sData);
            frontOrder(localnode.leftNode);
            frontOrder(localnode.rightNode);
        }
    }

    // 中序遍历
    public void inOrder(Node localnode) {
        if (localnode != null) {
            inOrder(localnode.leftNode);
            System.out.println(localnode.data + "," + localnode.sData);
            inOrder(localnode.rightNode);
        }
    }

    // 后序遍历
    public void afterOrder(Node localnode) {
        if (localnode != null) {
            afterOrder(localnode.leftNode);
            afterOrder(localnode.rightNode);
            System.out.println(localnode.data + "," + localnode.sData);
        }
    }
}
测试类

public class TestTree {
    public static void main(String[] args) {
        Tree tree = new Tree();
        tree.insert(12,"Jamea");
        tree.insert(7,"ddd");
        tree.insert(20,"Kobi");
        tree.insert(30,"my");
        tree.insert(10,"Kobi");
        tree.insert(3,"my");
//      System.out.println(tree.root.data);
//      System.out.println(tree.root.leftNode.data);
//      Node node = tree.find(30);
//      System.out.println(node.data+","+node.sData);
        tree.afterOrder(tree.root);
    } 
}

最后的总结:

刚开始学习算法时候,思路清晰,就是写不出来,后来经常练习渐渐地也就能够把自己思路用代码体现出来了,所以以后我会经常写博客,把自己的想法记录下来,也算是巩固以前学习的知识点了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值