STM32 qsort快速排序

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

#define   BUF_SIZE      10
#define   RAND_NUM_MAX  100

unsigned char buf[BUF_SIZE] = {0};

//===串口打印=================================
int fputc(int ch, FILE * stream)
{
    HAL_UART_Transmit(&huart1, (unsigned char *)&ch, 1, HAL_MAX_DELAY);
    return ch;
}

//===回调比较函数,注意数组类型和回调匹配===========
int compare_char(const void *a, const void *b)
{
    return (*(char *)a) - (*(char *)b);
}

int compare_int(const void *a, const void *b)
{
    return (*(int *)a) - (*(int *)b);
}

//===产生随机数排序=============================
void rand_num(void)
{
    unsigned int i;
    
    for(i = 0; i < BUF_SIZE; i++)
    {
        srand(HAL_GetTick());    //设置随机数种子
        HAL_Delay(1);            //等待改变种子
        buf[i] = rand()%(RAND_NUM_MAX + 1);
        printf("%d ", buf[i]);
    }

    //---排序----------------------------
    qsort(buf, BUF_SIZE, sizeof(buf[0]), compare_char);
    
    printf("\r\n");
    for(i = 0; i < BUF_SIZE; i++)
        printf("%d ", buf[i]);
}

 

全篇完。

本人博客仅仅代表我个人见解方便记录成长笔记。

若有与 看官老爷见解有冲突,我坚信看官老爷见解是对的,我的是错的。

感谢~!

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值