基于C#的快速排序算法?

syhnjs 2003-07-19 12:03:57
如下
using System;
namespace ConsoleApplication7
{
public class Class1
{
public int [] a=new int[9] {8,6,3,5,4,9,1,2,7};
public int lefth,leftt,righth,rightt;
public void quicksort(int h,int t)
{
if(h<t)
{
partion(h,t);
quicksort(lefth,leftt);
quicksort(righth,rightt);
}
}
//quicksort end
public void partion(int h,int t)
{
int pivot;
int i,j;
int extr;
i=h;
j=t;
pivot=a[(i+j)/2];
do
{
while(a[i]<pivot)
i++;
while(a[j]>pivot)
j--;
if(i<=j)
{
extr=a[i];
a[i]=a[j];
a[j]=extr;
i++;
j--;
}
}while(i<=j);
lefth=h;
leftt=j;
righth=i;
rightt=t;
}
public void writea()
{
int a1;
for(a1=0;a1<=8;a1++)
Console.WriteLine("the array is {0}",a[a1]);
}
}
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class MainClass
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]

//partion end
static void Main(string[] args)
{
Class1 doit=new Class1();
doit.quicksort(0,8);
doit.writea();
Console.Read();
}
}
}

结果为何不正确亚?
...全文
143 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
saucer 2003-07-19
  • 打赏
  • 举报
回复
int [] a=new int[9] {8,6,3,5,4,9,1,2,7};
System.Array.Sort(a);
foreach (int i in a)
Console.WriteLine(i);

panyee 2003-07-19
  • 打赏
  • 举报
回复
123459687?????
saucer 2003-07-19
  • 打赏
  • 举报
回复
using System;
namespace ConsoleApplication7
{
public class Class1
{
public static void QuickSort (int[] intArray, int nLower, int nUpper)
{
if (nLower < nUpper)
{
int nSplit = Partition (intArray, nLower, nUpper);
QuickSort (intArray, nLower, nSplit - 1);
QuickSort (intArray, nSplit + 1, nUpper);
}
}

static int Partition (int[] intArray, int nLower, int nUpper)
{
int nLeft = nLower + 1;
int nPivot = intArray[nLower];
int nRight = nUpper;

int nSwap;
while (nLeft <= nRight)
{

while (nLeft <= nRight && intArray[nLeft] < nPivot)
nLeft++;

while (nLeft <= nRight && intArray[nRight] >=nPivot )
nRight--;

if (nLeft < nRight)
{
nSwap = intArray[nLeft];
intArray[nLeft] = intArray[nRight];
intArray[nRight] = nSwap;
nLeft++;
nRight--;
}
}

// Move pivot element
nSwap = intArray[nLower];
intArray[nLower] = intArray[nRight];
intArray[nRight] = nSwap;
return nRight;
}
}

public class MainClass
{
static void Main(string[] args)
{
int[] a= new int[9] {8,6,3,5,4,9,1,2,7};

Class1.QuickSort(a,0,a.Length-1);

foreach (int i in a)
Console.WriteLine(i);

Console.ReadLine();
}
}
}

Godshow 2003-07-19
  • 打赏
  • 举报
回复
C#下的System.Array.Sort()就是快速排序啊。
panyee 2003-07-19
  • 打赏
  • 举报
回复
帮你改了一下:

using System;
namespace ConsoleApplication7
{
public class Class1
{
public int [] a=new int[9] {8,6,3,5,4,9,1,2,7};

public void quicksort(int h,int t)
{
if(h<t)
{
partion(h,t);

}
}
//quicksort end
public void partion(int h,int t)
{
int pivot;
int i,j;
int extr;
i=h;
j=t;
pivot=a[(i+j)/2];
do
{
while(a[i]<pivot)
i++;
while(a[j]>pivot)
j--;
if(i<=j)
{
if(i != j)
{
extr=a[i];
a[i]=a[j];
a[j]=extr;
}
i++;
j--;
}
}while(i<=j);

if(i<t)
quicksort(i,t);
if(j>h)
quicksort(h,j);
}

public void writea()
{
int a1;
for(a1=0;a1<=8;a1++)
Console.WriteLine("the array is {0}",a[a1]);
}
}
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class MainClass
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]

//partion end
static void Main(string[] args)
{
Class1 doit=new Class1();
doit.quicksort(0,8);
doit.writea();
Console.Read();
}
}
}

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧