C#快速排序算法

来源:互联网 发布:黑莓转制软件原理 编辑:程序博客网 时间:2024/05/29 04:46

唉 算法功底太差了~~

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{    class Class1    {        public static void Main(string[] avg)        {            int[] source = new int[100];            Random r = new Random();            for (int i = 0; i < source.Length; i++)            {                source[i] = r.Next(1000);                Console.WriteLine(source[i]);            }            sort(source, 0, source.Length - 1);            Console.WriteLine();            foreach (var temp in source)            {                Console.WriteLine(temp);            }            Console.ReadLine();        }        public static void sort(int[] source, int startPoint, int endPoint)        {            if (startPoint < endPoint)            {                int i=partrn(source, startPoint, endPoint);                sort(source, startPoint, i - 1);                sort(source, i + 1, endPoint);            }        }        public static void swap(int[] source, int left, int right)        {            source[left] = source[right] + source[left];            source[right] = source[left] - source[right];            source[left] = source[left] - source[right];        }        public static int partrn(int[] source, int left, int right)        {            int pivot = source[left];            while (left < right)            {                while (left < right && source[right] >= pivot)                    right--;                if (left < right)                {                    swap(source, left, right);                }                while (left < right && source[left] <= pivot)                    left++;                if (left < right)                {                    swap(source, left, right);                }            }            source[left] = pivot;            return left;        }    }}