zoukankan      html  css  js  c++  java
  • 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();
            }
        }

  • 相关阅读:
    蒙特卡洛法(随即取样法) 数模 笔记
    【数模学习】Matlab 符号微积分 计算微分、雅可比矩阵、不定积分与定积分、求解微分方程
    Length of Last Word
    基于视频深度学习的人物行为识别 资料汇总
    3S比赛预定
    求解一元多次方程 牛顿迭代法
    LeetCode | Climbing Stairs
    LeetCode | Palindrome Number
    LeetCode | Merge Sorted Array
    LeetCode | Valid Palindrome
  • 原文地址:https://www.cnblogs.com/cxd4321/p/797841.html
Copyright © 2011-2022 走看看