学习型 ASP/PHP/ASP.NET 主机 30元/年全能 ASP/PHP/ASP.NET 主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付
发新话题
打印

怎样输出count,麻烦大家看

怎样输出count,麻烦大家看

大家好,我想弄个计数器计算排序的比较次数,可是无法输出count,老是报错说"The name 'count' does not exist in the current context"不知道该怎样改啊。麻烦大家看看我把程序贴出来了

using System;

using System.IO;

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();
            }
        }

    }
}

TOP

public class QuickSort
    {
          static int count=0;
           // count = 0;
        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)
        {
            
            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++;
            }

TOP

你的count是方法变量,在另一个方法里当然访问不到了!参照LS的就对了!

TOP

能通过编译了还是无法输出count啊

TOP

Console.Write("{0} ", iArrary[m],count);

TOP

我这么写的呀

TOP

你不觉得有问题吗?

TOP

发新话题