zoukankan      html  css  js  c++  java
  • Android 常用算法

    排序算法

    简单排序算法

    冒泡排序

    • 两两比较相邻记录的关键字,如果反序则交换,直到没有反序的记录为止

    直接插入排序

    • 通过 n-i 次关键字间的比较,从 n-i+1 个记录中选出关键字最小的记录,并和第 i 个记录交换

    简单选择排序

    改进算法

    快速排序(冒泡排序的改进)

    • 先随机选择一个记录,比它大的放在右边,比它小的放在左边,采用递归的方式进行排序
    • java 代码
    /**
     * 快排,先找一个记录,把大于他的放在右边,小的放在左边,然后采用递归的方式进行排序
     */
    public class QuickSort2 {
    
        public void quickSort(int[] array) {
            if (array.length > 0) {
                doQuickSort(array, 0, array.length - 1);
            }
        }
    
        private void doQuickSort(int[] array, int left, int right) {
            if (left >= right) {
                return;
            }
            int low = left;
            int high = right;
            int temp = array[low];
            while (low != high) {
                while (low < high && array[high] > temp) {
                    high--;
                }
                array[low] = array[high];
                while (low < high && array[low] < temp) {
                    low++;
                }
                array[high] = array[low];
            }
            array[high] = temp;
            Utils.printArray(array);
            doQuickSort(array, left, low - 1);
            doQuickSort(array, high + 1, right);
        }
    
        public static void main(String[] args) {
            QuickSort2 qs = new QuickSort2();
            int[] a = {9, 1, 5, 8, 3, 7, 4, 6, 2};
            qs.quickSort(a);
        }
    }
    
    • 测试结果
    2 1 5 8 3 7 4 6 9 -
    1 2 5 8 3 7 4 6 9 -
    1 2 4 3 5 7 8 6 9 -
    1 2 3 4 5 7 8 6 9 -
    1 2 3 4 5 6 7 8 9 -
    

    希尔排序

    堆排序

    归并排序

    查找算法

    • 二分查找

    常见数据结构题目

    • 翻转链表
      • 通过栈实现翻转
        public static void reverseNode(Node head) {
            Stack<Node> stack = new Stack<>();
            while (head != null) {
                stack.push(head);
                head = head.next;
            }
            while (!stack.isEmpty()) {
                System.out.println(stack.pop().data + "");
            }
        }
    
    - 通过Arraylist,然后倒叙输出
    
        public static void reverseNodeByArrayList(Node head) {
            ArrayList<Node> list = new ArrayList<>();
            while (head != null) {
                list.add(head);
                head = head.next;
            }
            for (int i = list.size()-1 ; i >= 0; i--) {
                System.out.println(list.get(i).data);
            }
        }
    
    • 两个栈实现一个队列
    • 两个队列实现一个栈
    • string 转 int
    • 合并 k 个已排序的链表
    • 给已排序的链表去重
      • 思路: 当前节点跟下一个节点的值做比较,相等的话,就把当前节点指向下一个节点的下一个节点,把下一个节点去除了,就把重复的元素去掉了。
      • Java 代码
        public static Node deleteDuplicatesNodeFromLinkedlist(Node head) {
            if (head == null) {
                return null;
            }
            Node preNode = head;
            Node currentNode = head.next;
            while (currentNode != null) {
                if (currentNode.data == preNode.data) {
                    preNode.next = currentNode.next;
                } else {
                    preNode = currentNode;
                }
                currentNode = preNode.next;
            }
            return head;
        }
    

    参考:https://www.jianshu.com/p/ae97c3ceea8d

  • 相关阅读:
    解决:Could not resolve archetype org.apache.maven.archetypes
    Spring MVC配置MyBatis输出SQL
    Spring集成MyBatis 通用Mapper以及 pagehelper分页插件
    关于SpringMVC或Struts2接受参数接收不到的原因
    配置quartz启动时就执行一次
    ajaxFileUpload进行文件上传时,总是进入error
    spring mvc注入配置文件里的属性
    java中将一个文件夹下所有的文件压缩成一个文件
    flume failed to start agent because dependencies were not found in classpath
    ubuntu不能安装pip unable to install pip in unbuntu
  • 原文地址:https://www.cnblogs.com/liyiran/p/9263919.html
Copyright © 2011-2022 走看看