c#实现折半查找(winform程序)

折半查找类


     //折半查找
     //____________________________________________________________________________
     //____________________________________________________________________________
     //___________________________________________________________________________

     public int BinarySearch(int[] a, int key, int n)
     {
         int low = 0;
         int high = n - 1;
         while (low <= high)//当前区间存在元素时循环
         {
             int mid = (low + high) / 2;//求查找区间的中间位置
             if (a[mid] == key)//查找成功,返回其逻辑序号mid+1
             {
                 return mid + 1;
             }

             if (a[mid] < key)//继续查找
             {
                 low = mid + 1;
             }
             else
             {
                 high = mid - 1;
             }
         }

         return -1;
     }

冒泡排序类

//冒泡排序
 //____________________________________________________________________________
 //____________________________________________________________________________
 //___________________________________________________________________________

 class MP
 {
     public int[] mp(int[] list)
     {
         for (int i = 0; i < list.Length - 1; i++)
         {
             for (int j = 0; j < list.Length - 1 - i; j++)//一趟中找出最小关键字的元素
             {
                 if (list[j] > list[j + 1])//反序时交换
                 {
                     int t = list[j];
                     list[j] = list[j + 1];
                     list[j + 1] = t;
                 }
             }

         }
         return list;
     }

窗口设计

在这里插入图片描述

代码实现

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sqlist
{
    public partial class zhebanchazhao : Form
    {
        MP m = new MP();
        string[] temp;
        int[] list;
        public zhebanchazhao()
        {
            InitializeComponent();
        }
      
        //创建顺序表
        private void button1_Click(object sender, EventArgs e)
        {
            string str = textBox1.Text.Trim();
            if (str == "")
                MessageBox.Show("请输入元素,否则无法进行运算", "信息提示", MessageBoxButtons.OK);
            else
            {
                string[] split = str.Split(new Char[] { ' ', ',', '.', ':' });//视线多种符号分割字符串
                temp = textBox1.Text.Split(new[] { ' ', ',', '.', ':', '	' }, StringSplitOptions.RemoveEmptyEntries);
                list = Array.ConvertAll<string, int>(temp, s => int.Parse(s));
                label5.Text = "操作提示:成功创建顺序表";
            }
        }

        //排序并输出有序顺序表
        private void button2_Click(object sender, EventArgs e)
        {
            //输出信息
            if (textBox1.Text == "")
            {
                MessageBox.Show("请先输入元素,再进行输出", "信息提示", MessageBoxButtons.OK);
                textBox2.Text = "";
            }
            int[] list1 = m.mp(list);
            foreach (int item in list1)
            {
                textBox2.Text += (item + "   ");
            }
        }

        //查找序号
        private void button4_Click(object sender, EventArgs e)
        {
            int key = Convert.ToInt32(textBox4.Text);
            textBox5.Text = m.BinarySearch(m.mp(list), key, Convert.ToInt32(list.Length)).ToString();
        }

        //清空
        private void button3_Click(object sender, EventArgs e)
        {
            //清空顺序表
            textBox1.Text = " ";
            textBox2.Text = " ";
        }
    }
}

效果展示

在这里插入图片描述
程序下载(包含顺序表、单链表、顺序栈、冒泡排序、折半查找):https://download.csdn.net/download/HAIIAKU/20672183

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风风疯风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值