算法学习笔记之快速排序

来源:互联网 发布:c语言基础知识视频教程 编辑:程序博客网 时间:2024/04/28 14:48
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace AlgorithmTest{    class QuickSort    {        private int Partition ( int[] arr, int p, int r )        {            int x = arr[r];            int j = p -1;            for (int i = p; i < r; i++)            {                if (arr[i] <= x)                {                    j++;                    int temp1 = arr[j];                    arr[j] = arr[i];                    arr[i] = temp1;                }            }            int temp2 = arr[++j];            arr[j] = x;            arr[r] = temp2;            return j;        }        public void Quick_Sort ( int[] arr, int p, int r )        {            if (p < r)            {                int q = Partition(arr, p, r);                Quick_Sort(arr, p, q-1);                Quick_Sort(arr, q + 1, r);            }        }    }}

原创粉丝点击