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

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;

    }

}

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

java 复制代码
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;
    }
}
相关推荐
AI进化营-智能译站几秒前
ROS2 C++开发系列19-枚举定义机器人状态机|随机数生成仿真测试数据流
java·c++·ai·机器人
fengxin_rou1 分钟前
黑马点评项目万字总结:从redis基础到实战应用详解
java·开发语言·分布式·后端·黑马点评
dEso RSET1 分钟前
FrankenPHP实践
java
逸Y 仙X12 分钟前
文章二十:Elasticsearch高亮搜索完全指南
java·大数据·运维·elasticsearch·搜索引擎·全文检索
ulias21220 分钟前
leetcode热题 - 5
数据结构·算法·leetcode
Lyyaoo.24 分钟前
【JAVA Spring面经】Spring 事务失效情况
java·数据库·spring
Funny_AI_LAB30 分钟前
Naval最新播客谈“氛围编码”:Vibe Coding 开启“一人独角兽”时代
人工智能·算法·语言模型·agi
如何原谅奋力过但无声30 分钟前
【灵神高频面试题合集04-05】二分查找
数据结构·python·算法·leetcode
salipopl32 分钟前
Spring Boot 整合 Druid 并开启监控
java·spring boot·后端
我不是懒洋洋34 分钟前
【数据结构】排序算法(直接插入排序、希尔排序、选择排序、堆排序、冒泡排序、快速排序、归并排序、计数排序)
c语言·数据结构·c++·经验分享·算法·排序算法