Use bubble sort to complete the function of library function qsort

Insert image description here
Hello, what we share today is that we use the bubble function to implement qsort, which is quick sort. We have also talked about how to use the library function qsort before. Today we try to use the bubble function to implement it. Of course, we have also seen qsort, and we will also see it later. Will continue to improve. These days I am a college student who has broken defenses, alas!
Let's first take a look at what qsort is like. We can log in to the website cplusplus.
Insert image description here
Insert image description here
Through this website, we can see how to use qsor. First of all, we can see that its return type is void, and the parameters are base, num, size, and Compar What we can see from English is that base is actually what we want to sort. The previous one used a void*
pointer. We can think of void as actually a trash can. It can receive anything. An array can also be a structure, so void* here indicates that the type pointed to by this pointer can be int, a structure, or a double type, whichever is OK.

Then let’s first write what a bubble function looks like. I believe those who have read my previous article will know this, hand-made bubble sorting.

void bubble(int* arr, int sz)
{
    
    
	int i = 0;
	for (i = 0; i < sz - 1; i++)
	{
    
    
		int j = 0;
		for (j = 0; i < sz - 1 - i; j++)
		{
    
    
			if (arr[j] > arr[j + 1])
			{
    
    
				int tmp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = arr[j];
			}
		}
	}
}

Let’s look at our bubbling program. The first loop determines the number of times we sort. For example, if we have ten elements, do we have to sort them ten times? In fact, nine times is enough. The last one is already sorted, so then There is no need to sort, so only nine times are needed. Because we assume that it is in ascending order, that is, the previous one is greater than the next one. So in the first pass, we can put the largest number at the end, so We need to subtract i when looping later. Bubble sorting must be very simple for everyone.
So what should we do with bubble sort when we simulate the implementation of qsort? First, we can use the parameters of qsort

Insert image description here
Then we can write it like this void bubble(void* base, int num, int sz, int (*cmp)(const void*, const void*)), so what we need to do afterwards is actually the same as the bubble sorting idea, the number of times in the first loop, so what we can see is the following code

void bubble(void* base, int num, int sz, int (*cmp)(const void*, const void*))
{
    
    
	int i = 0;
	for (i = 0; i < num - 1; i++)
	{
    
    
		int j = 0;
		for (j = 0; j < num - 1 - i; j++)
		{
    
    
			if (cmp((char*)base + j * sz, (char*)base + (j + 1) * sz) > 0)
			{
    
    
				swap((char*)base + j * sz, (char*)base + (j + 1) * sz, sz);
			}
		}
	}
}

I have released them all here, and it is easier to explain. First of all, we use a function to determine whether it is greater than 0. How to implement the function? The parameters passed here are a particularly important place. Why do we say this? See When we get to the code, we first need to cast it into the char* type, because this will facilitate our subsequent access. We can first take a look at the comparison function.

int cmp(const void* e1, const void* e2)
{
    
    
	return *(int*)e1 - *(int*)e2;
}

What we are comparing is an integer type, so we have to cast it to an integer type first, and then we can start accessing the dereference operation. We also use the void type to receive pointers, so there is also an advantage here, that is, we can compare any type of data, such as double. Type, what we need to change is to convert it to a double type, and the structure can also be sorted. Does this satisfy our quick sorting and can sort a lot of data?
Then let's improve the swap function again. Looking at the code, we find that the parameters we pass are actually the same. Why is this? Because we don't know what type of data it is. So we need to know this type of data here one by one. Size, all need to pass an sz parameter, then let’s take a look at the code of the exchange function

void swap(char* e1, char* e2,int sz)
{
    
    
	int i = 0;
	for (i = 0; i < sz; i++)
	{
    
    
		char tmp = *e1;
		*e1 = *e2;
		*e2 = tmp;
		e1++;
		e2++;
	}
}

There is a misunderstanding here. I also made it at the beginning. I hope you won’t make it. First, we pass the pointer and write the char type. What we receive is a pointer like char*. Then what is exchanged is not the address, because the pointer is the address. We know this, so here we are exchanging the characters pointed to by the pointer. We are exchanging one by one, so we need to set a loop outside so that we can all exchange. Here is the reason why we also need to pass a parameter sz. , the following is the complete code

#include<stdio.h>
void print(int arr[], int sz)
{
    
    
	int i = 0;
	for (i = 0; i < sz; i++)
	{
    
    
		printf("%d ", arr[i]);
	}
	printf("\n");
}
int cmp(const void* e1, const void* e2)
{
    
    
	return *(int*)e1 - *(int*)e2;
}
void swap(char* e1, char* e2,int sz)
{
    
    
	int i = 0;
	for (i = 0; i < sz; i++)
	{
    
    
		char tmp = *e1;
		*e1 = *e2;
		*e2 = tmp;
		e1++;
		e2++;
	}
}
void bubble(void* base, int num, int sz, int (*cmp)(const void*, const void*))
{
    
    
	int i = 0;
	for (i = 0; i < num - 1; i++)
	{
    
    
		int j = 0;
		for (j = 0; j < num - 1 - i; j++)
		{
    
    
			if (cmp((char*)base + j * sz, (char*)base + (j + 1) * sz) > 0)
			{
    
    
				swap((char*)base + j * sz, (char*)base + (j + 1) * sz, sz);
			}
		}
	}
}
int main()
{
    
    
	int arr[] = {
    
     9,8,0,5,5,4,3,2,1 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	print(arr,sz);
	bubble(arr, sz, sizeof(arr[0]), cmp);
	print(arr,sz);
	return 0;
}

Insert image description here
Our bubbling version of the qsort function is also successfully implemented here. Let’s not talk about efficiency here. The main thing is to know this idea. When we learn the eight major sortings later, we will learn about quick sorting. There are three ways to implement it, so we don’t need to in a hurry.

Today is the second article I wrote in school. My computer broke down yesterday and I have to reinstall the system. Things have not always gone well lately. When things go wrong, I feel very sad. I also hope that I can adjust my mentality in the past few days. Thank you everyone. , we will share it later, let’s make progress together! ! !

Guess you like

Origin blog.csdn.net/2301_76895050/article/details/132864684