1. 程式人生 > >拉格朗日插值、快速排序

拉格朗日插值、快速排序

namespace 拉格朗日插值法
{
    class Program
    {
        private static int Cha(int[] shuzhu, int key)
        {
            int left = 0;  //陣列中起始位置下標
            int right = shuzhu.Length - 1; //陣列最後一位下標
            int middle = -1;//查詢不到
            while (left <= right)
            {
                middle = left + (right - left) * (key - shuzhu[left]) / (shuzhu[right] - shuzhu[left]);
                if (key == shuzhu[middle])
                {
                    return middle;
                }
                else if (key > shuzhu[middle])
                {
                    left = middle + 1;
                }
                else
                {
                    right = middle - 1;
                }
            }
            return -1;
        }
        static void Main(string[] args)
        {
            int[] num = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,45,46,48};
            int a = Cha(num, 4);//查詢陣列中數值為4的下標
            Console.WriteLine(a);
            Console.ReadLine();
        }
    }
}

  快速排序

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


namespace 快速排序
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("請輸入待排序數列(以\",\"分割):");
            string _s = Console.ReadLine();
            string[] _sArray = _s.Split(",".ToCharArray());
            int _nLength = _sArray.Length;
            int[] _nArray = new int[_nLength];
            for (int i = 0; i < _nLength; i++)
            {
                _nArray[i] = Convert.ToInt32(_sArray[i]);
            }

            var list = _nArray.ToList();
            QuickSort(list, 0, _nLength - 1);

            foreach (var i in list)
            {
                Console.WriteLine(i.ToString());
            }
            while (true)
            {
                Thread.Sleep(10000);
            }
        }
        //獲取按樞軸值左右分流後樞軸的位置
        private static int Division(List<int> list, int left, int right)
        {
            while (left < right)
            {
                int num = list[left]; //將首元素作為樞軸
                if (num > list[left + 1])
                {
                    list[left] = list[left + 1];
                    list[left + 1] = num;
                    left++;
                }
                else
                {
                    int temp = list[right];
                    list[right] = list[left + 1];
                    list[left + 1] = temp;
                    right--;
                }
                Console.WriteLine(string.Join(",", list));
            }
            Console.WriteLine("--------------\n");
            return left; //指向的此時樞軸的位置
        }
        private static void QuickSort(List<int> list, int left, int right)
        {
            if (left < right)
            {
                int i = Division(list, left, right);
                //對樞軸的左邊部分進行排序
                QuickSort(list, i + 1, right);
                //對樞軸的右邊部分進行排序
                QuickSort(list, left, i - 1);
            }
        }
    }
}

  二分法

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

namespace 二分法查詢
{
    class 二分法
    {

       public static void Main(string[] args)
        {
            int[] y = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
            int rr = BinarySearch(y, 0, y.Length - 1, 13);
            Console.Write(rr);
        }
        public static int BinarySearch(int[] arr, int low, int high, int key)
        {
            int mid = (low + high) / 2;
            if (low > high)
                return -1;
            else
            {
                if (arr[mid] == key)
                    return mid;
                else if (arr[mid] > key)
                    return BinarySearch(arr, low, mid - 1, key);
                else
                    return BinarySearch(arr, mid + 1, high, key);
            }
        }

    }
}