C#入门之流程控制练习题(排序)

来源:互联网 发布:mac如何制作启动盘 编辑:程序博客网 时间:2024/04/29 12:34

流程控制练习题

排序算法练习

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleAppTest{    class Program    {        //排序        static void SortNum(ref int[] arrInt)        {             //冒泡排序            //for (int i = arrInt.Length; i >= 0; i--)            //{            //    for (int j = 0; j < i - 1; j++)            //    {            //        if (arrInt[j] > arrInt[j + 1])            //        {            //            exchange(ref arrInt[j], ref arrInt[j + 1]);            //        }            //    }            //    printArry(arrInt);            //}            //时间复杂度 O(n^2 )            //选择排序            //            //int min;            //for (int i = 0; i < arrInt.length - 1; i++)            //{            //    min = i;            //    for (int j = i + 1; j < arrInt.length; j++)            //    {            //        //寻找未排序中的最小值            //        if (arrint[j] < arrInt[min])            //        {            //            min = j;            //        }            //    }            //    if (i != min)            //    {            //        exchange(ref arrInt[i], ref arrInt[min]);            //    }            //    printArry(arrInt);            //}            //插入排序            //for (int i = 1; i < arrInt.Length; i++)            //{            //    int t = arrInt[i];//当前未排序的值            //    int j = i;            //    while (j > 0 && arrInt[j - 1] > t)            //    {            //        arrInt[j] = arrInt[j - 1];            //        j--;            //    }            //    arrInt[j] = t;            //    printArry(arrInt);            //}            //快速排序            //printArry(arrInt);            //quickSort(ref arrInt, 0, 3);            //printArry(arrInt);        }        static void quickSort(ref int[] arrInt, int left, int right)        {            if (left > right)            {                return;            }            int key = arrInt[left];            int i = left;            int j = right;                       while (i != j)            {                 //j从右向左开始查找 小于 key的值                while (arrInt[j] >= key && i < j)                {                    j--;                }                //i从左向右查找 大于key的值                while (arrInt[i] <= key && i < j)                {                    i++;                }                //找到后交换两个值                if(i < j)                {                    exchange(ref arrInt[i], ref arrInt[j]);                }                            }            if (arrInt[i] == 0)            {                Console.WriteLine("aaaaaaaa");            }            //找到中点的值 i== j 的时候            //交换关键位置            exchange(ref arrInt[left], ref arrInt[i]);            //递归查找 key左边和右边的数组            quickSort(ref arrInt, left, i - 1);            quickSort(ref arrInt, i + 1, right);        }        //引用传递 ref关键字        static void exchange(ref int a,ref int b)        {            int temp;            temp = a;            a = b;            b = temp;        }        static void printArry(int[] arrInt)        {            foreach (var item in arrInt)            {                Console.Write(string.Format("{0} ", item));                            }            Console.WriteLine("");        }        static void Main(string[] args)        {            Console.WriteLine("(1) 输入四个整数:");            int[] arrInt = new int[4];            for (int i = 0; i < arrInt.Length; i++)            {                arrInt[i] = int.Parse(Console.ReadLine());                Console.WriteLine("arrInt[{0}] = {1}",i, arrInt[i]);            }            SortNum(ref arrInt);            Console.ReadKey();        }    }}



0 0
原创粉丝点击