码迷,mamicode.com
首页 > 编程语言 > 详细

拉格朗日插值、快速排序

时间:2018-11-10 15:27:30      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:col   arch   shu   div   ati   二分   read   name   main   

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);
            }
        }

    }
}

  

 

拉格朗日插值、快速排序

标签:col   arch   shu   div   ati   二分   read   name   main   

原文地址:https://www.cnblogs.com/tjg33/p/9938679.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!