Comparison of the use of qsort library function and bubble_sort (bubble sort) and detailed explanation of qsort function

Both are also sorting methods, so what are the advantages of the qsort library function?

First we have to understand bubble sorting

1. Only ordered arrays can be arranged

2. The time complexity is high and unstable;

The idea of ​​bubble sort is to compare elements in an array pair by pair.

 As shown in the picture above, it takes 8 pairwise exchanges to determine the position of a number.

Therefore, it takes a total of eight such loops to determine the position of each number in ascending order, which is very inefficient;

The following introduces an efficient library function qsort, which needs to reference the header file <stdlib.h>

The advantages of qsort are relatively obvious

It can be used directly and can sort any type of data

void qsort ( 

void* base, //要排序的目标数组
size_t num,     //待排序的元素个数
size_t width,    //一个元素的大小,单位是字节
int(*cmp)(const void* e1, const void* e2)

);

 qsort has four parameters, the first three are as shown in the comments above

The last parameter is a function pointer

int(*cmp)(const void* e1,const void* e2)

This pointer points to a function. This function needs to compare the sizes of two elements, so we need to type this function ourselves.

Then we have to discuss why we have to write this function ourselves;

As mentioned earlier, qsort can sort any type of data.

If we want to compare the size of two numbers, just use > and <;

If we want to compare the size of strings, we need to use strcmp;

If we want to compare the size of structure data, neither of the above methods is feasible, so we need to specify a standard to judge the size of two elements. As long as we tell the qsort function how the two elements compare, then qsoer can Help you sort out the order;

Here we continue to take an integer array as an example;

test()
{
	int arr[] = { 9,8,7,6,5,4,3,2,1 ,0};
	int sz = sizeof(arr) / sizeof(arr[0]);
	qsort(arr, sz, sizeof(arr[0]), cmp_int);//qsort是默认排成降序的
	print_arr(arr, sz);
}
int main()
{
	test();
	return 0;
}

 The parameters in qsort are the target array to be sorted, the number of elements to be sorted, the size of one of the elements (in bytes), and a function to compare the sizes of the two data;

int(*cmp)(const void* e1,const void* e2)

The parameters in the cmp function are the addresses of the two elements to be compared. For example, if we compare 9 and 8, then pass the address of 9 to the first parameter, which is e1; pass the address of 8 to the second parameter. Parameter, that is, e2; using a void* type pointer is also because qosrt can be any type of data, so the two elements to be compared cannot be determined, and a void* type pointer must be used, as follows

int cmp_int(const void* p1, const void* p2)
{
	
}

Next, you need to use pointers to find two integers to compare their sizes. Can you directly compare *p1 and *p2?

Obviously not

Because the parameter of the function is a const void pointer, the void* type pointer is an untyped pointer and can also be considered a universal type pointer. It can accept any type of address.

Therefore, when we apply pointer solution, we need to cast its type.

Finally write it as *(int*)p1, so that the value in the pointer can be applied

Finally, compare the two numbers. Then you might as well make the difference between the two numbers; as follows:

int cmp_int(const void* p1, const void* p2)
{
	return *(int*)p1 - *(int*)p2;
}

In this way, the return value of the function can be >0, =0 or <0, so that the parameters in the qsort function are complete. Next, just print out the content. The complete code is as follows

int cmp_int(const void* p1, const void* p2)
{
	return *(int*)p1 - *(int*)p2;//按照降序排列;
	//return *(int*)p2 - *(int*)p1;//按照升序排列;
}
void print_arr(int arr[],int sz)
{
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}
test()
{
	int arr[] = { 9,8,7,6,5,4,3,2,1 ,0};
	int sz = sizeof(arr) / sizeof(arr[0]);
	qsort(arr, sz, sizeof(arr[0]), cmp_int);//qsort是默认排成降序的
	print_arr(arr, sz);
}
int main()
{
	test();
	return 0;
}

I believe you will gain something 

Guess you like

Origin blog.csdn.net/wangduduniubi/article/details/129268750