下载中心┊文章中心┊软件分类┊更新软件┊推荐下载┊热门软件┊更新文章┊热门文章
 
《拓文asp.net网站内容管理系统》官方演示站。
 设为首页    
 收藏本站
 作者:黄振安
 
网站首页 开源拓文 .NET编程 网站编程 网页设计 图形图像 数据库 软件工程留言本
    站内搜索:   
您现在的位置: 拓文网络 → 文章中心 → .NET编程 → C#.NET → C#快速排序类[正文]

热 门 文 章

相 关 文 章
  • C#常用函数和方法集汇总
  • c#中分割字符串的几种方法(sp...
  • C#调用父类的父类的方法
  • 计算俩个时间之间隔多少月
  • 批量字符串替换程序-1
  • 批量字符串替换程序-2
  • C#中利用SharpZipLib进行文件...
  • c#学习体会:使用 ref 和 out ...
  • C#.NET 中的类型转换
  • 如何通过一个图片的URL得到该...
  • C#快速排序类
     
    [作者:不详    转贴自:不详    点击数:    字体:【大 中 小】    更新时间:2006-3-16 9:59:42]

    快速排序的基本思想是基于分治策略的。对于输入的子序列ap..ar,如果规模足够小则直接进行排序,否则分三步处理:

    分解(Divide):将输入的序列ap..ar划分成两个非空子序列ap..aq和aq+1..ar,使ap..aq中任一元素的值不大于aq+1..ar中任一元素的值。 
    递归求解(Conquer):通过递归对p..aq和aq+1..ar进行排序。 
    合并(Merge):由于对分解出的两个子序列的排序是就地进行的,所以在ap..aq和aq+1..ar都排好序后不需要执行任何计算ap..ar就已排好序。 
    这个解决流程是符合分治法的基本步骤的。因此,快速排序法是分治法的经典应用实例之一。

    using System;

    namespace VcQuickSort
    {
     /// <summary>
     /// ClassQuickSort 快速排序。
     /// 范维肖
     /// </summary>

     public class QuickSort
     {
      public QuickSort()
      {
      }

      private void Swap(ref int i,ref int j)
      //swap two integer
      {
       int t;
       t=i;
       i=j;
       j=t;
      }
      
      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;


    页次:1/1  1篇/页  首页   上一页   下一页   尾页  合计 1 篇 Go:
     
    上一篇:C#中利用SharpZipLib进行文件...
    下一篇:c#学习体会:使用 ref 和 out ...
    winner 打印本页 关闭窗口

         文 章 评 论
    评论人 查看该文章的评论 发表时间
       笔 名* : 0 50分 60分 70分 80分 90分 100分
    评论内容 *: (最多600字。当前字数:0)


    80 分经济计划呷呷2006-4-3 16:34:36

    - 关于本站 - 网站帮助 - 下载声明 - 友情连接 - 网站地图 -
    Copyright © 2005-2006 myTopWin.Com. All Rights Reserved . [桂ICP备06004496号]