快速排序算法C#

来源:百度知道 编辑:UC知道 时间:2024/05/17 04:27:33
using System;
using System.Collections.Generic;
using System.Text;

namespace 快速排序
{
public class QuickSorter
{
public void Swap(int a, int b)
{
int c=0;
a = c;
a = b;
b = c;
}
public void QuickSort(int[] a, int p, int r)
{
if (p < r)
{
int q = Partition(a, p, r);
QuickSort(a, p, q - 1);
QuickSort(a, q + 1, r);
}
}
public int Partition(int[] a, int p, int r)
{
int i = p, j = r+1;
int x = a[p];
while (true)
{
while (a[++i] < x && i < r) ;
while (a[--j] > x) ;
if (i >= j) break;
Swap(a[i],a[j]);

虽然没细看,但你至少有一个问题
你的Swap函数没起作用,因为int是struct的,引用传递
所以你定义Swap时要写成
public void Swap(ref int a, ref int b){}
那么使用的时候也要用Swap(ref a[i], ref a[j]);