using System;
namespace Algorithm.AFind
{
public class Example04:IComMethod
{
int[] a1 = { 8, 9, 7, 5, 4, 3, 10, 12, 1, 2, 21, 66, 43};
public Example04()
{
}
private int sepCompare(int[]a, int lo, int hi){
// Console.WriteLine("---");
int key = a[lo]; // 当次排序当首位
while (lo < hi){
while ((a[hi] >= key) && (hi > lo)){
hi = hi - 1; //上标往前移动
}
// 交换,为了把值分按key分成两部分
int tmp = a[lo];
a[lo] = a[hi];
a[hi] = tmp;
Console.WriteLine(tmp + " " + a[lo] + " " + a[hi]);
while((a[lo] <= key) && (lo < hi)){
lo = lo + 1; // 下标往后移
}
int tmp2 = a[hi];
a[hi] = a[lo];
a[lo] = tmp2;
// 交换,为了把值分按key分成两部分
}
return hi;
}
private void sort(int[]a, int lo, int hi){
// Console.WriteLine("test");
if(lo >= hi){
return;
}
// 将分成的两部分进行递归直到每次传递的值左右标相等为止
int index = sepCompare(a, lo, a.Length - 1);
sort(a, lo, index - 1);
sort(a, index + 1, a.Length - 1);
}
private void printIntGroup(int[]a){
Console.WriteLine("输出");
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
}
public void Run()
{
sort(a1, 0, a1.Length - 1);
printIntGroup(a1);
}
public void Run(int key)
{
}
}
}
学习笔记,排序算法(快速排序)
猜你喜欢
转载自www.cnblogs.com/BXLH/p/10446246.html
今日推荐
周排行