排序算法:快速排序

来源:互联网 发布:淘宝未18购买虚拟物品 编辑:程序博客网 时间:2024/05/17 19:57

快速排序和冒泡排序成为交换排序:

1.找到一个值:然后把比他大的放到一边,比他小或者相等的放到一边;

2.利用分治法再分别对比他大的子说组和比他小的子数组分别排序,依次低轨到最后没元素。

空间复杂度:O()



using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication5{    class Program    {        static void Main(string[] args)        {            int[] s=new int[]{108, 332, 2,3,5,5,8,1,18,7};            QuickSort q = new QuickSort();            q.QuickSortMethod(s, 0, s.Length - 1);            foreach(int e in s)            Console.WriteLine(e);        }        public class QuickSort        {            public void QuickSortMethod(int[] list, int start, int end)                        {                if (list == null || list.Length <= 0) return;                int s=start;                int  e=end;                if (s >= e)                {                    return;                }                                int temp = list[s];                                while(s<e)                {                                       while(temp<=list[e] && s<e)                    {                        e--;                    }                    if(s==e)                    {                        break;                    }                    list[s] = list[e];                    s++;                    while(temp>list[s] && s<e)                    {                        s++;                    }                    if(s==e)                    {                        break;                    }                    list[e] = list[s];                }                list[s] = temp;                QuickSortMethod(list, start, s - 1);                QuickSortMethod(list, s + 1, end);            }                    }    }}

0 0
原创粉丝点击