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

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

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 7855 个阅读者浏览上一篇主题  刷新本主题   平板显示贴子 浏览下一篇主题
     * 贴子主题: 刚写的一段快速排序算法,尽可能的面向对象,请大家在质量属性各个方面给出批评意见。 举报  打印  推荐  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
     
     GoogleAdSense白羊座1979-4-7
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 编程心得 』 的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/21 6:22:10

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

     *树形目录 (最近20个回帖) 顶端 
    主题:  刚写的一段快速排序算法,尽可能的面向对象,请大家在质量属性各个方面给出批评意见。..(2609字) - pennyliang,2006年1月16日
        回复:  using System;using System.Collections;using NUn..(4077字) - pennyliang,2006年3月13日
        回复:  还真给忘了呵呵3ks(19字) - reallyh,2006年2月12日
        回复:  //这个是我两年前写得代码#include "stdafx.h"int list[] =..(1144字) - pennyliang,2006年1月16日
        回复:  //根据chen3feng的意见修改第三个版本#include <cstdlib>#inclu..(2963字) - pennyliang,2006年1月16日
        回复:  //根据好友Loki的意见修改的代码#include <cstdlib>#include <..(2823字) - pennyliang,2006年1月16日
        回复:  这居然是本学期写的第二次代码。(30字) - pennyliang,2006年1月16日

    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    66.406ms