一天一个小算法的学习之快速排序

  选择排序..排列的数组是arr[0]....arr[n-1],取出任意数据,作为关键数,将所有比它小的数都放该数前面,比它打的数放后面..(多个相同的值相对位置在算法结束时变动)

  代码:

using System;

namespace SortPra
{
    public class QuickSort
    {
        public QuickSort ()
        {
        }

        private static int arrSize;

        public static int[] QuickWithSort (int[] array)
        {
            arrSize = array.Length;
            QuickSortVoid (array, 0, arrSize - 1);
            for (int i = 0; i < array.Length; i++) {
                Console.WriteLine ("快速排序:" + array [i]);
            }
            return array;
        }

        static void QuickSortVoid (int[] array, int leftSize, int rightSize)
        {
            int i, j, s;
            if (leftSize < rightSize) {
                i = leftSize - 1;
                j = rightSize + 1;
                s = array [(i + j) / 2];

                //Console.WriteLine ("j = " + j);
                Console.WriteLine ("s = " + s);
                while (true) {
                    while (array [++i] < s)
                        Console.WriteLine ("array[i] = " + array [i]);
                    ;
                    while (array [--j] > s)
                        Console.WriteLine ("array[j] = " + array [j]);
                    ;
                    if (i >= j)
                        break;
                    Swap (ref array [i], ref array [j]);
                    
                }
                QuickSortVoid (array, leftSize, i - 1);
                QuickSortVoid (array, j + 1, rightSize);
            }
        }

        static void Swap (ref int left, ref int right)
        {
            int temp;
            temp = left;
            left = right;
            right = temp;
        }

    }
}
原文地址:https://www.cnblogs.com/jbw752746541/p/8708248.html