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();
            }
        }

  • 相关阅读:
    python之类的详解
    flask中cookie和session介绍
    Flask数据库的基本操作
    CSRF原理
    Ajax.2
    浅谈Ajax
    Django中的缓存机制
    Django简介
    HTTP协议
    web应用
  • 原文地址:https://www.cnblogs.com/cxd4321/p/797841.html
Copyright © 2011-2022 走看看