Searching: Uniform Binary Search-1

介绍了一种名为均匀二分搜索(Uniform Binary Search)的算法,该算法能够在已排序的记录表中查找特定键值。通过逐步调整搜索区间,直至找到目标键值或确认不存在于表中。提供了一个Java实现示例。

Algorithm U

Algorithm U (Uniform binary search). Given a table of records R1, R2,…, RN
whose keys are in increasing order K1 < K2< … < KN, this algorithm searches
for a given argument K. If N is even, the algorithm will sometimes refer to a
dummy key K0 that should be set to -Negative Infinite (or any value less than K). We assume
that N >= 1.
U1. [Initialize.] Set i <– ceil(N/2), m <– floor(N/2).
U2. [Compare.] If K < Ki, go to U3; if K > Ki, go to U4; and if K = Ki, the
algorithm terminates successfully.
U3. [Decrease i] (We have pinpointed the search to an interval that contains
either m or m-1 records; i points just to the right of this interval.) If m = 0,
the algorithm terminates unsuccessfully. Otherwise set i <– i-ceil(m/2); then
set m <– floor(m/2) and return to U2.
U4. [Increase i.] (We have pinpointed the search to an interval that contains
either m or m-1 records; i points just to the left of this interval.) If m = 0,
the algorithm terminates unsuccessfully. Otherwise set i <– i+ceil(m/2); then
set m <– floor(m/2) and return to U2. |


Comparison tree

这里写图片描述


Java program

In this program, R1,…,RN were simplified to K1,…,KN.

/**
 * Created with IntelliJ IDEA.
 * User: 1O1O
 * Date: 12/10/13
 * Time: 6:52 PM
 * :)~
 * Uniform Binary Search-1:Searching
 */
public class Main {

    public static void main(String[] args) {
        int N = 16;
        int[] K = new int[17];

        /*Prepare the ordered data table*/
        K[1] = 61;
        K[2] = 87;
        K[3] = 154;
        K[4] = 170;
        K[5] = 275;
        K[6] = 426;
        K[7] = 503;
        K[8] = 509;
        K[9] = 512;
        K[10] = 612;
        K[11] = 653;
        K[12] = 677;
        K[13] = 703;
        K[14] = 765;
        K[15] = 897;
        K[16] = 908;

        /*Output sorted Ks*/
        System.out.println("Sorted Ks:");
        for(int i=1; i<=N; i++){
            System.out.println(i+":"+K[i]);
        }
        System.out.println();

        /*Kernel of the Algorithm!*/
        int Key = 653;                  /*Key to be found*/
        K[0] = -10000;                /*Negative Infinite*/
        int i = (int)Math.ceil((double)N/2);
        int m = (int)Math.floor((double)N/2);

        do{
            if(Key < K[i]){
                if(m == 0){
                    System.out.println("Outputs: "+Key+" not found.");
                    break;
                }else {
                    i = i-(int)Math.ceil((double)m/2);
                    m = (int)Math.floor((double)m/2);
                }
            }else if(Key > K[i]){
                if(m == 0){
                    System.out.println("Outputs: "+Key+" not found.");
                    break;
                }else {
                    i = i+(int)Math.ceil((double)m/2);
                    m = (int)Math.floor((double)m/2);
                }
            }else {
                System.out.println("Outputs: "+Key+" in K["+i+"].");
                break;
            }
        }while (true);
    }
}

Outputs

Sorted Ks:
1:61
2:87
3:154
4:170
5:275
6:426
7:503
8:509
9:512
10:612
11:653
12:677
13:703
14:765
15:897
16:908

Outputs: 653 in K[11].

Reference

<< The art of computer programming: Sorting and Searching >> VOLUME 3, DONALD E. KNUTH

11:47:50: Information - CPreRequisiteInstallerApp::InitializeLog(273) - * Version: 25.3.10.69 11:47:50: Information - CPreRequisiteInstallerApp::InitializeLog(275) - * Build Date: Jul 7 2025 11:47:50: Information - CPreRequisiteInstallerApp::InitializeLog(276) - * Build Time: 03:52:09 11:47:50: Information - CPreRequisiteInstallerApp::InitializeLog(277) - * Command Line: OfflineInstaller 11:47:50: Information - CPreRequisiteInstallerApp::Run(308) - Prerequisite Installation via UI is triggered. Progress dialog will show up now.. 11:47:50: Information - PreRequisiteUI::ProgressDialog(82) - PreRequsiteUI.cpp : Entry to progress dialog function 11:47:50: Information - CPreRequisiteVerification::IsDotNetInstalled(98) - Checking if .NET Framework is present with Min Release No 528040 11:47:50: Information - CPreRequisiteVerification::IsDotNetInstalled(121) - The.NET Framework requirement satisfied 11:47:50: Information - CPreRequisiteVerification::IsDotNetCoreInstalled(65) - Searching for Desktop Runtime 8.0.15, currently found 8.0.15 11:47:50: Information - CPreRequisiteVerification::IsDotNetCoreInstalled(79) - Found Desktop Runtime 8.0.15 that is greater than or equal to 8.0.15, the .net 8 requirement is satisfied 11:47:50: Information - CPreRequisiteVerification::IsRequireToInstallVCRedist(131) - Checking for installed VC Redist 11:47:50: Information - CPreRequisiteVerification::IsRequireToInstallVCRedist(136) - Found the installed VC Redist version details as 14.42.34433.00, 14.44.35208.00 11:47:50: Information - CPreRequisiteVerification::IsEdgeWebView2Installed(155) - Checking if Microsoft Webview2 Runtime is present on the system. 11:47:50: Information - CPreRequisiteVerification::IsEdgeWebView2Installed(219) - Status of Edge runtime on system : 1 11:47:50: Information - InstallationWorkerFunction(17) - Installation completed ... 11:47:52: Information - CPreRequisiteInstallerApp::ExitInstance(369) - Exit Code = 0
最新发布
07-10
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值