排序算法:快速排序

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

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

            
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值