⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 quicksort.h

📁 这个是symbian下的一个蛮庞大的3D游戏源代码!对于学习3D开发的人有很大的帮助!
💻 H
字号:
#ifndef _LANG_QUICKSORT_H
#define _LANG_QUICKSORT_H


#ifdef __SYMBIAN32__
	#define LANG_SORT lang::quicksort
#else
	#include <algorithm>
	#define LANG_SORT std::sort
#endif


namespace lang
{


/**
 * Traditional quick sort.
 * Slower than HP/SGI introsort, but generates
 * only a fraction of code so useful on mobile platforms.
 */
template <class T> void quicksort( T* begin, T* end )
{
	int count = end - begin;
	if ( count < 1 )
		return;

    int i = 0;
	int j = count - 1;
    T x = begin[count>>1];

    do
    {    
        while ( begin[i] < x )
			i++; 
        while ( x < begin[j] )
			j--;

        if ( i <= j )
        {
            T h = begin[i];
			begin[i] = begin[j];
			begin[j] = h;

            i++;
			j--;
        }
    } while ( i <= j );

    if ( 0 < j )
		quicksort( begin, begin+j+1 );
    if ( i < count-1 )
		quicksort( begin+i, end );
}

/**
 * Traditional quick sort using predicate.
 * Slower than HP/SGI introsort, but generates
 * only a fraction of code so useful on mobile platforms.
 */
template <class T, class C> void quicksort( T* begin, T* end, C comp )
{
	int count = end - begin;
	if ( count < 1 )
		return;

	int i = 0;
	int j = count - 1;
    T x = begin[count>>1];

    do
    {    
        while ( comp(begin[i],x) )
			i++; 
        while ( comp(x,begin[j]) )
			j--;

        if ( i <= j )
        {
            T h = begin[i];
			begin[i] = begin[j];
			begin[j] = h;

            i++;
			j--;
        }
    } while ( i <= j );

    if ( 0 < j )
		quicksort( begin, begin+j+1, comp );
    if ( i < count-1 )
		quicksort( begin+i, end, comp );
}


} // lang


#endif // _LANG_QUICKSORT_H

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -