c#的常用排序

class Program
    {
        static void Main(string[] args)
        {
            int[] arr ={ 23,3, 2, 5, 6, 1, 234, 4, 345, 567, 7 };

            print(arr);

            //QuickSort(arr,0,arr.Length-1);
            selectSort(arr);

            print(arr);           
        }

        private static void selectSort(int []arr)
        {
            for (int i = 0; i < arr.Length - 1;i++ )
            {
                for (int j = i + 1; j <=arr.Length - 1;j++ )
                {
                    if(arr[i]>arr[j])
                    {
                        int temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;
                    }
                }
            }
        }

        private static void QuickSort(int[] arr,int low,int high)
        {
            if(low<high)
            {
                int pivotLoc = partialQuickSort(arr,low,high);
                QuickSort(arr,low,pivotLoc-1);
                QuickSort(arr,pivotLoc+1,high);
            }
        }

        private static int partialQuickSort(int[] arr, int low, int high)
        {           
            int pivot = arr[low];
            int pivotIndex = low;
            while (low < high)
            {
                while (low < high && arr[high] >= pivot)
                {
                    high--;
                }

                int temp = arr[pivotIndex];
                arr[pivotIndex] = arr[high];
                arr[high] = temp;

                pivotIndex = high;

                while (low < high && arr[low] <= pivot)
                {
                    low++;
                }

                temp = arr[pivotIndex];
                arr[pivotIndex] = arr[low];
                arr[low] = temp;

                pivotIndex = low;
            }

            return pivotIndex;
        }       

        private static void BubbleSort(int []arr)
        {
            for (int i = 0; i < arr.Length-1; i++)
            {
                bool isChange = false;
                for (int j = 0; j < arr.Length - 1-i; j++)
                {
                    if (arr[j]<arr[j+1])
                    {
                        isChange = true;
                        int temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                    }
                }
                if(!isChange)
                {
                    return;
                }
            }
        }

        private static void print(int []arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                if (i != arr.Length - 1)
                {
                    Console.Write("{0,3},", arr[i].ToString().PadLeft());
                }
                else
                {
                    Console.Write("{0,3}", arr[i]);
                }
            }
            Console.WriteLine();
        }
    }

转载于:https://www.cnblogs.com/cxd4321/archive/2007/06/27/797841.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值