C#,数据检索算法之跳跃搜索(Jump Search)的源代码

数据检索算法是指从数据集合(数组、表、哈希表等)中检索指定的数据项。

数据检索算法是所有算法的基础算法之一。

本文提供跳跃搜索的源代码。

1 文本格式

using System;

namespace Legalsoft.Truffer.Algorithm
{
    public static class ArraySearch_Algorithm
    {
        /// <summary>
        /// 跳跃搜索
        /// </summary>
        /// <param name="arr"></param>
        /// <param name="x"></param>
        /// <returns></returns>
        public static int Jump_Search(int[] arr, int x)
        {
            int n = arr.Length;
            int step = (int)Math.Sqrt(n);
            int prev = 0;
            while (arr[Math.Min(step, n) - 1] < x)
            {
                prev = step;
                step += (int)Math.Sqrt(n);
                if (prev >= n)
                {
                    return -1;
                }
            }
            while (arr[prev] < x)
            {
                prev++;
                if (prev == Math.Min(step, n))
                {
                    return -1;
                }
            }
            if (arr[prev] == x)
            {
                return prev;
            }
            return -1;
        }
    }
}

代码不重要,思路最重要。

2 代码格式

using System;

namespace Legalsoft.Truffer.Algorithm
{
    public static class ArraySearch_Algorithm
    {
        /// <summary>
        /// 跳跃搜索
        /// </summary>
        /// <param name="arr"></param>
        /// <param name="x"></param>
        /// <returns></returns>
        public static int Jump_Search(int[] arr, int x)
        {
            int n = arr.Length;
            int step = (int)Math.Sqrt(n);
            int prev = 0;
            while (arr[Math.Min(step, n) - 1] < x)
            {
                prev = step;
                step += (int)Math.Sqrt(n);
                if (prev >= n)
                {
                    return -1;
                }
            }
            while (arr[prev] < x)
            {
                prev++;
                if (prev == Math.Min(step, n))
                {
                    return -1;
                }
            }
            if (arr[prev] == x)
            {
                return prev;
            }
            return -1;
        }
    }
}

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-28 06:08:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-28 06:08:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-28 06:08:01       82 阅读
  4. Python语言-面向对象

    2024-01-28 06:08:01       91 阅读

热门阅读

  1. 2023年企业网络安全预算情况分析

    2024-01-28 06:08:01       56 阅读
  2. VUE3 加载自定义SVG文件

    2024-01-28 06:08:01       55 阅读
  3. 模型训练trick篇

    2024-01-28 06:08:01       66 阅读
  4. MySQL封装JDBC为工具类(JDBC简化)

    2024-01-28 06:08:01       56 阅读
  5. MySQL 函数参考手册(MySQL 字符串函数)

    2024-01-28 06:08:01       42 阅读
  6. 中科大--高级数据库期末试卷

    2024-01-28 06:08:01       57 阅读
  7. MySQL 函数参考手册(MySQL 数值函数)

    2024-01-28 06:08:01       35 阅读
  8. mysql数据库的备份和恢复

    2024-01-28 06:08:01       54 阅读
  9. 基础算法--搜索与图论(2)

    2024-01-28 06:08:01       37 阅读
  10. 图论第一天|797.所有可能的路径 200. 岛屿数量

    2024-01-28 06:08:01       67 阅读
  11. vue项目使用element-plus

    2024-01-28 06:08:01       50 阅读
  12. 题记(32)--矩阵K次幂

    2024-01-28 06:08:01       51 阅读
  13. 【LeetCode-435】无重叠区间(贪心)

    2024-01-28 06:08:01       59 阅读
  14. el-tree实现多选、反选、指定选择

    2024-01-28 06:08:01       54 阅读
  15. Compose | UI组件(五) | Button 按钮组件

    2024-01-28 06:08:01       52 阅读
  16. 73. 矩阵置零

    2024-01-28 06:08:01       53 阅读