快速排序法 拉格朗日插值法 二分法

快速排序法(泛型)

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


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
拉格朗日插值法(C++语言)

 <pre name="code" class="cpp">void search2(int a[M], int num)
{
	int tou = 0;
	int wei = M - 1;
	int zhong;
	int flag = -1;//一开始找不到
	int  ci = 0;
	while (tou <= wei)
	{
		ci++;
		//zhong =tou+ ( wei-tou) / 2;
		zhong = tou + (wei - tou) *1.0*(num - a[tou]) / (a[wei] - a[tou]);
		printf("%d   %d   %d   %d\n", tou, wei, zhong, ci);
		if (num == a[zhong])
		{
			printf("一共查找%d次    找到:a[%d]=%d\n", ci, zhong, num);
			flag = 1;//找到
			break;
		}
		else if (num > a[zhong])
		{
			tou = zhong + 1;
		}
		else
		{
			wei = zhong - 1;
		}
	}
	if (-1 == flag)
	{
		printf("没有找到\n");
	}
}
//二分查找 拉格朗日插值查找
void main()
{
	int a[M];
	for (size_t i = 0; i < M; i++)
	{
		a[i] = i;
		printf("%d    ", i);
	}
	printf("\n请输入要查找的数据:");
	int num;
	scanf("%d", &num);
	search2(a, num);
	system("pause");
}

====================================================================================================================================================================

二分法

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





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);    //12

猜你喜欢

转载自blog.csdn.net/hennysky/article/details/83588507