C/C++实现的冒泡排序.插入排序.快速排序

1.冒泡排序

/**
* @file GM_BSort.h
* @brief 冒泡排序
* @author 
* @date 
* @version 
*/
#ifndef _GM_BSORT_H
#define _GM_BSORT_H

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

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

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

#endif /**< _GM_BSORT_H */
/**
* @file GM_BSort.c
* @brief 
* @author 
* @date 
* @version 
*/
#include "GM_BSort.h"

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

void GM_BSort( char* data, int size, int isDes )
{
    int i = 0;
    int j = 0;

    if (NULL == data)
    {
        return;
    }

    if (1 == isDes)
    {
        for (i = 0; i < size; ++i)
        {
            for (j = 0; j < size - i - 1; ++j)
            {   
                //采用异或的方法来交换两个数据的内容
                if (data[j] < data [j + 1])
                {
                    data[j] ^= data [j + 1];
                    data[j + 1] ^= data[j];
                    data[j] ^= data [j + 1];
                }
            }
        }
    }
    else
    {
        for (i = 0; i < size; ++i)
        {
            for (j = 0; j < size - i - 1; ++j)
            {
                if (data[j] > data [j + 1])
                {
                    data[j] ^= data [j + 1];
                    data[j + 1] ^= data[j];
                    data[j] ^= data [j + 1];
                }
            }
        }
    }
}

int 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_BSort(a, 10, 1);

    GM_BSort(b, 10, 0);

}
2.插入排序

/**
* @file GM_ISort.h
* @brief 实现插入排序
* @author 
* @date 
* @version 
*/
#ifndef _GM_ISORT_H
#define _GM_ISORT_H

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

    /** 
    * @brief GM_ISort 
    * 
    * Detailed description.
    * @param[in] data 要排序的数据
    * @param[in] size 数据大小
    * @param[in] isDesc 1:降序,否则升序
    */
    void GM_ISort(char* data, int size, int isDesc);
#ifdef __cplusplus
}
#endif /**< __cplusplus */

#endif /**< _GM_ISORT_H */
/**
* @file GM_ISort.c
* @brief 
* @author 
* @date 
* @version 
*/
#include "GM_ISort.h"

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

void GM_ISort( char* data, int size, int isDesc )
{
    int i = 0;
    int j = 0;
    int tmp = 0;

    if (NULL == data)
    {
        return;
    }

    if (1 == isDesc)
    {
        for (i = 1; i < size; ++i)
        {
            j = i;
            
            tmp = data[i];

            while((j > 0) && (tmp > data[j - 1]))
            {
                data[j] = data[j - 1];
                --j;
            }

            data[j] = tmp;
        }
    }
    else
    {
        for (i = 1; i < size; ++i)
        {
            j = i;

            tmp = data[i];

            while((j > 0) && (tmp < data[j - 1]))
            {
                data[j] = data[j - 1];
                --j;
            }

            data[j] = tmp;
        }
    }
}
 

int 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_ISort(a, 10, 1);

    GM_ISort(b, 10, 0);

}
3.快速排序

/**
* @file GM_QSort.h
* @brief 实现快速排序
* @author 
* @date 
* @version 
*/
#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 
* @date 
* @version 
*/
#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);

}

转载于:https://my.oschina.net/kaixindewo/blog/30426

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值