Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use qsort function in C language

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use the qsort function in the C language, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

I. what is the qsort function

We can use the search base function URL or MSDN software to find it.

Qsort () function: quick sort function-reference stdlib. H header file.

Parameter description:

Void qsort (void* base, / / the target array size_t num to be sorted, / / the number of elements to be sorted size_t width, / / the size of an element in bytes int (* cmp) (const void* E1, const void* e2))

Where cmp is the function pointer and cmp points to the function used to compare two elements when sorting. You need to write it yourself.

Return value:

two。 Sort using qsort-take ascending order as an example

About void* pointers:

Void*: has no specific type of pointer that can receive any type of address

Disadvantages: unable to perform operations. Cannot be +-integer, cannot be dereferenced

Int a = 0There float f = 5.5f * p1 = & a TX Void * p2 = & f TX p1 = p1float 1; / / err1. Sorting of integer arrays

Note:

1. The parameter type of the comparison function is void*, so we need to cast it! And you have to dereference in order to get the corresponding value!

two。 If we want to put it in descending order, we just need to write it as e2-e1.

Void Print (int* arr, int sz) {int I = 0; for (I = 0; I

< sz; i++) { printf("%d ", *(arr + i)); } printf("\n");}//比较整形//注意类型时void* 所以要强制类型转化,还要解引用才是对应的值!!!int cmp_int(const void* e1, const void* e2){ return *(int*)e1 - *(int*)e2;}void test1(){ int arr[] = { 9,8,7,6,7,5,4,8 }; int sz = sizeof(arr) / sizeof(arr[0]); qsort(arr, sz, sizeof(arr[0]), cmp_int); Print(arr, sz);}2.字符数组排序 注意使用sizeof()操作符和strlen()函数的区别 //注意要要强制类型转换!! 要解引用!!! 本质上是比较Ascii值int cmp_char(const void* e1, const void* e2){ return *(char*)e1 - *(char*)e2;}void test4(){ char arr[] ="mango"; //若使用sizeof计算长度: //int sz = sizeof(arr) / sizeof(arr[0]); //6 //qsort(arr, sz-1, sizeof(arr[0]), cmp_float); //因为sizeof把\0也算进去了,所以计算出来的值比字符串本身长度多1 int sz = strlen(arr); //5 qsort(arr, sz, sizeof(arr[0]), cmp_char); printf("%s\n",arr);}3.字符指针数组排序 先看看下面这段程序有没有问题? int cmp_chars(const void* e1, const void* e2){ return strcmp((char*)e1, *(char*)e2);}void test2(){ char* arr1 = "abc"; char* arr2 = "wcad"; char* arr3 = "cab"; char* p[3] = { arr1,arr2,arr3 }; int sz = sizeof(p) / sizeof(p[0]); qsort(p, sz, sizeof(p[0]), cmp_chars); int i = 0; for (i = 0; i < sz; i++) { printf("%s\n", p[i]); }} 打印出来发现:结果是错误的! ->

After debugging, it is found that e2 stores the address of p (char** type), and E1 stores the address of the next element that p points to (char** type).

For this writing, the address of p is passed in, and strcmp () converts the content corresponding to the address of p into a string, that is, the address of arr1,arr2,arr3 in p into a string.

In fact, the address of arr1,arr2 in the p address space should be passed so that strcmp () can find the string corresponding to arr1 and arr2, so it is necessary to convert E1 arr2 e2 into char**, so that it is a char* address after dereferencing.

Reason: passing p to qsort,p is the array name-> the address of the first element, and the element type is char* >, so the type of p is: char**. So E1 and e2 also have to force the type to be converted to char**, dereferencing E1 ref e2 is the address of the corresponding string!

Positive solution:

Int cmp_chars (const void* E1, const void* e2) {return strcmp (* (char**) E1, * (char**) e2);} void test2 () {char* arr1 = "abc"; char* arr2 = "wcad"; char* arr3 = "cab"; char* p [3] = {arr1,arr2,arr3}; int sz = sizeof (p) / sizeof (p [0]) Qsort (p, sz, sizeof (p [0]), cmp_chars); int I = 0; for (I = 0; I)

< sz; i++) { printf("%s\n", p[i]); }4.结构体数组排序 比较年龄->

The actual comparison is plastic surgery.

Compare names-> actually compare strings-> use the strcmp function, not use = = to judge

Struct Stu {int age; char name [20];}; / / compare the ages of elements in the structure int cmp_age (const void* e1, const void* e2) {/ / the essence is to compare plastic return ((struct Stu*) E1)-> age-((struct Stu*) e2)-> age } / / the comparison name int cmp_name (const void* E1, const void* e2) {/ / is essentially a string comparison-> using the strcmp function return strcmp (struct Stu*) E1)-> name, ((struct Stu*) e2)-> name) } void test2 () {/ / create an array of structures, initialize struct Stu s [3] = {{19, "Mango"}, {18, "Lemon"}, {20, "Hello"}} with curly braces; int sz = sizeof (s) / sizeof (s [0]); / / arrange qsort (s, sz, sizeof (s [0]), cmp_age) by age Printf ("% s% d", s [0] .name, s [0] .age); printf ("% s% d", s [1] .name, s [1] .age); printf ("% s% d", s [2] .name, s [2] .age); printf ("\ n"); / / qsort (s, sz, sizeof (s [0]), cmp_name) by name Printf ("% s% d", s [0] .name, s [0] .age); printf ("% s% d", s [1] .name, s [1] .age); printf ("% s% d", s [2] .name, s [2] .age); printf ("\ n");} 5. Floating point array sorting

Note: in the comparison function, the return type is int, and the subtracted value will force the type to be converted to int, but this will also cause errors. It is recommended to use method 2.

/ / Writing method 1: possible error / / reason: 0.20.1 = 0.1After forced type conversion to int, the result is 0//int cmp_float (const void* e1, const void* e2) / / {/ the return type is int, so the subtracted result is forced type conversion / / return (int) (* (float*) E1-* (float*) e2) / /} / / Writing method 2: the return value int cmp_float (const void* E1, const void* e2) {if ((* (float*) E1-* (float*) e2) > 0.00000) return 1; else if ((* (float*) E1-* (float*) e2) = 0.000000) return 0 corresponding to return; else return-1 } void test3 () {float arr [5] = {5.01f [5] = {5.01f (arr) / sizeof (arr [0]); qsort (arr, sz, sizeof (arr [0]), cmp_float); int I = 0; for (I = 0; I)

< sz; i++) { printf("%f ", arr[i]); }}三.使用冒泡排序思想模拟实现qsort函数1.什么是冒泡排序: 主要思想:相邻的两个元素进行比较 对于冒泡排序: n个元素 共进行n-1趟冒泡排序。一趟可以使一个元素在特定位置上,每趟排序可以少比较一个元素 但是冒泡排序只能排序整形 2.冒泡排序代码void BubbleSort(int* arr, int sz){ int i = 0; int j = 0; int flag = 1;//假设一开始有序 //共进行sz-1趟 for (i = 0; i < sz-1; i++) { // 每一趟 for (j = 0; j < sz - 1 - i; j++) { if (arr[j] >

Arr [j + 1]) {int tmp = arr [j]; arr [j] = arr [j + 1]; arr [j + 1] = tmp; flag = 0 }} if (flag = = 1) {break;}} int main () {int arr [10] = {2arr [10] = {2Mae 3pje 6pje 7je 0je 0je 3je 2pi 10}; int sz = sizeof (arr) / sizeof (arr [0]) BubbleSort (arr, sz); return 0;} 3. Using the idea of bubble sorting to simulate the implementation of qsort function

What parameters are used by the qsort library function, the function we designed will use the parameters!

1. Why convert base forced types to char* pointers:

Reason: char* pointer + 1 skips one byte, + width: skips width bytes and points to the next element. It is not appropriate to convert to other types

two。 Swap function: also pass the width (the number of bytes per element)

Because the address is passed when switching, it is important to know the width of the element, a byte swap, which also proves the benefits of using char* pointers!

3. (char*) base + j * width, (char*) base + (j + 1) * width

When j = 0: the first element and the second element are compared

J = 1, the second element and the third element are compared

.... It's a wonderful way to write.

/ / swap-A swap of one byte for a total of width times void Swap (char* buf1, char* buf2, size_t width) {size_t I = 0; for (I = 0; I

< width; i++) { char tmp = *buf1; *buf1 = *buf2; *buf2 = tmp; buf1++; buf2++; }}void my_BubbleSort(void* base, size_t num,size_t width, int(*cmp)(const void* e1, const void* e2)){ //冒泡排序 //若要排序n个元素,只需要进行n-1趟 //每一趟可以少比较一个元素,每一趟可以使一个元素在确定的位置上 //num:要排序元素的个数 类型是size_t //num是无符号数 防止产生警告 所以i和j也定义为size_t // size_t == unsigned int size_t i = 0; size_t j = 0; //共进行num-1趟 for (i = 0; i < num; i++) { //每一趟 for (j = 0; j < num - 1 - i; j++) { //比较 //传地址 //相邻两个元素比较 width:宽度,每个元素所占字节 //排成升序 if (cmp((char*)base + j * width, (char*)base + (j + 1) * width) >

0) {/ / Exchange two digits Swap ((char*) base + j * width, (char*) base + (j + 1) * width, width);}}

Of course, the exchange can also use the library function memcpy

Dest: target SPAC

Src: the character to be copied to the target space-because it is not modified, it can be decorated with const

Count: number of bytes

Char tmp [30]; / / prevent type temporary spaces such as structure types memcpy (tmp, (char*) base + j * size, size); memcpy ((char*) base + j * size, (char*) base + (j + 1) * size, size); memcpy ((char*) base + (j + 1) * size, tmp, size) These are all the contents of the article "how to use qsort functions in C language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report