C language-qsort function arranges data and simulates implementation

Table of contents

1.Library function qsort

2. Use qsort to arrange various types of data

3. Implement qsort through bubble sort simulation


1.Library function qsort

#include<stdlib.h>

int main()
{
    void qsort( void *base, size_t num, size_t width, int (__cdecl *compare )(const void 
   *elem1, const void *elem2 ) );

     return 0;
}

Definition: Perform quick sort

Header file: <stdlib.h>

Note: The qsort function implements a quick sort algorithm to sort an array containing num elements, with the width/byte of each element. The actual parameter base is a pointer to the starting position of the array to be sorted. Qsort overwrites this array with sorted elements. The argument compare is a pointer to a user-supplied routine that compares two array elements and returns a value that specifies the relationship between them. Qsort calls the comparison routine one or more times during the sorting process, passing each call a pointer to two array elements:

 Note: The reason why the comparison function needs to be customized here is because when users compare data, the comparison types include but are not limited to integers, floating point types, and even structure type data. Users can customize it according to their own needs. To meet the needs, design functions that specifically compare different types.

2. Use qsort to arrange various types of data

      2.1 qsort sorts integer types

#include<stdlib.h>

void print(int arr[], int sz)//打印数组
{
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
}

int __cdelc(const void* e1, const void* e2)//自定义比较函数
{
	if (*(int*)e1 , *(int*)e2)
	{
		return  (*(int*)e1 - *(int*)e2);
	}
}


void test1() //qsort函数排序整型数组
{
	int arr[] = { 1,3,5,7,9,2,4,6,8,10 };

	int sz = sizeof(arr) / sizeof(arr[0]);


	qsort(arr, sz, sizeof(arr[0]), __cdelc);

	print(arr, sz);
}


int main()
{
	test1();

	return 0;
}

   

  2.2 qsort sorting structure type

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

struct stu//定义结构体类型
{
	char name[10];
	int age;
	double score;
};

void print2(struct stu arr[], int sz)//打印
{
		int i = 0;
		for (i = 0; i < sz; i++)
		{
			printf("%s, %d, %lf\n", arr[i].name, arr[i].age, arr[i].score);
		}
}

int __cdelc2_by_age(const void* e1, const void* e2)//通过年龄比较
{
	return (((struct stu*)e1)->age - ((struct stu*)e2)->age);
}

int __cdelc2_by_name(const void* e1, const void* e2)//通过名字比较
{
	return strcpy( ((struct stu*)e1)->name, ((struct stu*)e2)->name );
}

test2()
{
	struct  stu arr[] = {
   
   {"小明",15,65.5},{"小强",19,80.5},{"小美",17,70.0}};
	int sz = sizeof(arr) / sizeof(arr[0]);

	qsort(arr, sz, sizeof(arr[0]), __cdelc2_by_age);
	//qsort(arr, sz, sizeof(arr[0]), __cdelc2_by_name);

	print2(arr, sz);
}

int main()
{
	test2();

	return 0;
}

3. Implement qsort through bubble sort simulation

 code show as below:

#include<stdio.h>
void print3(int arr[], int sz)//打印
{
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
}


void swap(char* buf1, char* buf2, int width) //交换函数
{
	int i = 0;
	for (i = 0; i < width; i++)
	{
		char* tmp = *buf1;
		*buf1 = *buf2;
		*buf2 = tmp;
		buf1++;
		buf2++;
	}
}

void  bubble_qsort(void* base, int num, int width, int(*cmp)(const void* e1, const void* e2))
{
	int i = 0;
	int j = 0;
	for (i = 0; i < num - 1; i++)
	{
		for (j = 0; j < num - 1 - i; j++)
		{
			if (cmp((char*)base + j * width, (char*)base + (j + 1) * width) > 0) //比较
			{
				swap((char*)base + j * width, (char*)base + (j + 1) * width, width);//交换
			}
		}   
	}
}


int __cdelc3(const void* e1, const void* e2)//比较函数
{
	return (*(int*)e1 - *(int*)e2);
}


void test3()
{
	int arr[] = { 1,3,5,7,9,2,4,6,8,10 };

	int sz = sizeof(arr) / sizeof(arr[0]);

	bubble_qsort(arr, sz, sizeof(arr[0]), __cdelc3);

	print3(arr, sz);
}


int main()
{
	test3();

	return 0;
}

Idea:

 Ideas for comparing elements:

Ideas for exchanging elements:

 


The above is for reference only, corrections are welcome

Guess you like

Origin blog.csdn.net/m0_73969113/article/details/130829929