如何在数据上使用剪接和二进制搜索来删除和搜索

发布于 2025-02-07 21:04:11 字数 1720 浏览 1 评论 0原文

我正在尝试使用剪接方法,但它无法正常工作以删除一个用户,但它正在删除2。

还想添加二进制搜索按钮,

这是二进制搜索的要求

使用二进制搜索算法替换顺序访问算法以在数组上操作。 为了实现此目的,必须对数组的数据进行排序。您可以在array.prototype.sort()中参考一个在线示例,以了解在数组中对数据进行分类。

 function Display_Athlete() {
        var text = "<hr/>";
        for (let i = 0; i < athlete_name.length; i++) {
            text += "Athlete No - " + (i + 1) + ",  Athlete Name is  " + athlete_name[i]
                + "   and height is " + athlete_height[i] + "<br>";
        }
        document.getElementById("message").innerHTML = text;
    }

    //Created function to remove the user
    function Remove_Athlete() {
        var person = prompt("Enter the name to remove the Athlete");
        for (let i = 0; i < athlete_name.length; i++) {
            athlete_name.splice(person - 1, 1);
            athlete_height.splice(person - 1, 1);
            alert("  Athlete_name " + athlete_name + "   Athlete_height  " + athlete_height + "  is removed  ");
        }
    }

    //Create the function to find  the user 
    function Find_Athlete() {
        var person = prompt("Enter the name to Find the Athlete");
        var text = "";
        for (let i = 0; i < athlete_name.length; i++) {
            if (athlete_name[i] == person) {
                text += "Athlete No - " + (i + 1) + ",  Athlete Name is  " + athlete_name[i]
                    + "   and height is " + athlete_height[i] + "<br>";
            }
        }
        document.getElementById("message").innerHTML = text;
        if (text == "")
            alert(`${person} Invalid Athlete name`);
        return "";
    }

    function Binary_Search(){
        
    }

I am trying to the splice method but it is not working properly want to delete one user but it is deleting 2.

Also want to add the binary search button

this is the requirement for binary search

Replace the sequential access algorithm to operate on the arrays with a binary search algorithm.
To implement this, the array’s data must be sorted. You may refer to an online example available at Array.prototype.sort() to learn about sorting data in arrays.

 function Display_Athlete() {
        var text = "<hr/>";
        for (let i = 0; i < athlete_name.length; i++) {
            text += "Athlete No - " + (i + 1) + ",  Athlete Name is  " + athlete_name[i]
                + "   and height is " + athlete_height[i] + "<br>";
        }
        document.getElementById("message").innerHTML = text;
    }

    //Created function to remove the user
    function Remove_Athlete() {
        var person = prompt("Enter the name to remove the Athlete");
        for (let i = 0; i < athlete_name.length; i++) {
            athlete_name.splice(person - 1, 1);
            athlete_height.splice(person - 1, 1);
            alert("  Athlete_name " + athlete_name + "   Athlete_height  " + athlete_height + "  is removed  ");
        }
    }

    //Create the function to find  the user 
    function Find_Athlete() {
        var person = prompt("Enter the name to Find the Athlete");
        var text = "";
        for (let i = 0; i < athlete_name.length; i++) {
            if (athlete_name[i] == person) {
                text += "Athlete No - " + (i + 1) + ",  Athlete Name is  " + athlete_name[i]
                    + "   and height is " + athlete_height[i] + "<br>";
            }
        }
        document.getElementById("message").innerHTML = text;
        if (text == "")
            alert(`${person} Invalid Athlete name`);
        return "";
    }

    function Binary_Search(){
        
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文