[C#]演算法-循序搜尋法(Linear/Sequential Search)

演算法有很多種,包含搜尋、排序等等,很多人都接觸過,在比較龐大的數學運算上真的是十分重要,可以節省很多時間及資源,像是Google、Yahoo 搜尋,都有自行的算法來應付大量的搜尋及資料。

底下先介紹一個常見的搜尋演算法循序搜尋法(Linear/Sequential Search),觀念十分簡單,也常用到,就是直接用迴圈一個個去比對,找到時就跳出(回傳)。

演算過程的畫面如下程式畫面:

數列串是1,2,3,4,5,6,7,然後要查詢5這個數字

搜尋1.png

搜尋2.png

演算法程式碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        public static int linearSearch(int[] list, int item)
        {
            int value = -1;
            for (int i = 0; i < list.Length;i++ )
            {
                if (list[i] == item)
                {
                    value = i;
                    break;
                }
            }
                return value;
        }
        static void Main(string[] args)
        {
            int[] list = {3,6,7,9,3,2,1,10 };
            Console.WriteLine("原始數列串:");
            for (int i = 0; i < list.Length; i++)
            {
                Console.Write(list[i]+" ");
            }
            Console.WriteLine("\r\n請輸入要查詢的數字:");

            int item = System.Convert.ToInt32(Console.ReadLine());
            int index = linearSearch(list,item);
            if (index >= 0)
            {
                Console.WriteLine("在第 "+index+" 位置找到!");
            }
            else
            {
                Console.WriteLine("找不到!");
            }

            Console.ReadLine();
        }
    }
}

執行結果如下:

搜尋3.png

執行動作過程的程式原始碼如下:

Form1

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            string value = this.textBox1.Text;

            string search = this.textBox2.Text;
            if (value.Equals(""))
            {
                MessageBox.Show(this, "請先輸入數字串");
            }
            else if (search.Equals(""))
            {
                MessageBox.Show(this, "請先輸入要查詢數字");
            }
            else
            {
                string[] strnum = value.Split(new Char[]{','});
                int[] num = new int[strnum.Length];
                for (int i = 0; i < num.Length; i++)
                {
                    num[i] = System.Convert.ToInt32(strnum[i]);
                }
                int iSearch = System.Convert.ToInt32(search);
                Form2 form2 = new Form2(num,iSearch);
                form2.Visible = true;
            }
        }
    }
}

Form2

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

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        private int[] num;
        private int search;
        public Form2()
        {
            InitializeComponent();
            num = new int[0];
            search = -1;
        }
        public Form2(int[] num,int search)
        {
            InitializeComponent();
            this.num = num;
            this.search = search;
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            //base.OnPaint(e);
            Font font = new Font("Verdana",14);
            Font font_word = new Font("Verdana", 12);
            SolidBrush brush_check = new SolidBrush(Color.Red);
            SolidBrush brush = new SolidBrush(Color.Blue);
            SolidBrush brush_word = new SolidBrush(Color.Black);
            Graphics g = e.Graphics;
            int top = 20;

            for (int j = 0; j < num.Length; j++)
            {
                top = 20 * (j+1);
                for (int i = 0; i <= j; i++)
                {
                    g.DrawString("找第 " + (j+1) + " 次", font_word, brush_word, 0, top);
                    if (num[i] == search)
                    {
                        g.DrawString(num[i].ToString(), font, brush_check, 30 * i+90, top);//找到
                        g.DrawString("找到了", font, brush_check, 30 * (i+1) + 90, top);//找到
                        return;
                    }
                    else
                        g.DrawString(num[i].ToString(), font, brush, 30 * i+90, top);
                }
            }
            g.DrawString("找不到", font, brush_check, 0, top+20);//找到
        }
       
    }
}

主要是利用Form的Graphics來DrawString,演算法的內容跟前一個範例是一樣的

原始碼下載

One thought to “[C#]演算法-循序搜尋法(Linear/Sequential Search)”

發表迴響