C#快速排序法+冒泡排序法+二分查找法

快速排序法

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

namespace 排序法
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = {5,8,3,6,7,4,9,1,2 }; //待排序数组
            QuickSort(arr, 0, arr.Length - 1);  //调用快速排序函数。传值(要排序数组,基准值位置,数组长度)

            //控制台遍历输出
            Console.WriteLine("排序后的数列:");
            foreach (int item in arr)
                Console.WriteLine(item);
            Console.ReadLine();

        }

        private static void QuickSort(int[] arr, int begin, int end)
        {
            if (begin >= end) return;   //两个指针重合就返回,结束调用
            int pivotIndex = QuickSort_Once(arr, begin, end);  //会得到一个基准值下标h

            QuickSort(arr, begin, pivotIndex - 1);  //对基准的左端进行排序  递归
            QuickSort(arr, pivotIndex + 1, end);   //对基准的右端进行排序  递归
        }

        private static int QuickSort_Once(int[] arr, int begin, int end)
        {
            int pivot = arr[begin];   //将首元素作为基准
            int i = begin;
            int j = end;
            while (i < j)
            {
                //从右到左,寻找第一个小于基准pivot的元素
                while (arr[j] >= pivot && i < j)
                {
                    j--; //指针向前移
                }
                arr[i] = arr[j];  //执行到此,j已指向从右端起第一个小于基准pivot的元素,执行替换

                //从左到右,寻找首个大于基准pivot的元素
                while (arr[i] <= pivot && i < j)
                {
                    i++;  //指针向后移
                }
                arr[j] = arr[i];  //执行到此,i已指向从左端起首个大于基准pivot的元素,执行替换
            }
            //退出while循环,执行至此,必定是 i= j的情况(最后两个指针会碰头)
            //i(或j)所指向的既是基准位置,定位该趟的基准并将该基准位置返回
            arr[i] = pivot;
            return i;
        }
    }
}

冒泡排序

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

namespace 排序法
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arry = { 5, 2, 9, 4, 6, 3, 7, 8, 1 };
            for (int i = 0; i < arry.Length-1; i++)
            {
                for (int j = 0; j < arry.Length-1-i; j++) //一定能把最大的数 放到最后一位
                {
                    if (arry[j] > arry[j + 1])  //只要前一位大于后一位 就调换位置
                    {
                        int temp = arry[j];
                        arry[j] = arry[j + 1];
                        arry[j + 1] = temp;
                    }
                }
            }
            foreach (int s in arry)
            {
                Console.WriteLine(s);
            }
            Console.ReadLine();
        }
    }
}

二分查找法

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

namespace 二分法
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = {1,2,3,4,5,6,7,8,9,10 };//测试的数组
            int value = BinarySearCh(arr,8);//调用方法
            Console.WriteLine(value);
            Console.ReadLine();

        }
        public static int BinarySearCh(int[] arr, int value)//传入数组和需要查找的值
        {
            int fir = 0;//数组下标第一位
            int end = arr.Length - 1;//数组下标最后一位
            while (fir <= end)//下标第一位不能大于最后一位
            {
                int num = (fir + end) / 2;//最中间的索引值
                if (value == arr[num])//传入的值是否跟最中间的相等,相等就返回这个最中间索引值
                {
                    return num;
                }
                else if (value > arr[num])//传入的值大于中间的,相当于需要查找的值在比较大的那一半,所以下一次最小的索引从最中间的数加一开始再次循环
                {
                    fir = num + 1;//
                }
                else//传入的值小于中间的,相当于需要查找的值在比较小的那一半,所以下一次最大的索引从最中间的数减一开始再次循环
                {
                    end = num - 1;
                }
            }
            return -1;//如果没找到就返回-1

        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43140883/article/details/83743177
今日推荐