当前位置:网站首页>Advanced C language - pointer 3 - knowledge points sorting
Advanced C language - pointer 3 - knowledge points sorting
2022-06-30 15:34:00 【Confused guy】
Catalog
One 、 VC & Delphi , Simulation Implementation qsort:
Two 、 Use callback letter , Simulation Implementation qsort( By bubbling )
3、 ... and 、 Pointer and array written test question analysis
Callback function :
1. Is a function called through a function pointer , If you put a pointer to a function ( Address ) Pass as argument to another function , When this pointer is used to call the function it points to , We call this function a callback function .
2. The callback function is not called directly by the function's implementer , But by... When a specific event or condition occurs
Called by the other party , Used to respond to the event or condition .
// Code 1:
// Bubble sort -- It can only be used in plastic surgery
//void bubble_sort(int* arr,int sz)
//{
//
// int i = 0;
// int flag = 1;// If there is no exchange on the first trip , This indicates that the array is in the given order
// for (i = 0;i < sz - 1;i++)// Number of trips ,10 Elements should be 9 Trip to , The last element , There's no need to sort
// {
// // Exchange per trip The first trip is 9 Secondary exchange
// int j = 0;
// for (j = 0; j< sz -1 - i ;j++)
// {
// if (arr[j] > arr[j+1])
// {
// int temp = arr[j];
// arr[j] = arr[j + 1];
// arr[j + 1] = temp;
// flag = 0;
// }
// }
// if (flag == 1)
// {
// break;
// }
// }
//}
//int main()
//{
// int arr[] = {9,8,7,6,5,4,3,2,1,0};
// int sz = sizeof(arr) / sizeof(arr[0]);
// bubble_sort(arr,sz);
// int i = 0;
// for (i = 0;i < sz;i++ )
// {
// printf("%d ",arr[i]);
// }
// return 0;
//}One 、 VC & Delphi , Simulation Implementation qsort:
#include<stdlib.h>
//qsort Can be applied to any type of comparison
/*void qsort(void* base, size_t num, size_t size,int (*compar)(const void*, const void*));*/// Initial address , Number , The size of each element , Compare elements
int cmp_int(const void* e1,const void* e2)
{
return *((int*)e1) - *((int*)e2);
}
struct Stu
{
char Name[20];
int age;
};
int cmp_by_name(const void* e1,const void* e2)
{
return strcmp(((struct Stu*)e1)->Name, ((struct Stu*)e2)->Name);
}
int cmp_by_age(const void* e1, const void* e2)
{
return (((struct Stu*)e1)->age - ((struct Stu*)e2)->age);
}
void test1()
{
int arr[] = { 9,8,7,6,5,4,3,2,1,0 };
int sz = sizeof(arr) / sizeof(arr[0]);
qsort(arr, sz, 4, cmp_int);
print(arr, sz);
}
void test2()
{
struct Stu s[3] = { {" Zhang San ",30},{" Li Si ",40},{" Wang Wu ",50} };
int sz = sizeof(s) / sizeof(s[0]);
int i = 0;
/// Compare by name
qsort(s,sz,sizeof(s[0]),cmp_by_name);
for (i = 0; i < 3; i++)
{
printf("%s\n", s[i].Name);
}
// Compare by age
qsort(s, sz, sizeof(s[0]), cmp_by_age);
for (i = 0; i < 3; i++)
{
printf("%d\n", s[i].age);
}
}
int main()
{
test1();
test2();
return 0;
}Two 、 Use callback letter , Simulation Implementation qsort( By bubbling )
int cmp_int(const void* e1,const void* e2)
{
return *((int*)e1) - *((int*)e2);
}
struct Stu
{
char Name[20];
int age;
};
int cmp_by_name(const void* e1,const void* e2)
{
return strcmp(((struct Stu*)e1)->Name, ((struct Stu*)e2)->Name);
}
int cmp_by_age(const void* e1, const void* e2)
{
return (((struct Stu*)e1)->age - ((struct Stu*)e2)->age);
}
void Swap(char* a,char* b,size_t width)
{
size_t i = 0;
for (i = 0;i < width;i++)
{
char temp = *a;// One byte at a time
*a = *b;
*b = temp;
a++;
b++;
}
}
void bubble_sort(void* base, size_t sz, size_t width, int (*cmp)(const void* e1,const void* e2)) // Define function pointers ,e1.e2 Don't write
{
size_t i = 0;
for (i = 0;i < sz - 1;i++)// Number of trips ,10 Elements should be 9 Trip to , The last element , There's no need to sort
{
// Exchange per trip The first trip is 9 Secondary exchange
size_t j = 0;
for (j = 0; j< sz -1 - i ;j++)
{
if (cmp((char*)base+ j*width,(char*)base+(j+1)*width)>0)// Because I don't know how big it is , So use the smallest char, Every skip 1 Bytes
{
// In exchange for
Swap((char*)base + j * width, (char*)base + (j + 1) * width,width);
}
}
}
}
void print(int* arr, int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
}
// Test custom Bubblesort() Sort structure
void test2()
{
struct Stu s[3] = { {" Zhang San ",30},{" Li Si ",40},{" Wang Wu ",50} };
int sz = sizeof(s) / sizeof(s[0]);
int i = 0;
/// Compare by name
bubble_sort(s, sz, sizeof(s[0]), cmp_by_name);
for (i = 0; i < 3; i++)
{
printf("%s\n", s[i].Name);
}
// Compare by age
bubble_sort(s, sz, sizeof(s[0]), cmp_by_age);
for (i = 0; i < 3; i++)
{
printf("%d\n", s[i].age);
}
}
// Test custom Bubblesort();
void test3()
{
int arr[] = {9,8,7,6,5,4,3,2,1,0};
int sz = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr,sz,sizeof(arr[0]),cmp_int);
print(arr,sz);
}
int main()
{
test2();// Comparison of custom test structures
/*test3();*/// Customize bubblesort test
return 0;
}Print :
3、 ... and 、 Pointer and array written test question analysis
The above is the pointer 3 To sort out the knowledge points of , Such as incorrect , Please comment more in the comment area
边栏推荐
- Shift operator (detailed)
- 容器常用命令
- How should we understand the variability of architecture design?
- 001 basic knowledge (unfinished)
- 4.8 data type conversion
- 4.2 escape characters
- 1031 Hello world for u (20 points)
- Xiao Sha's pain (thinking problem)
- Management joint examination - mathematical formula
- 数控加工中心打刀缸工作原理及故障处理
猜你喜欢

Single cycle CPU of the design group of West University of Technology

Experiment of the planning group of the West University of technology -- pipeline CPU and data processing Adventure

Developer practice - the future of Agora home AI audio and video

Rte2021 review of the practice and the way of AI OPS landing

Rte2021 review HDR technology product practice and exploration

The principle of fluent 2 rendering and how to realize video rendering

The short video and live broadcast incubation training camp with goods opens nationwide enrollment!

How to do a good job in high concurrency system design? I have summarized three points

At the beginning of the 2022 new year, I will send you hundreds of dry articles

Oracle中的With As 子查询
随机推荐
DR-TANet: Dynamic Receptive Temporal Attention Network for Street Scene Change Detection
How many questions can you answer for the interview of Mechanical Engineer?
1136: password translation
1135: paired base chain
Technology sharing | how to quickly realize audio and video online calls
Some reference routines for cache update
Talk about why I started technical writing
Npumcm selection question 3 and acmc2020a
On which platform is it safer to buy Treasury reverse repo?
[ten thousand words long article] thoroughly understand load balancing
001 data type [basic]
openresty 内置变量
Repair of incorrect deletion of win10 boot entry
Matlab draws the image of the larger function value of the two functions (super simple)
4.3 variables and assignments
Win10 one click Reset win10 to solve all system bugs without deleting any files and Applications
C. Registration system(map)
Noj1042 - electronic mouse running through maze
Management joint examination - mathematical formula
1148 werewolf - Simple Version (20 points)

