新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     >>计算机科学论坛<<     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版用于讨论编程和软件设计的技巧
    [返回] 计算机科学论坛计算机技术与应用『 编程心得 』 → 刚写的一段快速排序算法,尽可能的面向对象,请大家在质量属性各个方面给出批评意见。 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 7833 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 刚写的一段快速排序算法,尽可能的面向对象,请大家在质量属性各个方面给出批评意见。 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     pennyliang 帅哥哟,离线,有人找我吗?白羊座1979-4-7
      
      
      威望:8
      等级:大二期末(C++考了100分!)
      文章:266
      积分:1911
      门派:Lilybbs.net
      注册:2005/3/11

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给pennyliang发送一个短消息 把pennyliang加入好友 查看pennyliang的个人资料 搜索pennyliang在『 编程心得 』 的所有贴子 引用回复这个贴子 回复这个贴子 查看pennyliang的博客楼主
    发贴心情 刚写的一段快速排序算法,尽可能的面向对象,请大家在质量属性各个方面给出批评意见。

    //主要考虑的因素,可读性,分解均衡性。如果大家对快速排序遗忘,在看了这段代码很
    //容易回忆出来,目的就达到了

    #include <cstdlib>
    #include <iostream>

    using namespace std;
    class QuickSort
    {
     private:
      int* m_array;
      int   m_first;
      int   m_last;
     public :
      QuickSort(int* unSortedArray,int low,int high);
      virtual ~QuickSort();
      bool Process();
     private:
      int Partition(int key,int low,int high);
      int ExtendLargeRegion(int key,int lowVac,int high);       
      int ExtendSmallRegion(int key,int low,int HighVac);
    };

    QuickSort::QuickSort(int* unSortedArray,int low,int high)
    {
     m_array = unSortedArray;
     m_first = low;
     m_last  = high;
    };
    QuickSort::~QuickSort()
    {
    };

    bool QuickSort::Process()
    {
     if(m_array == NULL)
     {
      return false;
     }
     if(m_first<m_last)
     {
      int pivot = m_array[m_first];
      int splitPoint = Partition(pivot,m_first,m_last);
      m_array[splitPoint] = pivot;
      
      QuickSort *quickSortLeft = new QuickSort(m_array,m_first,splitPoint-1);
      quickSortLeft->Process();
      delete quickSortLeft;

      QuickSort *quickSortRight = new QuickSort(m_array,splitPoint+1,m_last);
      quickSortRight->Process();  
      delete quickSortRight;
     }

     return true;
    };

    int QuickSort::Partition(int key,int low,int high)
    {
     int lowVac = low;
     int highVac = -1;

     while(low<high)
     {
      highVac = ExtendLargeRegion(key,lowVac,high);
      lowVac = ExtendSmallRegion(key,low+1,highVac);
      low = lowVac;
      high = highVac - 1;
     }

     return low;
    };

    int QuickSort::ExtendLargeRegion(int key,int lowVac,int high)
    {
     int highVac,curr;
     highVac = lowVac;
      
     for(curr = high;curr>lowVac;curr--)
     {
      if(m_array[curr]<key)
      {
       m_array[lowVac]= m_array[curr];
       highVac = curr;
       break;
      }
     }

     return highVac;
    };

    int QuickSort::ExtendSmallRegion(int key,int low,int highVac)
    {
     int lowVac,curr;
     lowVac = highVac;
     for(curr = low;curr<highVac;curr++)
     {
      if(m_array[curr]>=key)
      {
       m_array[highVac]= m_array[curr];
       lowVac = curr;
       break;
      }
     }

     return lowVac;
    };


    int main(int argc, char *argv[])
    {
        int unSortedArray[] = {31,45,36,27,88,3,4,56,78,44,65,102,-5,0};

        
        QuickSort *Sorter = new QuickSort(&unSortedArray[0],0,13);
        Sorter->Process();
        delete Sorter;
        
        cout<<endl;
        for(int i=0;i<14;i++)
        {
         cout<<unSortedArray[i]<<endl;
        }
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }



    [此贴子已经被作者于2006-1-16 10:44:26编辑过]

       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/1/16 0:58:00
     
     pennyliang 帅哥哟,离线,有人找我吗?白羊座1979-4-7
      
      
      威望:8
      等级:大二期末(C++考了100分!)
      文章:266
      积分:1911
      门派:Lilybbs.net
      注册:2005/3/11

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给pennyliang发送一个短消息 把pennyliang加入好友 查看pennyliang的个人资料 搜索pennyliang在『 编程心得 』 的所有贴子 引用回复这个贴子 回复这个贴子 查看pennyliang的博客2
    发贴心情 
    这居然是本学期写的第二次代码。
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/1/16 1:00:00
     
     pennyliang 帅哥哟,离线,有人找我吗?白羊座1979-4-7
      
      
      威望:8
      等级:大二期末(C++考了100分!)
      文章:266
      积分:1911
      门派:Lilybbs.net
      注册:2005/3/11

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给pennyliang发送一个短消息 把pennyliang加入好友 查看pennyliang的个人资料 搜索pennyliang在『 编程心得 』 的所有贴子 引用回复这个贴子 回复这个贴子 查看pennyliang的博客3
    发贴心情 
    //根据好友Loki的意见修改的代码

    #include <cstdlib>
    #include <iostream>

    using namespace std;
    class QuickSort
    {
     private:
      int* m_array;
      int   m_first;
      int   m_last;
     public :
      QuickSort(int* unSortedArray,int low,int high);
      virtual ~QuickSort();
      bool Process();
     private:
      int Partition(int key,int low,int high);
      int ExtendLargeRegion(int key,int lowVac,int high);       
      int ExtendSmallRegion(int key,int low,int HighVac);
      bool QuickSortLeftRegion(int low,int high);
      bool QuickSortRightRegion(int low,int high);
      bool QuickSortRegion(int low,int high);
    };

    QuickSort::QuickSort(int* unSortedArray,int low,int high)
    {
     m_array = unSortedArray;
     m_first = low;
     m_last  = high;
    };
    QuickSort::~QuickSort()
    {
    };
    bool QuickSort::Process()
    {
     if(m_array == NULL)
     {
      return false;
     }
     if(m_first<m_last)
     {
      int pivot = m_array[m_first];
      int splitPoint = Partition(pivot,m_first,m_last);
      m_array[splitPoint] = pivot;
      QuickSortLeftRegion(m_first,splitPoint-1);
      QuickSortRightRegion(splitPoint+1,m_last);
     }
     return true;
    };

    int QuickSort::Partition(int key,int low,int high)
    {
     int lowVac = low;
     int highVac = -1;

     while(low<high)
     {
      highVac = ExtendLargeRegion(key,lowVac,high);
      lowVac = ExtendSmallRegion(key,low+1,highVac);
      low = lowVac;
      high = highVac - 1;
     }

     return low;
    };

    int QuickSort::ExtendLargeRegion(int key,int lowVac,int high)
    {
     int highVac,curr;
     highVac = lowVac;
      
     for(curr = high;curr>lowVac;curr--)
     {
      if(m_array[curr]<key)
      {
       m_array[lowVac]= m_array[curr];
       highVac = curr;
       break;
      }
     }

     return highVac;
    };

    int QuickSort::ExtendSmallRegion(int key,int low,int highVac)
    {
     int lowVac,curr;
     lowVac = highVac;
     for(curr = low;curr<highVac;curr++)
     {
      if(m_array[curr]>=key)
      {
       m_array[highVac]= m_array[curr];
       lowVac = curr;
       break;
      }
     }

     return lowVac;
    };

    bool QuickSort::QuickSortRegion(int low,int high)
    {
         QuickSort* sorter = new QuickSort(m_array,low,high);
         sorter->Process();
         delete sorter;
         return true;
    };

    bool QuickSort::QuickSortLeftRegion(int low,int high)
    {
         return QuickSortRegion(low,high);
    };

    bool QuickSort::QuickSortRightRegion(int low,int high)
    {
         return QuickSortRegion(low,high);
    };


    int main(int argc, char *argv[])
    {
        int unSortedArray[] = {31,45,36,27,88,3,4,56,78,44,65,102,-5,0};

        
        QuickSort *Sorter = new QuickSort(&unSortedArray[0],0,13);
        Sorter->Process();
        delete Sorter;
        
        cout<<endl;
        for(int i=0;i<14;i++)
        {
         cout<<unSortedArray[i]<<endl;
        }
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }

    [此贴子已经被作者于2006-1-16 13:16:42编辑过]
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/1/16 12:45:00
     
     pennyliang 帅哥哟,离线,有人找我吗?白羊座1979-4-7
      
      
      威望:8
      等级:大二期末(C++考了100分!)
      文章:266
      积分:1911
      门派:Lilybbs.net
      注册:2005/3/11

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给pennyliang发送一个短消息 把pennyliang加入好友 查看pennyliang的个人资料 搜索pennyliang在『 编程心得 』 的所有贴子 引用回复这个贴子 回复这个贴子 查看pennyliang的博客4
    发贴心情 
    //根据chen3feng的意见修改第三个版本
    #include <cstdlib>
    #include <iostream>

    using namespace std;

    class QuickSort
    {

    private:
        int* m_array;
        int m_first;
        int m_last;

    public :
        QuickSort(int* unSortedArray, int low, int high);
        virtual ~QuickSort();
        bool Process();

    private:
        int Partition(int key, int low, int high);
        int ExtendLargeRegion(int key, int lowVac, int high);
        int ExtendSmallRegion(int key, int low, int HighVac);
        bool QuickSortLeftRegion(int low, int high);
        bool QuickSortRightRegion(int low, int high);
        bool QuickSortRegion(int low, int high);
    };

    QuickSort::QuickSort(int* unSortedArray, int low, int high)
    {
        m_array = unSortedArray;
        m_first = low;
        m_last = high;
    }

    QuickSort::~QuickSort()
    {
    }

    bool QuickSort::Process()
    {
        if (m_array == NULL)
        {
            return false;
        }

        if (m_first < m_last)
        {
            int pivot = m_array[m_first];
            int splitPoint = Partition(pivot, m_first, m_last);
            m_array[splitPoint] = pivot;
            QuickSortLeftRegion(m_first, splitPoint - 1);
            QuickSortRightRegion(splitPoint + 1, m_last);
        }

        return true;
    }

    int QuickSort::Partition(int key, int low, int high)
    {
        int lowVac = low;
        int highVac = -1;

        while (low < high)
        {
            highVac = ExtendLargeRegion(key, lowVac, high);
            lowVac = ExtendSmallRegion(key, low + 1, highVac);
            low = lowVac;
            high = highVac - 1;
        }

        return low;
    }

    int QuickSort::ExtendLargeRegion(int key, int lowVac, int high)
    {
        int highVac, curr;
        highVac = lowVac;

        for (curr = high;curr > lowVac;curr--)
        {
            if (m_array[curr] < key)
            {
                m_array[lowVac] = m_array[curr];
                highVac = curr;
                break;
            }
        }

        return highVac;
    }

    int QuickSort::ExtendSmallRegion(int key, int low, int highVac)
    {
        int lowVac, curr;
        lowVac = highVac;

        for (curr = low;curr < highVac;curr++)
        {
            if (m_array[curr] >= key)
            {
                m_array[highVac] = m_array[curr];
                lowVac = curr;
                break;
            }
        }

        return lowVac;
    }

    bool QuickSort::QuickSortRegion(int low, int high)
    {
        QuickSort sorter(m_array, low, high);
        sorter.Process();
        
        return true;
    }

    bool QuickSort::QuickSortLeftRegion(int low, int high)
    {
        return QuickSortRegion(low, high);
    }

    bool QuickSort::QuickSortRightRegion(int low, int high)
    {
        return QuickSortRegion(low, high);
    }

    int main(int argc, char *argv[])
    {
        int unSortedArray[] = {31, 45, 36, 27, 88, 3, 4, 56, 78, 44, 65, 102, -5, 0};
        QuickSort Sorter(&unSortedArray[0], 0, 13);
        
        Sorter.Process();
        for (int i = 0;i < 14;i++)
        {
            cout << unSortedArray[i] << endl;
        }
        system("PAUSE");
        
        return EXIT_SUCCESS;
    }

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/1/16 18:19:00
     
     pennyliang 帅哥哟,离线,有人找我吗?白羊座1979-4-7
      
      
      威望:8
      等级:大二期末(C++考了100分!)
      文章:266
      积分:1911
      门派:Lilybbs.net
      注册:2005/3/11

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给pennyliang发送一个短消息 把pennyliang加入好友 查看pennyliang的个人资料 搜索pennyliang在『 编程心得 』 的所有贴子 引用回复这个贴子 回复这个贴子 查看pennyliang的博客5
    发贴心情 
    //这个是我两年前写得代码

    #include "stdafx.h"

    int list[] =
    {
     12, 4, 6, 65, 7, 18, 23, 2, 45, 66, 8, 10, 16, 44, 22, 24,
     17, 23, 34, 45, 67, 23, 12, 54, 22, 16, 47, 89, 36, 66
    };

    void exchange(int&a, int&b)
    {
        int temp;
        temp = a;
        a = b;
        b = temp;
    };

    int partition(int list[], int l, int h, int pivotal)
    {
        int temp, pivotval;

        exchange(list[pivotal], list[l]);

        int pivotpos = l;

        pivotval = list[pivotpos];

        for (int i = l + 1;i <= h;i++)
        {

            if (list[i] < pivotval && ++pivotpos != i)
            {
                exchange(list[i], list[pivotpos]);
            }
        }

        exchange(list[l], list[pivotpos]);
        return pivotpos;

    };

    int quicksort(int list[], int l, int h)
    {
        int pivotal;

        if (h > l)
        {

            pivotal = (l + h) / 2;

            int pivotpos = partition(list, l, h, pivotal);

            quicksort(list, l, pivotpos - 1);
            quicksort(list, pivotpos + 1, h);
        }
        else
        {}

        return 0;
    };

    int main(int argc, char* argv[])
    {
        quicksort(list, 0, sizeof(list) / sizeof(int) - 1);
        return 0;
    }

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/1/16 18:25:00
     
     reallyh 帅哥哟,离线,有人找我吗?
      
      
      威望:4
      等级:大四(GRE考了1600分!)(版主)
      文章:114
      积分:1041
      门派:Lilybbs.net
      注册:2005/12/6

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给reallyh发送一个短消息 把reallyh加入好友 查看reallyh的个人资料 搜索reallyh在『 编程心得 』 的所有贴子 引用回复这个贴子 回复这个贴子 查看reallyh的博客6
    发贴心情 
    还真给忘了呵呵
    3ks
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/2/12 18:45:00
     
     pennyliang 帅哥哟,离线,有人找我吗?白羊座1979-4-7
      
      
      威望:8
      等级:大二期末(C++考了100分!)
      文章:266
      积分:1911
      门派:Lilybbs.net
      注册:2005/3/11

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给pennyliang发送一个短消息 把pennyliang加入好友 查看pennyliang的个人资料 搜索pennyliang在『 编程心得 』 的所有贴子 引用回复这个贴子 回复这个贴子 查看pennyliang的博客7
    发贴心情 
    using System;
    using System.Collections;
    using NUnit.Framework;

    namespace Algorithm.Sort
    {
     class QuickSort:ISort
     {
     #region Member List
     ArrayList m_array;
        int m_first;
        int m_last;
     #endregion

     #region Ctor
     public QuickSort(ArrayList unSortedArray, int low, int high)
     {
      #region precondition
      if(null == unSortedArray)
      {
       throw new Exception(string.Format("ArrayList cannot be sorted,{0},{1}",low,high));
      }
      #endregion
      
      m_array = unSortedArray;
      m_first = low;
      m_last = high;
     }
     #endregion

     #region ISort 成员

     public bool Sort()
     {
      if (m_first < m_last)
      {
       IComparable pivot = (IComparable)m_array[m_first];
       int splitPoint = Partition(pivot, m_first, m_last);
       m_array[splitPoint] = pivot;
       QuickSortLeftRegion(m_first, splitPoint - 1);
       QuickSortRightRegion(splitPoint + 1, m_last);
      }
      return true;
     }

     #endregion

     #region Private Fun
     private int Partition(IComparable key, int low, int high)
     {
      int lowVac = low;
      int highVac = -1;

      while (low < high)
      {
       highVac = ExtendLargeRegion(key, lowVac, high);
       lowVac = ExtendSmallRegion(key, low + 1, highVac);
       low = lowVac;
       high = highVac - 1;
      }

      return low;
     }
     private int ExtendLargeRegion(IComparable key, int lowVac, int high)
     {
      int highVac, curr;
      highVac = lowVac;

      for (curr = high;curr > lowVac;curr--)
      {
       if (key.CompareTo(m_array[curr])>0)
       {
        m_array[lowVac] = m_array[curr];
        highVac = curr;
        break;
       }
      }

      return highVac;
     }
     private int ExtendSmallRegion(IComparable key, int low, int highVac)
     {
      int lowVac, curr;
      lowVac = highVac;

      for (curr = low;curr < highVac;curr++)
      {
       int comparedValue = key.CompareTo(m_array[curr]);
       if (comparedValue<0||comparedValue==0)
       {
        m_array[highVac] = m_array[curr];
        lowVac = curr;
        break;
       }
      }
      return lowVac;
     }
     private bool QuickSortLeftRegion(int low, int high)
     {
       return QuickSortRegion(low, high);
     }
     private bool QuickSortRightRegion(int low, int high)
     {
       return QuickSortRegion(low, high);
     }
     private bool QuickSortRegion(int low, int high)
     {
      QuickSort m_instance = new QuickSort(m_array,low,high);
      return m_instance.Sort();
     }
      #endregion 
     }
     public class TestObjcet:IComparable
     {
      int m_value;
      public int Value
      {
       get{return m_value;}
      }
      public TestObjcet(int value)
      {
       m_value = value;
      }
      public static implicit operator int(TestObjcet m)
      {
       return m.Value;
      }

      #region IComparable 成员

      public int CompareTo(object obj)
      {
       
       TestObjcet testObj = obj as TestObjcet;
       if(null!=testObj)
       {
        if(testObj.Value>this.Value)
        {
         return -1;
        }
        else if(testObj.Value == this.Value)
        {
         return 0;
        }
        else
        {
         return 1;
        }
       }
       else
       {
        throw new Exception(string.Format("object type is not testCase"));
       }
      }

      #endregion
     }
     [TestFixture]
     public class TestQuickSort
     {
      
      [Test]
      public void test()
      {
       ArrayList people1 = new ArrayList();
       people1.Add(new TestObjcet(1));
       people1.Add(new TestObjcet(2));
       people1.Add(new TestObjcet(4));
       people1.Add(new TestObjcet(3));

       ArrayList people2 = new ArrayList();
       people2.Add('a');
       people2.Add('d');
       people2.Add('c');
       people2.Add('b');
       
       QuickSort sorter1 = new QuickSort(people1,0,people1.Count-1);
       QuickSort sorter2 = new QuickSort(people2,0,people2.Count-1);
       Queue queue = new Queue(20);
       queue.Enqueue(sorter1);
       queue.Enqueue(sorter2);
       testSorter(queue);

       foreach (TestObjcet p in people1)
        Console.WriteLine(p);
       foreach (char p in people2)
        Console.WriteLine(p);
             
       Console.ReadLine();
      }
      private void testSorter(Queue queue)
      {
       ISort sorter = (queue.Dequeue())as ISort;
       if(null!=sorter)
       {
        sorter.Sort();
       }
      }
      public static void Main()
      {
       (new TestQuickSort()).test();
      }
     }
    }

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/3/13 15:46:00
     
     GoogleAdSense白羊座1979-4-7
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 编程心得 』 的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/4/30 13:54:03

    本主题贴数7,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    156.250ms