常用算法模板

开始存模板

快速排序

#include <cstdio>
#include <algorithm>
#include <iostream>

using namespace std ; 
typedef long long ll ; 
const int maxN = 100010 ; 

int a[ maxN ] ; 

inline ll INPUT ( ) {
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(x=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
    return x*f;
} 

void quick_sort ( const int l , const int r ) {
    if ( l > r ) {
        return ; ;
    }
    int i = l , j = r ; 
    while ( i < j ) {
        while ( a[ j ] >= a[ l ] && j > i ) {
            --j ; 
        }
        while ( a[ i ] <= a[ l ] && j > i ) {
            ++i ; 
        }
        if ( j > i )
            swap ( a[ i ] , a[ j ] ) ; 
    }
    swap ( a[ l ] , a[ i ] ) ;
    quick_sort ( l , i - 1 ) ;
    quick_sort ( i + 1 , r ) ; 
}

int main ( ) {
    int N = INPUT( ) ;
    for ( int i=1 ; i<=N ; ++i ) {
        a[ i ] = INPUT( ) ; 
    }
    quick_sort ( 1 , N ) ; 
    for ( int i=1 ; i<=N ; ++i ) {
        cout << a[ i ] << endl ; 
    }
    return 0 ; 
}
View Code

猜你喜欢

转载自www.cnblogs.com/shadowland/p/9859099.html