zoukankan      html  css  js  c++  java
  • 简单算法实现 java

    • 冒泡排序
        //冒泡排序
        public static void demo(int[] arr) {
            for (int i = 0; i < arr.length - 1; i++) {
                for (int j = 0; j < arr.length - 1 - i; j++) {
                    if (arr[j] > arr[j + 1]) {
                        int temp = arr[j + 1];
                        arr[j + 1] = arr[j];
                        arr[j] = temp;
                    }
                }
            }
            System.out.println(Arrays.toString(arr));
        }
    • 选择排序
        //选择排序
        public static int[] xzDemo(int[] arr) {
            for (int i = 0; i < arr.length - 1; i++) {
                for (int j = i + 1; j < arr.length; j++) {
                    if (arr[i] > arr[j]) {
                        int temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;
                    }
                }
            }
            return arr;
        }
    • 二分查找 这个数组必须是排好序的(从大到小或者从小到大)  
        //二分查找
        public static int towDome(int[] arr, int key) {
            int left = 0;
            int right = arr.length - 1;
            while (left <= right) {
                int middle = (left + right) / 2;
                if (arr[middle] == key) {
                    System.out.println(middle);
                    return middle;
                } else if (arr[middle] > key) {
                    right = middle - 1;
                } else {
                    left = middle + 1;
                }
            }
            return -1;
        }
  • 相关阅读:
    【AGC010E】Rearranging(博弈,图论,拓扑排序)
    【ARC074F】Lotus Leaves(最小割)
    【ARC069F】Flags(2-SAT,Tarjan,线段树优化建图)
    [CTS 2019] 氪金手游
    HDU
    LOJ
    LOJ
    [TJOI 2015] 概率论
    [AGC 018F] Two Trees
    LOJ
  • 原文地址:https://www.cnblogs.com/xiaozhang666/p/13153063.html
Copyright © 2011-2022 走看看