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 the quick sorting algorithm of qsort and bsearch in C language library

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

Share

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

This article is about how to use the function qsort and bsearch quick sorting algorithm in C language library. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Qsort

Qsrot is the quick sort function in the C language library function, which can quickly sort arrays and structures. it is used in header files with the following declaration format:

Void qsort (void* base, size_t nums, size_t size, int (* compare) (const void*, const void*))

What does it mean by such an annoying long list of parameters? base is a pointer to the first element of the array to be sorted. Nums is the number of elements in the array pointed to by base. Size is the size of each element in the array, in bytes. Compare is a function used to compare two elements, which needs to be completed by ourselves.

Meaning

Void* represents any type of array, which is the array of objects we want to sort; size_t is defined as int in the system, so we can default to an integer for the number modified by size_t.

Why refine the array size and element size? What does this have to do with my ranking? In fact, this is to distinguish between different types of arrays, int and char type array of each element occupies a different space, naturally to distinguish.

Int main () {int arr [6] = {1 int arr 4, 5 8, 2 3}; qsort (arr, 6, sizeof (arr [0]), compare);}

In the last compare function, I passed this element directly as an argument, so the question is, how to write this comparison function?

We don't care about the pointer of * compare at all. It is tantamount to telling you that an external function is being used here. We just need to understand that the name of the whole function is appropriate. The name of this function is not necessarily compare.

Realize

The following (const void, const void) is naturally the parameter of this function, the actual use of the two void* is regarded as a void* b, since it is an external function, we have to do it ourselves, our ultimate goal is to sort, the comparison function should achieve the comparison of the size of the array elements, in essence, it is to compare the size of an and b, and a void* b is any two elements in my array.

Well, the first thing to do is to change this unknown type of void pointer into the one given by us. The integer array given in the previous code will correspond to the integer pointer here. These two pointers point to the two integers in the array. Since we want to compare them, we can just do subtraction to see the positive and negative. After converting the two pointers into real integers, we are done:

Int* p = (int*) a; int* Q = (int*) b; int c = * p; int d = * Q

The finished products are as follows:

# includeint compare (const void* an int* Const void* b) {int* p = (int*) a; int* Q = (int*) b; int c = * p; int d = * q; return c-d;} int main () {int I = 0; int arr [6] = {1 int 4 5 int 8 2) 3}; qsort (arr, 6, sizeof (arr [0]), compare) For (I = 0; I < 6; iTunes +) {printf ("% d", arr [I]);} return 0;}

The results are as follows

The same is true for the ordering of structures, as follows:

# includeint compare (const void* an int* Const void* b) {int* p = (int*) a; int* Q = (int*) b; int c = * p; int d = * q; return c-d;} int main () {int I = 0; int arr [6] = {1 int 4 5 int 8 2) 3}; qsort (arr, 6, sizeof (arr [0]), compare) For (I = 0; I < 6; iTunes +) {printf ("% d", arr [I]);} return 0;}

The result is arranged according to the size of the a member in the structure:

Pattern open

1. The above is to achieve the arrangement from small to large, to achieve from large to small row only need return d-c.

two。 If you compare floating-point numbers, notice that when there is little difference between the two numbers, between (- 1), because now is an integer pointer, the return value is also an integer, return returns a 0, causing a meaningless operation, how to deal with it? It's very simple, just change it to the following:

Int compare (const void* a Const void* b) {int* p = (int*) a; int* Q = (int*) b; int c = * p; int d = * Q; if (c-d)

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