排序算法

冒泡排序

class Program {
        static int[] arr = new int[] { 3, 1, 2, 0, 9, 6, 7, 5, 8, 4 };
        static void Main(string[] args)
        {
            BubbleSort();
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine(arr[i]);
            }
            Console.ReadKey();
        }
        static void BubbleSort()
        {
            for (int i = 0; i < arr.Length-1; i++)
            {
                for (int j = i + 1; j < arr.Length; j++)
                {
                    if (arr[j] < arr[i])
                    {
                        int temp;
                        temp = arr[j];
                        arr[j] = arr[i];
                        arr[i] = temp;
                    }
                }
            }
        }
    }

选择排序

    class Program
    {
        static int[] arr = new int[] { 3, 1, 2, 0, 9, 6, 7, 5, 8, 4 };
        static void Main(string[] args)
        {
            SelectSort();
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine(arr[i]);
            }
            Console.ReadKey();
        }

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

                if(temp != i)
                {
                    int t = arr[i];
                    arr[i] = arr[temp];
                    arr[temp] = t;
                }
            }
        }
    }

插入排序

    class Program {
        static int[] arr = new int[] { 3, 1, 2, 0, 9, 6, 7, 5, 8, 4 };
        static void Main(string[] args)
        {
            InsertSort();
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine(arr[i]);
            }
            Console.ReadKey();
        }

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

快速排序

class Program
    {
        static int[] arr = new int[] { 3, 1, 2, 0, 9, 6, 7, 5, 8, 4 };
        static void Main(string[] args)
        {
            QuickSort(arr, 0 , arr.Length-1);
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine(arr[i]);
            }
            Console.ReadKey();
        }

        static void QuickSort(int[] array, int low, int high)
        {
            if (low < high)
            {  
                int i = low, j = high, value = array[low];
                while (i < j)
                {
                    while (i < j && array[j] >= value) // 从右向左找第一个小于x的数 
                        j--;
                    if (i < j)
                        array[i++] = array[j];

                    while (i < j && array[i] < value) // 从左向右找第一个大于等于x的数 
                        i++;
                    if (i < j)
                        array[j--] = array[i];
                }
                array[i] = value;
                QuickSort(array, low, i - 1); // 递归调用 
                QuickSort(array, i + 1, high);
            }
        }
    }
点赞