| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1229 人关注过本帖
标题:怎样输出count,麻烦大家看
只看楼主 加入收藏
sdnd2000
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2008-4-10
收藏
 问题点数:0 回复次数:6 
怎样输出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();
            }
        }

    }
}
搜索更多相关主题的帖子: count 计数器 using int 麻烦 
2008-04-20 12:34
冢骨
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2008-3-14
收藏
得分:0 
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++;
            }
2008-04-20 13:05
gtrgtr
Rank: 1
等 级:新手上路
帖 子:58
专家分:0
注 册:2007-9-4
收藏
得分:0 
你的count是方法变量,在另一个方法里当然访问不到了!参照LS的就对了!
2008-04-20 15:10
sdnd2000
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2008-4-10
收藏
得分:0 
能通过编译了还是无法输出count啊
2008-04-20 21:06
blueskyss
Rank: 1
来 自:湖北
等 级:新手上路
帖 子:81
专家分:0
注 册:2008-2-19
收藏
得分:0 
Console.Write("{0} ", iArrary[m],count);
2008-04-21 13:19
sdnd2000
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2008-4-10
收藏
得分:0 
我这么写的呀
2008-04-21 20:50
blueskyss
Rank: 1
来 自:湖北
等 级:新手上路
帖 子:81
专家分:0
注 册:2008-2-19
收藏
得分:0 
你不觉得有问题吗?
2008-04-22 18:56
快速回复:怎样输出count,麻烦大家看
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018313 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved