找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
宇哥帮你零基础建设外贸独立站
宇哥淘宝虚拟类目-付费微信群
宇哥闲鱼3个月陪跑课
Access数据库-零基础入门课程
Access数据库-自用软件开发课程
Access数据库-即学即用课程
Access数据库-进销存课程
Access数据库-VBA入门课程
Access数据库-陪跑课程
查看: 159|回复: 0

240124-JavaScript编程代码分享:网页版二分查找算法

[复制链接]

23

主题

3

回帖

187

积分

注册会员

积分
187
发表于 2024-2-21 16:09:41 | 显示全部楼层 |阅读模式
本文分享的技术,是用Web做一个二分查找算法的小网页,包括所有的代码。通过二分查找的原理,做一个闭环Web小应用,这个网页包括HTML、CSS和JavaScript的配合。通过制作这个网页,老铁们可以熟悉网页设计、练习算法的落地应用,做到学以致用



最终的二分算法网页效果展示


本文代码分为3个部分:核心JavaScript代码讲解(包括调用函数JS代码);
HTML网页设计讲解;
CSS的美化讲解。
第1部分 JavaScript代码讲解
本案例的JavaScript代码包括核心的算法代码和调用代码。
先介绍核心算法代码:
  1. function binarySearch(nums, target) //定义了一个名为 binarySearch 的函数,
  2. //该函数接受两个参数,nums 是已排序数组,target 是要查找的目标值。
  3. {
  4. let left = 0;//声明两个变量 left 和 right,它们分别代表搜索范围的左边界和右边界。
  5. //初始时,左边界为数组的第一个元素索引(0),右边界为数组的最后一个元素索引。
  6. let right = nums.length - 1;
  7.     while (left <= right) //使用循环来执行二分查找,条件是左边界小于等于右边界。
  8.     {
  9.     let middle = left + Math.floor((right - left) / 2);
  10.     //计算当前搜索范围的中间位置,使用 Math.floor 函数确保取整。
  11.     if (nums[middle] > target) //如果中间位置的元素值大于目标值。
  12.         {
  13.         right = middle - 1;//将右边界更新为中间位置的前一个位置,缩小搜索范围至左半部分。
  14.         }
  15.     else if (nums[middle] < target) //如果中间位置的元素值小于目标值。
  16.         {
  17.         left = middle + 1;//将左边界更新为中间位置的后一个位置,缩小搜索范围至右半部分。
  18.         }
  19.     else
  20.         {
  21.         return middle;//直接返回中间位置的索引,表示找到目标值。
  22.         }
  23.     }
  24.     return -1;
  25. }
复制代码
这段代码的含义是:先定义两个边界索引,左边界初始值为0,右边界初始值为数组的长度。循环条件是左边界的数值小于右边界。

二分查找是很基础的算法,很容易搜索到,在此不做更多的原理讲解。只要记住查找的数组必须有序排列即可。
由于这个算法要在Web端调用,因此还需要进行调用代码的编写:
  1. function calculateBinarySearch()
  2. {
  3.     const inputNums = document.getElementById("output").value.split(" ").map(Number);
  4.     // 获取页面上 id 为 "output" 的元素的值,将其以空格分割成数字数组并映射为数字类型
  5.     const inputTarget = parseInt(document.getElementById("textBox1").value);
  6.     // 获取页面上 id 为 "textBox1" 的元素的值,将其解析为整数,作为二分查找的目标值
  7.     const result = binarySearch(inputNums, inputTarget);
  8.     // 调用二分查找函数 binarySearch,传入数字数组和目标值,并将结果保存在变量 result 中
  9.     if (result !== -1)
  10.     // 如果结果不等于 -1,表示找到了目标值
  11.     {
  12.         document.getElementById("textBox2").value = `数字${inputTarget}排在第${result + 1}位`;
  13.         // 在页面上 id 为 "textBox2" 的元素中显示目标值在数组中的位置(索引 + 1)
  14.     }

  15.     else
  16.     {
  17.        document.getElementById("textBox2").value = "这个数字不存在";
  18.        // 如果结果等于 -1,表示目标值不存在于数组中
  19.     }
  20. }
复制代码
在本案例中,显示25个数字的窗口是一个文本框“output”,一个输入查找数字的文本框“textBox1”和一个输出查找结果的文本框“textBox2”。
调用代码是将随机生成的25个数字提取成数组,把textBox1里输入的数字也进行必要的清洗,然后作为二分查找函数的输入值,进行计算,结算结果显示在textBox2中。显示结果要进行一下必要的美化,做一下人机交互的设计。
如何生成25个随机数字,我会在另一篇文章仔细讲解,在此不做赘述。
第2部分 HTML代码讲解
本案例的HTML包括一个标题文件(其中包含一个LOGO图片)、1个展示25个数字的文本框output,1个输入文本框textBox1和1个输出文本框textBox2。还有两个按钮,分别控制生成25个数字和清除25个数字。
  1. <h1>
  2.     <img src="logo.png" alt="Logo" width="130" height="130">JavaScript编程练习:二分算法演示
  3. </h1>

  4. <textarea id="output" readonly>
  5. </textarea>
  6. <textarea id="textBox2" readonly>
  7. </textarea>

  8. <br>
  9. <div class="button-container">
  10.     <button onclick="generateNumbers()">生成数字</button>
  11.     <button onclick="clearText()">清空数字</button>
  12. </div>

  13. <input type="text" id="textBox1">

  14. <div class="color-box-container">
  15. <div class="color-box" id="whiteBox" onclick="changeBackgroundColor('white')"></div>
  16. <div class="color-box" id="blueBox" onclick="changeBackgroundColor('rgba(247, 160, 255, 0.348)')"></div>
  17. <div class="color-box" id="pinkBox" onclick="changeBackgroundColor('rgba(202, 251, 189, 0.966)')"></div>
  18. </div>

  19. <a href="index.html">
  20.     <button id="backToHomeButton">回到主页</button>
  21. </a>
复制代码
第3部分 CSS代码讲解
CSS代码考虑到了手机竖屏的应用。注意按钮使用了群组,本案例还有颜色代码块,纯装饰用,不是必须的。
  1. <head>
  2. <meta name="viewport" content="width=device-width, initial-scale=0.5">
  3. <meta charset="UTF-8">
  4. <title>二分算法</title>
  5. <style>         
  6. @media screen and (max-width: 768px)
  7.     {                     
  8.     #output
  9.         {
  10.         position: static;
  11.         width: 80%; /* 调整文本框宽度 */
  12.         top:20%;
  13.         text-align: left;
  14.         }

  15.     .button-container
  16.         {
  17.         position: fixed;
  18.         margin-top: 20px; /* 调整按钮与文本框的间距 */
  19.         display: flex;
  20.         flex-direction: column;
  21.         align-items: left;
  22.         }

  23.     #textBox1
  24.         {
  25.         position: static;
  26.         width: 80%; /* Adjust the width as needed */
  27.         margin-top: 20px;
  28.         }

  29.     #textBox2
  30.         {
  31.         position: static;
  32.         width: 80%; /* Adjust the width as needed */
  33.         margin-top: 50px;
  34.         }
  35.     }

  36. #output/*输出的文本框格式*/
  37. {
  38.     width: 350px;
  39.     height: 350px;
  40.     font-size: 45px;
  41.     font-family: Arial, Helvetica, sans-serif;
  42.     color: blue;
  43.     font-weight: bold;
  44.     position: fixed;
  45.     bottom: 15%;
  46.     left: 15%;
  47.     display: flex;
  48. }

  49. #textBox1
  50. {
  51.     width: 200px;
  52.     height: 100px;
  53.     font-size: 72px;
  54.     font-family: Arial, Helvetica, sans-serif;
  55.     color: rgb(74, 0, 0);
  56.     font-weight: bold;
  57.     position: fixed;
  58.     bottom: 45%;
  59.     left: 45%;
  60.     display: flex;
  61. }

  62. #textBox2
  63. {
  64.     width: 200px;
  65.     height: 200px;
  66.     font-size: 36px;
  67.     font-family: Arial, Helvetica, sans-serif;
  68.     color:rgb(74, 0, 0);
  69.     font-weight: bold;
  70.     position: fixed;
  71.     bottom: 15%;
  72.     left: 45%;
  73.     display: flex;
  74. }

  75. #textBox1::placeholder
  76. {
  77.     font-size: 32px; /* 或者可以使用具体的数值,例如 font-size: 12px; */
  78.     vertical-align: middle;
  79. }

  80. .button-container /*按钮群组,不用群组不好管理*/
  81. {
  82.     position: fixed;
  83.     bottom: 30%;
  84.     left: 70%;
  85.     display: flex;
  86.     flex-direction: column;
  87. }

  88. body /*主题格式*/
  89. {
  90.     display: flex;
  91.     flex-direction: column;
  92.     align-items: center;
  93.     font-family: Arial, sans-serif;
  94. }

  95. h1 /*标题*/
  96. {
  97.     display: flex;
  98.     font-size: 66px;
  99.     color: blue;
  100.     margin-top: 20px;
  101.     align-items: center;
  102. }

  103. img /*图片通用格式*/
  104. {
  105.     margin-right: 20px;
  106.     margin-left: 50px;
  107. }

  108. button
  109. {
  110.     margin-top: 25px;
  111.     margin-right: 25px;
  112.     font-size: 36px;
  113.     color: white;
  114.     background-color: blue;
  115.     border: none;
  116.     padding: 10px 50px;
  117.     cursor: pointer;
  118. }

  119. button:hover
  120. {
  121.     background-color: #2980b9;
  122. }   

  123. #bottomRightImage
  124. {
  125.     position: fixed;
  126.     bottom: 10px;
  127.     right: 10px;
  128.     margin: 10px;
  129.     width: auto;
  130.     height: 10%;
  131. }

  132. #bottomLeftImage
  133. {
  134.     position: fixed;
  135.     bottom: 0px;
  136.     left: 0px;
  137.     margin: 0px;
  138.     width: auto;
  139.     height: 50%;
  140. }

  141. .color-box-container
  142. {
  143.     position:fixed;
  144.     bottom:0;
  145.     margin-bottom: 20px;
  146.     align-items: center;
  147.     display: flex;
  148. }   

  149. .color-box
  150. {
  151.     width: 50px;
  152.     height: 50px;
  153.     margin: 10px;
  154.     border: 2px solid black;
  155.     cursor: pointer;
  156. }

  157. #whiteBox
  158. {
  159.     background-color: white;
  160. }

  161. #blueBox
  162. {
  163.     background-color: rgba(247, 160, 255, 0.348);
  164. }

  165. #pinkBox
  166. {
  167.     background-color: rgba(202, 251, 189, 0.966);
  168. }

  169. #backToHomeButton
  170. {
  171.     height: 50px;
  172.     position:fixed;
  173.     bottom:0;
  174.     margin-bottom: 30px;
  175.     left: 30px;
  176.     font-size:30px;
  177.     color:white;
  178.     background-color: blue;
  179.     border:none;
  180.     padding:5px 15px;
  181.     cursor:pointer;
  182. }

  183. #backToHomeButton:hover
  184. {
  185.     background-color: #2980b9;
  186. }
  187. </style>
  188. </head>
复制代码




您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Access零基础
Access即学即用
access陪跑
access开发
Access进销存

QQ|小黑屋|宇哥编程论坛 ( 京ICP备2022024677号-2|京公网安备11011202100561号 )

GMT+8, 2024-5-20 07:24 , Processed in 0.064710 second(s), 22 queries .

Powered by 宇哥

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表