C#冒泡排序&&选择排序&&快速排序

来源:互联网 发布:知乎女神 编辑:程序博客网 时间:2024/06/08 02:06

/*编写一个程序,要求用户从键盘输入3个不同整数,输出中间者。 */

 Console.WriteLine("请同时输入多个数,用空格键隔开:");            //输入的字符串            string str = Console.ReadLine();            //使用空格分隔成字符串数组            string[] str1 = str.Split(' ');            int[] number = new int[str1.Length];            for (int i = 0; i < str1.Length; i++)            {               //强制转换成int 类型                number[i] = Convert.ToInt32 (str1[i]);            }            //冒泡排序//            for (int i = 0; i < number.Length-1; i++)            {                for (int j = 0; j < number.Length-1-i; j++)                {                    if (number[j + 1] < number[j])                    {                        int temp;                        temp = number[j + 1];                        number[j + 1] = number[j];                        number[j] = temp;                    }                }            }            for (int i = 0; i < number.Length; i++)            {                Console.Write(" "+number[i]);            }            //输出中间值            Console.WriteLine(number[number.Length / 2]);

选择排序

//选择排序            //动态定义数组            int[] arr_2 = new int[14] { 6, 7, 8, 5, 3, 6, 345, 345, 234, 123, 45, 67, 12, 4 };            //局部变量            int min;            int minindex;            for (int i=0;i<arr_2.Length-1;i++)            {                min = arr_2[i];//记录最小值                minindex = i; //记录最小值的下标                //再剩下的数组中寻找最小值与 min 比较                for (int j = i+1; j < arr_2.Length; j++)                {                    if (min > arr_2[j])//比较值后,找最小值                    {                        min = arr_2[j];                                                minindex = j;//记录其下标                    }                }                //找到剩下数组中的最小值之后,与min交换                arr_2[minindex] = arr_2[i];                arr_2[i] = min;                           }            //显示答应            for (int i = 0; i < arr_2.Length; i++)            {                Console.Write("{0} ", arr_2[i]);            }            Console.WriteLine();

//快速排序

/*arr = {23,23,13,14,15,45,56}*分割数  k=arr[0]=23    15  14  13  23  23  45  56    *       通过划分  得到两部分   {15,14,13}  23  {23,45,56}* 前一部分的分割数  k=arr[0]=15  {13,14} 15 23 {23,45,56}* 后一部分的分割数  k=arr[4]=23  {13,14 } 15, 23, 23,{45,56}* 之后再获取到分割数划分。。。。。。。        */        public static void Main(string[] args)        {            int[] arr = {23,23,13,14,15,45,56};            QuickSort(arr,0,arr.Length-1);            for (int i = 0; i < arr.Length;i++)                Console.WriteLine(arr[i]);        }        //使用递归执行分割排序        public static void QuickSort(int[] array,int startIndex,int endIndex)        {            if (startIndex > endIndex)                return;            int boundary = Boundary(array, startIndex, endIndex);            //以当前找到的这个数的下标分割            QuickSort(array,startIndex,boundary-1);            QuickSort(array,boundary+1,endIndex);        }        //找到分割位置        public static int Boundary(int[] array,int startIndex,int endIndex)        {            int standary = array[startIndex];            //定义向左、向右的下标遍历的数            int start = startIndex;            int end = endIndex;            while(end>start)            {                //指针从右到左扫描,先右后左这个顺序很重要                while(start<end && array[end]>=standary)                {                    end--;                }                array[start] = array[end];                //这时array[end]这个数的位置空出来了                //指针从左到右扫描                while(start<end && array[start]<standary)                {                    start++;                }                array[end] = array[start];                //上面把array[end]这个空位填上了            }            //把end=start这个空位给分割数standary            /*帮助理解            array[start] = standary;            Console.WriteLine("分割数:"+standary+"  小标:"+start);            for (int i = 0; i <array.Length; i++)                Console.Write(array[i]+"  ");            Console.WriteLine();            */            //返回这个分割数的下标            return start;            //分割结束        }