• 查询算法(一) 顺序查询与折半查询


    顺序查找

    顺序查找(Order Search)比较简单,就是for循环遍历,适用于少量的、无规则的数据查找。

    代码省略......

    折半查找

    折半查找(Binary Search)又称二分查找,其要求数据序列是有序线性的。对于没有顺序的数据序列需要先进行排序在进行查找。折半查找先找到中间值,中间值与要进行比较的值进行比较,比中间值小就向数据序列左进行查找;比中间值大就与之相反。

    代码如下:

    快速排序+折半查找

    快速排序算法代码如下:

    /**
     * 快速排序
     * @author Red Ants(guangboyuan.cn) 
     *         微信公众号:程序员之路 堆排序
     *
     */
    public class QuickSort {
        public static void quickSort(int[] arr ,int left,int right) {
            System.out.println(String.format("left下标: %d,right下标: %d", left,right));
            //存储数组中的中间值作为快速排序的分界值
            int f=arr[(left+right)/2]; 
            
            System.out.println(String.format("分界值下标(left+right)/2: %d 分界值arr[(left+right)/2]: %d",(left+right)/2, f));
            //存储数组的开始和结束为止的下标
            int ltemp=left;
            int rtemp=right;
            int t;
            while (ltemp < rtemp) {
                System.out.println(String.format("ltemp下标:%d  arr[ltemp]: %d",ltemp, arr[ltemp]));
                while(arr[ltemp] < f){
                    ++ltemp;
                    System.out.println(String.format("ltemp下标:%d  arr[ltemp]: %d",ltemp, arr[ltemp]));
                }
                System.out.println(String.format("rtemp下标:%d  arr[rtemp]: %d",rtemp, arr[rtemp]));
                while(arr[rtemp] > f){
                    --rtemp;
                    System.out.println(String.format("rtemp下标:%d  arr[rtemp]: %d",rtemp, arr[rtemp]));
                }
                if(ltemp <= rtemp){
                    t = arr[ltemp];
                    arr[ltemp] = arr[rtemp];
                    arr[rtemp] = t;
                    --rtemp;
                    ++ltemp;
                }
                System.out.println(Arrays.toString(arr));
                if(ltemp == rtemp){
                    ltemp++;
                }
                if(left < rtemp){
                    quickSort(arr, left, ltemp-1);
                }
                if(ltemp < right){
                    quickSort(arr, rtemp+1, right);
                }
            }
        }
        
        public static void main(String[] args) {
            int[] arr = new int[]{20,11,3,7,1,9,22,33,12,11};
            System.out.println(Arrays.toString(arr));
            quickSort(arr, 0, arr.length-1);
            System.out.println(Arrays.toString(arr));
        }
    }

    折半查找如下:

    /**
     * @author Red Ants
     *         微信公众号:程序员之路
     * 折半查找
     */
    public class BinarySearch {
        public static int binarySearch(int[] arr,int x) {
            int low = 0;
            int high = arr.length-1;
            
            while (low <= high) {
                int mid = (low+high)/2;
                if(arr[mid] == x){
                    return mid;
                }else if(arr[mid] > x){
                    high = mid-1;
                }else if(arr[mid] < x){
                    low = mid+1;
                }
            }
            return -1;
        }
        
        public static void main(String[] args) {
            int[] arr = new int[]{3,2,9,1,10,11,20,18};
            QuickSort.quickSort(arr, 0, arr.length-1);
            System.out.println(String.format("数值  %d 所在数组 %s 中的位置是%d", 11,Arrays.toString(arr),binarySearch(arr, 11)));
        }
    }
  • 相关阅读:
    cudnn的下载地址
    rbg大神的主页
    ubuntu16.04上安装深度学习基本框架caffe2 pytorch tensorflow opencv
    linux sublime python
    ubuntu打开终端多开标签的快捷键是ctrl+ shift+ T 对比ctrl+ alt+ T 另外窗口打开一个终端
    在ubuntu1604上使用aria2下载coco数据集效率非常高
    MS coco数据集下载
    qtav----ffmeg在ubuntu和win10上的编译和运行
    论文预印版本的网站 https://arxiv.org/
    pip或者anacnda安装opencv以及opencv-contrib
  • 原文地址:https://www.cnblogs.com/chengxuyuanzhilu/p/6650223.html
Copyright © 2020-2023  润新知