在嵌入式开发中,可以使用c标准库自带的库函数,而不用自己去早轮子,qsort 和bsearch就是其中的两个比较好用的

二分法查找,前提是已经排序好的数据。下面的代码, 如果数据为排序,则要进行排序后,再查找。

/* bsearch example */
#include <stdio.h> /* printf */
#include <stdlib.h> /* qsort, bsearch, NULL */ int compareints (const void *a, const void *b)
{
return ( *(int *)a - * (int *)b );
} int values[] = { 50, 20, 60, 40, 10, 30 }; int main ()
{
int *pItem;
int key = 45;
qsort (values, 6, sizeof (int), compareints);
pItem = (int *) bsearch (&key, values, 6, sizeof (int), compareints);
if (pItem != NULL)
printf ("%d is in the array.\n", *pItem);
else
printf ("%d is not in the array.\n", key);
return 0;
}

快速排序

#include <stdio.h>
#include <stdlib.h> int values[] = { 88, 56, 100, 2, 25 }; int cmpfunc (const void *a, const void *b)
{
return ( *(int *)a - * (int *)b ); //升序
//return ( *(int *)a - * (int *)b ); //降序
} int main()
{
int n; printf("排序之前的列表:\n");
for ( n = 0 ; n < 5; n++ )
{
printf("%d ", values[n]);
} qsort(values, 5, sizeof(int), cmpfunc); printf("\n排序之后的列表:\n");
for ( n = 0 ; n < 5; n++ )
{
printf("%d ", values[n]);
} return (0);
}

比较函数需要特别注意~~~~

Pointer to a function that compares two elements.

This function is called repeatedly by qsort to compare two elements. It shall follow the following prototype:

 
int compar (const void* p1, const void* p2);
 

Taking two pointers as arguments (both converted to const void*). The function defines the order of the elements by returning (in a stable and transitive manner):

return value meaning
<0 The element pointed to by p1 goes before the element pointed to by p2
0 The element pointed to by p1 is equivalent to the element pointed to by p2
>0 The element pointed to by p1 goes after the element pointed to by p2

For types that can be compared using regular relational operators, a general compar function may look like:

1
2
3
4
5
6
int compareMyType (const void * a, const void * b)
{
if ( *(MyType*)a < *(MyType*)b ) return -1;
if ( *(MyType*)a == *(MyType*)b ) return 0;
if ( *(MyType*)a > *(MyType*)b ) return 1;
}

在stm32开发可以调用c标准库的排序和查找 qsort bsearch的更多相关文章

  1. stm32存储器映像和标准库中定义外设地址的方法

    结合存储器映像理解stm32标准库中定义外设地址的方法. stm32f103zet6是32位的.它所能访问的地址空间范围为2^32=4GB,把4GB分为8个block,分别为block0-block- ...

  2. 排序方法之标准库中的快排 qsort ()函数

    C标准库qsort()函数的用法(快排) 使用快速排序例程进行排序 头文件:stdlib.h 用 法: void qsort(void *base, int  nelem, int  width, i ...

  3. android studio 1.0 开发 ndk 调用 c++ so库

    一个没用过java和安卓的人使用android studio开发带c++ so库的安卓程序用例(以ndk的hello-jni为例),对于不熟悉java和安卓的人来说这个很花时间,希望通过这篇文章帮助跟 ...

  4. Linux golang使用cgo调用C++标准库问题

    我们知道cgo无法直接调用c++方法,但是可以通过c包装c++方法,以达到使用的目的. C++中,我们经常会用到STL.在cgo中,如果要调用STL,需要作如下操作: //cgo LDFLAGS: - ...

  5. C++标准库 vector排序

    前天要做一个对C++ STL的vector容器做一个排序操作,之前一直把vector当做一个容量可自动变化的数组,是的,数组,所以打算按照对数组进行排序的方法:用快速排序或是冒泡排序等算法自己写一个排 ...

  6. c运行库、c标准库、windows API的区别和联系

    C运行时库函数C运行时库函数是指C语言本身支持的一些基本函数,通常是汇编直接实现的.  API函数API函数是操作系统为方便用户设计应用程序而提供的实现特定功能的函数,API函数也是C语言的函数实现的 ...

  7. c运行时库,c标准库,Windows系统api的关系

    原文地址:http://blog.csdn.net/seastars_sh/article/details/8233324 C运行库和C标准库的关系 C标准库,顾名思义既然是标准,就是由标准组织制定的 ...

  8. (转)c运行库、c标准库、windows API的区别和联系

    C运行时库函数C运行时库函数是指C语言本身支持的一些基本函数,通常是汇编直接实现的.  API函数API函数是操作系统为方便用户设计应用程序而提供的实现特定功能的函数,API函数也是C语言的函数实现的 ...

  9. C++命名空间、标准库(std,全局命名空间)

    背景 别人遇到的问题: C++ 全局变量不明确与 using namespace std 冲突 我遇到的问题与他相似,函数调用冲突 using namespace std; class compare ...

随机推荐

  1. [翻译向] 当flush privileges生效时

    #前言: 最近频繁在mysql权限控制这里栽跟斗,在翻阅了一些资料之后,简单地翻译一下官网关于flush privileges的描述,抛砖引玉.   #翻译正文: If the mysqld serv ...

  2. Jmeter CSV操作

    统计行号列号 import java.io.BufferedReader; import java.io.FileReader; import java.io.File; print("== ...

  3. 给出一个javascript的Helloworld例子

    1.基础知识:Helloworld:例 1.1<html><head>    <!--  如果你用notepad建立一个txt之后你再改为html,一定在存时,要存成ut ...

  4. AtCoder整理(持续更新中……)

    做了那么久的atcoder觉得自己的题解发的很乱 给有想和我一起交流atcoder题目(或者指出我做法的很菜)(或者指责我为什么整场比赛只会抄题解)的同学一个索引的机会??? 于是写了个爬虫爬了下 A ...

  5. JAVA MAC 比较大小

    2个MAC String mac_1="AAAAAAAAAAAA"; String mac_1="AAAAAAAAAAAB"; 方法一: int a = 0; ...

  6. Essential C++ Reading Notes

    Chapter1 P6, 1.2 Why //#include<string> we still can use "string user_name"? -->c ...

  7. 记录学习Python的第一天

    这是我的第一篇博客,也是我学Python的第一天. 写这篇博客主要目的是为了记下我学习Python的过程以及所学到的知识点.我所学的是Python3版本,我所学的内容有如下几点: 1.python3中 ...

  8. ndarray笔记续

    数组的索引与切片 多维数组的索引 import numpy as np arr=np.arange(1,25).reshape(2,3,4) arr # 输出 array([[[ 1, 2, 3, 4 ...

  9. Spring框架ioc概括

    什么是Spring且能做什么 Spring是一个开源框架,它由Rod Johnson创建.它是为了解决企业应用开发的复杂性而创建的. Spring使用基本的JavaBean来完成以前只可能由EJB完成 ...

  10. k8s系列0--Kubernetes基础知识

    Kubernetes介绍 参考:Kubernetes核心组件解析 Pod是k8s的最小调度单元 每个pod有独立的IP,但是pod的IP是不可靠的,重新调度pod就会改变IP,service概念就是为 ...