快排,做个笔记

        static void Main(string[] args)
        {
            var list = new List<int>() { 3, 4, 1, 2, 6, 5, 7 };
            QuickSort(list, 0, list.Count - 1); 
            foreach (var item in list)
            {
                Console.WriteLine(item);
            }

        } 

        public static int Division(List<int> list, int left, int right)
        {
            while (left < right)
            {
                int num = list[left];
                if (num > list[left + 1])
                {
                    list[left] = list[left + 1];
                    list[left + 1] = num;
                    left++;
                }
                else {
                    int temp = list[right];
                    list[right] = list[left + 1];
                    list[left + 1] = temp;
                    right--;
                }
            }
            return left;
        }
        public static void QuickSort(List<int> list, int left, int right)
        {
            if (left < right) {
                int i = Division(list, left, right);
                QuickSort(list, i + 1,right);
                QuickSort(list, left, i - 1);
            }
        } 

 

posted @ 2018-08-17 14:10  顾星河  阅读(182)  评论(0编辑  收藏  举报