Interpretation and simulation implementation of important library functions in c language-Qsort

Qsort function

Insert picture description hereUse this function to sort any data type.

Bubble Sort

Given arr as shown in the figure, the 10 elements are required to be sorted from small to large. The sorting
Insert picture description here
idea is compared before and after. If the front is smaller than the back, the order is satisfied, otherwise the order of exchange.
Insert picture description here
From this figure, it can be seen that the key to judgment lies in two points

  1. The comparison object is adjacent elements
  2. If the order is the first small and then the big, otherwise replace it to
    realize the judgment
if (arr[i] > arr[i + 1])
	{
    
    
		int tmp = arr[i];
		arr[i ] = arr[i+1];
		arr[i + 1] = tmp;

	}

After the if statement, you can exchange before and after
. How many times do you need to exchange? How many elements need to be exchanged?
What needs to be exchanged must be the entire array. How many times do we exchange? Of course, we don’t need to exchange the previous exchanges. So the number of exchanges is minus the number of elements that have been exchanged. The
essence of bubble sorting: the essence of bubble sorting order That is, the largest is changed to the last, and each time the order is sent to the last, the last one does not need to be changed. The last element is one less shot for one corresponding one.

int main()
{
    
    

	int arr[] = {
    
     1,22,67,89,45,67,34,56,78,2 };
	int i = 0;
	
	for (i = 0; i < 10 - 1; i++)
	{
    
    
		for (int j = 0; j < 10 - 1 - i; j++)
		{
    
    
			if (arr[j] > arr[j + 1])
			{
    
    
				int tmp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = tmp;

			}
		}
	}
	for (i = 0; i < 10 ;i++)
	{
    
    
		printf("%d ", arr[i]);
	}

	return 0;
}

The bubble sort looks to compare and actually shifts the large number back.

Qsort uses

Insert picture description here

Insert picture description here
Example to sort an integer array

#include <stdio.h>
#include<string.h>
#include<stdlib.h>
int _Int(const void* p, const void* r)
{
    
    
	return (*(int*)p - *(int*)r);
}
int _Char(const void* p, const void* r)
{
    
    
	return (*(char*)p -*(char*)r);
}
int main()
{
    
    
	int arr[] = {
    
     1,3,4,5,32,1,2,22,345,987 };
	char arr2[] = "afedpo";
	qsort(arr2, (sizeof(arr2) / sizeof(arr2[0])), 
		sizeof(arr2[0]),
		&_Char);

	for (int i = 0; i < 6; i++)
	{
    
    
		printf("%c", arr2[i]);
	}
	return 0;
}

Qsort simulation implementation

The Qsort simulation implementation uses the bubble sorting method
. The essence of bubble sorting is to compare the left and right adjacent elements and change the largest to the back
. The core of bubble sorting is the writing of if () judgment statements. The judgment of
plastic arrays is

`	if (arr[j] > arr[j + 1])
			{
    
    
				int tmp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = tmp;
`

Use Qsort to use our own function to pass parameters to complete

The key judgment is

if (cmp((char*)base + j * width, (char*)base + (j + 1) * width))
			{
    
    

			}

The step size of char is 1 plus the number of j elements that need to be skipped * the size of each element just jumps to

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>

int _Int(const void* p, const void* r)
{
    
    
	return *(char*)p - *(char*)r;
}

void _swap(char* buf1,char *buf2,int size)
{
    
    
	int i = 0;
	for (i=0;i<size;i++)
	{
    
    
		char a = *(buf1+i);
		*(buf1 + i) = *(buf2 + i);
		*(buf2 + i) = a;
	}
}

void My_qsot(void* base, 
	size_t num, 
	size_t width, 
	int(*com)(const void*p, const void*r))
{
    
    
	size_t i = 0;
	size_t j = 0;
	
	for (i = 0; i < num; i++)
	{
    
    
		for (j = 0; j < num - i - 1; j++)
		{
    
    
			if (com((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[] = {
    
     2,3,1,4,6,9,81,12,11,10 };
	size_t sz = sizeof(arr) / sizeof(arr[0]);
	My_qsot(arr,sz,sizeof(arr[0]),&_Int);
	for (int i = 0; i < sz; i++)
	{
    
    
		printf("%d ", arr[i]);
	}
	return 0;
}
  1. When copying, copy byte by byte. The smallest unit in bytes.

Guess you like

Origin blog.csdn.net/qq_45849625/article/details/114928037
Recommended