Bubble sorting to realize quick sorting qsort C language

Posted by tsilenzio on Wed, 02 Feb 2022 11:36:44 +0100

Catalogue

preface

Quick sort

Basic use

Parameter interpretation

Simulation Implementation

preface

First of all, I wish you a happy New Year 🎉 🎉 🎉!

hello ✨, Hello, everyone. This is the original 💖 💛💙, With more and more articles, there may be many partners who can't find the articles they want to read, so I came out. Here are links to various articles. 🎁 🎁 🎁

The introduction and deep analysis of C language are arranged in here Ha, the advanced chapter is also in a hurry. It should be finished in less than a week, and then here It's a personal homepage. It's better to find articles than nodding avatars. Then there is the primary data structure, leaving only binary tree and eight sorting. It will be summarized later, and the links will also be put here. Later, you should know more about c + + and linux. Of course, advanced data structures will also be updated in c + + 😜. If you want to learn about c\c + +, paying attention to bloggers will be helpful to you 🎁 🎁 🎁 . Finally, thank you for watching this article 💖, Thank you for your attention 💖🎈 🎈, Let's refuel together! 🎉 🎉 🎉

Finally, there is the chicken blood link: it is really difficult to change, but the result is worth taking a risk and showing some courage. It's still a long way to go. It's just the beginning now. The past is irreversible and the future can be changed. 🚀 🚀 🚀

Quick sort

Before implementing quick sort, we need to know what quick sort is. In short, it can sort various types of data, such as integers, structures, etc. we use bubble sorting to write here, because we only learn bubble sorting at present. Ha, there will be other versions later,

Here we see that qsort has four parameters, which are explained below.

Basic use

It is worth noting that we need to implement a comparison function ourselves. As for why, the person who writes the qsort function does not know what users want to compare, or sort from large to small or from small to large, so we need to implement a comparison function ourselves, because users know what type of data they want to compare.  

Parameter interpretation

The first parameter of qsort function is the starting address of the array to be compared, the second parameter is the number of array elements, the third parameter is the size of each element, and the fourth parameter is the comparison function.  

Simulation Implementation

#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<stdlib.h>

//The integer here is written like this, but it will change when compared with other data
int cmp_int(const void* e1,const void* e2)
{
	return (*(int*)e1 - *(int*)e2);
}

//Print function
void Print(int arr[],int sz)
{
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%d ",arr[i]);
	}
}

//Exchange function
void swap(void* p1, void* p2, size_t width)
{
	int i = 0;
    //Because you have to swap so many length times, you need a for loop
	for (i = 0; i < width; i++)
	{
		char tmp = *((char*)p1 + i);
		*((char*)p1 + i) = *((char*)p2 + i);
		*((char*)p2+i) = tmp;
	}
}

void bubble_qsort(void* base, size_t num, size_t width, int(* cmp)(const void* e1, const void* e2))
{
	int i = 0;
	int j = 0;
	for (i = 0; i < num; i++)
	{
		for (j = 0; j < num-i-1; j++)
		{
            //Cast the type to char * and add j*width here, which is worth noting
			if (cmp((char*)base + j*width, (char*)base + (j + 1)*width)>0)
				swap((char*)base + j * width, (char*)base + (j + 1) * width,width);
		}
	}
}

int main()
{
	int arr[] = { 1,3,2,5,7,9,8,4,6 };
	int sz = sizeof(arr) / sizeof(arr[0]);

	//qsort(arr, sz, sizeof(int), cmp_int);
	bubble_qsort(arr, sz, sizeof(int), cmp_int);
	Print(arr, sz);
	return 0;
}
  1. First of all, we should note that the parameter types are void * for better use, because the author doesn't know what type we want to use.
  2. Then it is internally converted to char * type, so that it can be compared and exchanged byte by byte.
  3. The implementation here is bubble_ The qsort function should pay attention to two for loops: one is less than num, the other is less than num-i-1, which is the total number of times, and the other is the number of times each element needs to be compared. This is the same as bubble sorting.  

That's all for today!!!

If you think the author is a little helpful to you!

Just pay attention!!! Of course, subscription is even more desirable!

Give someone a rose, the hand has a lingering fragrance =. =!

Finally, thank you for watching!!!

Your support is the greatest driving force for the author to write!!!

See you next time!!!

Topics: C Algorithm data structure quick sort