| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 674 人关注过本帖
标题:新手请教,谢谢!
只看楼主 加入收藏
sdnd2000
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2008-4-10
收藏
 问题点数:0 回复次数:1 
新手请教,谢谢!
大家好,我现在用C#实现了快排,但是还需要用一个计数器来计算内部的key comparison,是我不太清楚该怎样实现。希望高手教我,我把源码贴出来了,谢谢大家!

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

            //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]);
                Console.WriteLine();
            }
        }

    }
}
搜索更多相关主题的帖子: 计数器 class comparison private public 
2008-04-19 11:33
sdnd2000
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2008-4-10
收藏
得分:0 
不会把,没人会吗?
2008-04-20 09:43
快速回复:新手请教,谢谢!
数据加载中...
 
   



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

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