当前位置: 首页 > news >正文

数据结构----------顺序查找,折半查找和分块查找(java实现)



import java.util.Arrays;

//顺序查找法
public class Main {
    public static void main(String[] args) {
        //查找表
        int[] arr = {4, 3, 5, 1, 2};
        System.out.print("5在数组中的索引:");
        System.out.println(SearchSeq(arr, 5));

        Arrays.sort(arr);
        System.out.print("2在数组中的索引:");
        System.out.println(BinarySearch(arr, 2));
        System.out.print("6在数组中的索引:");
        System.out.println(BinarySearch(arr, 6));

        arr = new int[]{24, 21, 6, 11, 8, 22, 32, 31, 54, 72, 61, 78, 88, 83};
        System.out.print("88在数组中的索引:");
        System.out.println(PartitionSearch(arr, 88));

    }

    //顺序查找
    public static int SearchSeq(int[] arr, int key) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == key)
                return i;
        }
        return -1;
    }

    //折半查找,有序序列
    public static int BinarySearch(int[] arr, int key) {
        int low = 0, high = arr.length - 1, mid;

        while (low <= high) {
            mid = (low + high) / 2;
            if (arr[mid] == key) {
                return mid;
            } else if (arr[mid] < key) {
                low = mid + 1;
            } else high = mid - 1;
        }

        return -1;


    }

    //分块查找,块内无序,块间有序
    //过程模拟
    public static int PartitionSearch(int[] arr, int key) {
        //对arr手动分块,已知arr的情况

        IndexTable indexTable1=new IndexTable();
        IndexTable indexTable2=new IndexTable();
        IndexTable indexTable3=new IndexTable();
        IndexTable indexTable4=new IndexTable();

        IndexTable []tables={indexTable1,indexTable2,indexTable3,indexTable4};

        tables[0].setIndex(0);
        tables[0].setMaxData(24);
        tables[1].setIndex(6);
        tables[1].setMaxData(54);
        tables[2].setIndex(9);
        tables[2].setMaxData(78);
        tables[3].setIndex(12);
        tables[3].setMaxData(88);

        tables[0].setCount(6);
        tables[1].setCount(3);
        tables[2].setCount(3);
        tables[3].setCount(2);

        for (int i = 0; i < tables.length; i++) {
            if (key == tables[i].getMaxData()|| key<tables[i].getMaxData()&&i==0) {
                for (int j = tables[i].getIndex(); j < tables[i].getIndex() + tables[i].getCount(); j++) {
                    if (arr[j] == key)
                        return j;
                }
            }
        }


        for (int i = 0; i < tables.length - 1; i++) {
            if (key > tables[i].getMaxData() && key < tables[i + 1].getMaxData()) {
                for (int j = tables[i + 1].getIndex(); j < tables[i + 1].getIndex() + tables[i + 1].getCount(); j++) {
                    if (arr[j] == key)
                        return j;
                }
                return -1;
            }


        }
        return -1;

    }

}

创建一个索引表对象,用来分块



public class IndexTable {
    private int maxData;
    private int index;

    private int count;


    public IndexTable() {
    }

    public int getMaxData() {
        return maxData;
    }

    public void setMaxData(int maxData) {
        this.maxData = maxData;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}

相关文章:

  • 解决conda命令报错CondaSSLError(mac也适用)
  • 机器视觉3D中激光偏镜的优点
  • 基于α-β剪枝的含禁手AI五子棋
  • MySQL的存储引擎有哪些,它们之间有什么区别?
  • Github 2025-04-06 php开源项目日报 Top10
  • nginx创建成功但是进不去页面
  • ArcGIS Pro新增功能:在3D场景中增加天气系统,晴天、阴天、雨天、下雪、有雾
  • 202520 | 微服务
  • 代码仓库使用git lfs上传模型文件
  • sentinel新手入门安装和限流,热点的使用
  • Github跑通GenSAM历程-GenSAM介绍与问题记录
  • go程序启动工具——cobra
  • 【力扣hot100题】(063)搜索二维矩阵
  • 项目开发流程总结
  • MySQL完整版基础
  • RabbitMQ概述和安装
  • leetcode每日一题:最大整除子集
  • 87.在线程中优雅处理TryCatch返回 C#例子 WPF例子
  • ResNet改进(19):基于PyTorch的ResNet改进方案详解:Mish激活+SPP模块+MixUp数据增强
  • Perl 条件语句
  • 为客户网站做产品描述/手机软文广告300字
  • 网站如何做竞价/每日新闻简报
  • 软件开发项目方案/seo搜索引擎优化试题及答案
  • 怎么给网站做https/湖南seo网站策划
  • 深圳专业做网站较好的公司/微信营销平台
  • wordpress 园林模板/提升seo排名平台