怎样输出count,麻烦大家看
大家好,我想弄个计数器计算排序的比较次数,可是无法输出count,老是报错说"The name 'count' does not exist in the current context"不知道该怎样改啊。麻烦大家看看我把程序贴出来了using System;
using
using System.Data;
namespace coreQuickSort
{
public class QuickSort
{
private void Swap(ref int i, ref int j)
//swap two integer
{
int t;
t = i;
i = j;
j = t;
}
public QuickSort()
{ }
public void Sort(int[] list)
{
Sort(list, 0, list.Length - 1);
}
public void Sort(int[] list, int low, int high)
{
int count;
count = 0;
if (high <= low)
{
//only one element in array list
//so it do not need sort
return;
}
else if (high == low + 1)
{
//means two elements in array list
//so we just compare them
if (list[low] > list[high])
{
//exchange them
Swap(ref list[low], ref list[high]);
return;
}
count++;
}
//more than 3 elements in the arrary list
//begin QuickSort
myQuickSort(list, low, high);
}
public void myQuickSort(int[] list, int low, int high)
{
if (low < high)
{
int pivot = Partition(list, low, high);
myQuickSort(list, low, pivot - 1);
myQuickSort(list, pivot + 1, high);
}
}
private int Partition(int[] list, int low, int high)
{
//get the pivot of the arrary list
int pivot;
pivot = list[low];
while (low < high)
{
while (low < high && list[high] >= pivot)
{
high--;
}
if (low != high)
{
Swap(ref list[low], ref list[high]);
low++;
}
while (low < high && list[low] <= pivot)
{
low++;
}
if (low != high)
{
Swap(ref list[low], ref list[high]);
high--;
}
}
return low;
}
public class MainClass
{
public static void Main()
{
int[] iArrary = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
QuickSort sh = new QuickSort();
sh.Sort(iArrary);
for (int m = 0; m < iArrary.Length; m++)
Console.Write("{0} ", iArrary[m],count);
Console.WriteLine();
}
}
}
}