111,129
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Text;
namespace QuickSort
{
class Program
{
static void Main(string[] args)
{
int[] array ={1,4,10,6,7,5,2,5}; //<<1>>
QuickSort(ref array,0,7); //<<1>>
foreach(int i in array)
{
Console.WriteLine("{0}",i); //<<8>>
}
}
public static void QuickSort(ref int[] arr, int startpos, int endpos)
{
int i = startpos, j = endpos; //<<5>>
int temp; //<<5>>
int tj = endpos; //<<5>>
dat da = new dat(0, 0); //<<5>>
Stack<dat> stk = new Stack<dat>(); //<<5>>
temp = arr[i]; //<<5>>
while (i < j)
{
while (i < j && arr[j] >= temp) --j; //<<7>>
arr[i] = arr[j]; //<<7>>
while (i < j && arr[i] < temp) ++i; //<<7>>
arr[j] = arr[i]; //<<7>>
}
arr[i] = temp; //<<5>>
if (startpos < i - 1) QuickSort(ref arr,startpos,i-1); //<<5>>
if (endpos > i + 1) QuickSort(ref arr, i+1, endpos); //<<5>>
}
/* if (tj > j + 1)
{
stk.Push(new dat(j + 1, tj)); //<<>>
i = startpos; //<<>>
j = j - 1; //<<>>
tj = j; //<<>>
}
else
if(startpos==j-1)
{
da = stk.Pop(); //<<>>
i = da.i; //<<>>
j = da.j; //<<>>
}; //<<>>
} while (j > startpos && stk.Count != 0); //<<>>*/
}
public struct dat
{
public int i;
public int j;
public dat(int a, int b)
{
i = a;
j = b;
}
}
}