首页 > c#, 数据结构与算法 > 输入10个数字,应用快速排序算法用c#实现

输入10个数字,应用快速排序算法用c#实现

请用c#写出一个快速排序算法,要求输入10个整数,输出排序结果。
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuickSorter
{
class Program
{
static void swap(ref int a, ref int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
static void quickSort(int[] arr, int left, int right)
{
int i, j, s;
if (left < right)
{
i = left - 1;
j = right + 1;
s = arr[(i + j) / 2];//划分基准线(i + j) / 2的取值
while (true)
{
while (arr[++i] < s) ;
while (arr[--j] > s) ;
if (i >= j)
break;
swap(ref arr[i], ref arr[j]);
}
quickSort(arr, left, i - 1);//对左区间递归排序
quickSort(arr, j + 1, right);//对右区间递归排序

}
}
static void Main(string[] args)
{
int[] arr = { 6, 1, 5, 3, 7, 11, 35, 6, 12, 82, 12, 39, 28, 39, 50 };
// int[] arr = { 6, 1, 5, 3, 7, 11, 35, 6, 12, 82, 12, 39, 28, 39, 50 };
// int[] arr = { 6, 1, 5, 3, 6, 10, 55, 9, 12, 87, 12, 34, 75, 33, 47 };
quickSort(arr, 0, arr.Length - 1);
Console.WriteLine("快速排序结果");
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
}
}
}

原创文章转载请注明出处:云飞扬IT的blog

本文链接: http://www.ajaxcn.net/archives/283

分类: c#, 数据结构与算法 标签: ,
一键分享到:新浪微博分享  分享到网易微博    转贴到开心网  推荐到豆瓣  分享到QQ空间    RSS订阅
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.