各种排序算法C++实现(冒泡,选择,插入,快速,归并,堆)

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

实现文件:

 

/*****************************************************************************
 *                                    sort.h
 *
 * Some sort algorithms.
 *
 * This file includes several usually used sorting algorithm, such as: bubble
 * sorting, selection sorting, insertion sorting, quick sorting, merging
 * sorting, and heap sorting.
 *
 * Zhang Ming, 2010-07
 *****************************************************************************/


#ifndef SORT_H
#define SORT_H


#include 


using namespace std;


namespace itlab
{

    /**
     * Bubble sort algorithm.
     * "a"      ---->   array of Comparable items.
     * "left"   ---->   the left-most index of the subarray.
     * "right"  ---->   the right-most index of the subarray.
     */
     template 
     void bubbleSort( vector &a, int left, int right )
     {
         bool cond;
         for( int i=left; ii; --j )
                 if( a[j] < a[j-1] )
                 {
                     swap( a[j], a[j-1] );
                     cond = true;
                 }

            if( !cond  )
                return;
         }
     }


    /**
     * Selection sort algorithm.
     * "a"      ---->   array of Comparable items.
     * "left"   ---->   the left-most index of the subarray.
     * "right"  ---->   the right-most index of the subarray.
     */
     template 
     void selectSort( vector &a, int left, int right )
     {
         Type minPos;
         for( int i=left; i   array of Comparable items.
     * "left"   ---->   the left-most index of the subarray.
     * "right"  ---->   the right-most index of the subarray.
     */
    template 
    void insertSort( vector &a, int left, int right )
    {
        for( int p=left+1; p<=right; p++ )
        {
            Type tmp = a[p];
            int j;

            for( j=p; j>left && tmp
    const Type& median3( vector &a, int left, int right )
    {
        int center = (left+right) / 2;

        if( a[center] < a[left] )
            swap( a[left], a[center] );

        if( a[right] < a[left] )
            swap( a[left], a[right] );

        if( a[right] < a[center] )
            swap( a[center], a[right] );

        swap( a[center], a[right-1] );

        return a[right-1];
    }


    /**
     * Internal quicksort method that makes recursive calls.
     * Uses median-of-three partitioning and a cutoff of 20.
     * "a"      ---->   array of Comparable items.
     * "left"   ---->   the left-most index of the subarray.
     * "right"  ---->   the right-most index of the subarray.
     */
    template 
    void quickSort( vector &a, int left, int right )
    {
        if( left+20 <= right )
        {
            Type pivot = median3( a, left, right );

            // begin partitioning
            int i = left, j = right-1;
            for( ; ; )
            {
                while( a[++i] < pivot ) { }
                while( pivot < a[--j] ) { }

                if( i < j )
                    swap( a[i], a[j] );
                else
                    break;
            }

            // Restore pivot
            swap( a[i], a[right-1] );

            // Sort small elements
            quickSort( a, left, i-1 );

            // Sort large elements
            quickSort( a, i+1, right );
        }
        else
            insertSort( a, left, right );
    }

    /**
     * Merg two subsequence to a bigger one.
     * The first subsequence is a[left1] ... a[right1], and
     * The second subsqeuence is a[left2] ... a[right2].
     */
    template 
    void merg( vector &a, int left1, int right1, int left2, int right2 )
    {
        int k = 0,
            i = left1,
            j = left2,
            n1 = right1-left1+1,
            n2 = right2-left2+1;

        Type *tmp = new Type[n1+n2];

        while( i <= right1 && j <= right2 )
            if( a[i] < a[j] )
                tmp[k++] = a[i++];
            else
                tmp[k++] = a[j++];

        while( i <= right1 )
            tmp[k++] = a[i++];
        while( j <= right2 )
            tmp[k++] = a[j++];

        for( int i=0; i   array of Comparable items.
     * "left"   ---->   the left-most index of the subarray.
     * "right"  ---->   the right-most index of the subarray.
     */
    template 
    void mergSort( vector &a, int left, int right )
    {
        int left1, right1, left2, right2,
            n = right - left + 1,
            size = 1;

        while( size < n )
        {
            left1 = left;

            while( left1+size < n )
            {
                left2 = left1+size;
                right1 = left2-1;
                if( left2+size > n )
                    right2 = right;
                else
                    right2 = left2+size-1;

                merg( a, left1, right1, left2, right2 );

                left1 = right2+1;
            }

            size *= 2;
        }
    }


    /**
     * Heap sort algorthm.
     * "a"      ---->   array of Comparable items.
     * "left"   ---->   the left-most index of the subarray.
     * "right"  ---->   the right-most index of the subarray.
     */
    template 
    void heapSort( vector &a, int left, int right )
    {
        int n = right-left+1;
        vector tmp( n );
        for( int i=0; i=0; --i )
            filterDown( tmp, i, n );
        for( int j=n-1; j>0; --j )
        {
            swap( tmp[0], tmp[j] );
            filterDown( tmp, 0, j );
        }

        for( int i=0; i  the position from which to percolate down.
     * "n"  ---->  the logical size of the binary heap.
     */
    template 
    void filterDown( vector &a, int i, int n )
    {
        int child;
        Type tmp;

        for( tmp=a[i]; 2*i+1

 

测试文件:

/*****************************************************************************
 *                                sort_test.cpp
 *
 * Sorting algorithm testing.
 *
 * Zhang Ming, 2009-10
 *****************************************************************************/


#include 
#include 
#include 


using namespace std;
using namespace itlab;


const int SIZE = 10;


template 
void printVector( const vector &a )
{
    vector::const_iterator itr = a.begin();
    while( itr != a.end() )
        cout << *itr++ << "\t";

    cout << endl;
}


int main()
{
    vector a( SIZE );

    cout << "Unsorted Numbers : " << endl;
    Random r(127);
    for( unsigned i=0; i

转载于:https://my.oschina.net/zmjerry/blog/6084

你可能感兴趣的:(各种排序算法C++实现(冒泡,选择,插入,快速,归并,堆))