C/C++实现快速排序

 
/**
* @file GM_QSort.h
* @brief 实现快速排序
* @author Don Hao
* @date 2011-8-21 22:31:06
* @version 
* <pre><b>copyright: </b></pre>
* <pre><b>email: </b>[email protected]</pre>
* <pre><b>company: </b>http://blog.csdn.net/donhao</pre>
* <pre><b>All rights reserved.</b></pre>
* <pre><b>modification:</b></pre>
* <pre>Write modifications here.</pre>
*/
#ifndef _GM_QSORT_H
#define _GM_QSORT_H

#ifdef __cplusplus
extern"C"
{
#endif /**< __cplusplus */

    /** 
    * @brief GM_QSort 
    * 
    * Detailed description.
    * @param[in] data 要排列的数组
    * @param[in] size 数组大小
    * @param[in] isDes 1,为降序排列,否则为升序
    */
    void GM_QSort(char* data, int size, int isDes);

#ifdef __cplusplus
}
#endif /**< __cplusplus */

#endif /**< _GM_QSORT_H */

/**
* @file GM_QSort.c
* @brief 
* @author Don Hao
* @date 2011-8-21 22:31:08
* @version 
* <pre><b>copyright: </b></pre>
* <pre><b>email: </b>[email protected]</pre>
* <pre><b>company: </b>http://blog.csdn.net/donhao</pre>
* <pre><b>All rights reserved.</b></pre>
* <pre><b>modification:</b></pre>
* <pre>Write modifications here.</pre>
*/
#include "GM_QSort.h"

#include <stdlib.h>
#include <stdio.h>

void GM_QSort( char* data, int size, int isDes )
{
    int pivot = 0;
    int i     = 0;
    int j     = size - 1;

    if (NULL == data)
    {
        return;
    }

    if (size > 1)
    {
        pivot = data[0];
    }
    else
    {
        return;
    }

    if (1 == isDes)
    {
        while(i < j)
        {
            while((i < j) && (data[j] < pivot))
            {
                --j;
            }

            while((i < j) && (data[i] > pivot))
            {
                ++i;
            }

            if (i < j)
            {
                data[i] ^= data[j];
                data[j] ^= data[i];
                data[i] ^= data[j];
            }
        }
        GM_QSort(data, i + 1, isDes);
        GM_QSort(data + i + 1, size - i - 1, isDes);
    }
    else
    {
        while(i < j)
        {
            while((i < j) && (data[j] > pivot))
            {
                --j;
            }



            while((i < j) && (data[i] < pivot))
            {
                ++i;
            }

            if (i < j)
            {
                data[i] ^= data[j];
                data[j] ^= data[i];
                data[i] ^= data[j];
            }
        }
        GM_QSort(data, i + 1, isDes);
        GM_QSort(data + i + 1, size - i - 1, isDes);
    }
    
}

void main()
{
    char a[10] = {1, 8, 3, 6, 5, 4, 7, 2, 9, 0};
    char b[10] = {1, 8, 3, 6, 5, 4, 7, 2, 9, 0};

    GM_QSort(a, 10, 1);

    GM_QSort(b, 10, 0);

}

你可能感兴趣的:(Date,File,null,PIVOT)