c#快速排序算法,输入整数,输出排序结果。

代码如下
C# code
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]);
}
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值