zoukankan      html  css  js  c++  java
  • 数据结构快速排序

    // Header.h
    //定义函数指针别名
    typedef int ( *PFI2S ) ( const string &, const string & );
    
    //函数声明
    int lexicoCompare( cnost string &, const string & );
    int sort( string*, string*, PFI2S = lexicoCompare);
    
    //Main.C
    #include <iostream>
    #include <string>
    
    //函数定义
    void sort ( string *s1, sring *s2, PFI2S compare = lexicoCompare )
    {
    	//递归的停止条件
    	if (s1 < s2 ) {
    		string elem = *s1;
    		string *low = *s1;
    		string *high = s2 -1; 
    
    		for (;;)
    		{
    			while (compare ( *++low, elem ) < 0 && low < s2 ) ;
    			while (compare ( elem, *--high) < 0 && high > s1 );
    			
    			if ( low < high)
    			{
    				low->swap(*high);
    			} 
    			else
    			{
    				break;
    			}
    		} //end, for (;;)
    
    		//put elem into correct location
    		s1->swap(*high);
    		sort( s1, high - 1, compare );
    		sort( low + 1, s2, compare );
    
    	} ///end, if (s1 < s2)
    }
    
    string as[] = { "a", "light", "drizzle", "was" "failing",
    				"when", "they", "left", "the", "museum" };
    int main() {
    	//use default parameter
    	sort ( as, as + sizeof(as)/sizeof(as[0])) - 1 };
    
    	for (int i = 0; i < sizeof(as)/sizeof(as[0]); ++i )
    		cout << as [ i ].c_str() << "\n\t";
    }
    
  • 相关阅读:
    页面渲染1——创建对象模型
    HTTP 缓存
    web安全字体
    图片优化
    基于文本内容的压缩
    Mac homebrew的熟悉和常用指令
    二、Java注释
    一、Java环境变量配置
    JS中的逻辑运算符&&、||
    js 中的 深拷贝与浅拷贝
  • 原文地址:https://www.cnblogs.com/dracohan/p/2985789.html
Copyright © 2011-2022 走看看