当前位置:网站首页>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
边栏推荐
- M - smooth engineering continuation (minimum spanning tree)
- Advanced functions of ES6 operation array map (), filter (), reduce()
- Infrastructure is code. What are you talking about?
- At the beginning of the 2022 new year, I will send you hundreds of dry articles
- Talk about why I started technical writing
- J - Borg maze (minimum spanning tree +bfs)
- Don't fight for big companies
- Steps for commissioning of vertical machining center
- H - Arctic network (minimum spanning tree)
- 高精度CNC加工中心为什么会出现误差?这4个原因你要注意!
猜你喜欢

Xiao Sha's pain (thinking problem)

Webrtc: industrial application based on Internet of things

Is Domain Driven Design (DDD) reliable?

NPM install --global --save --save dev differences

Infrastructure is code. What are you talking about?

Summary of system stability construction practice
![[matlab] 3D drawing summary](/img/57/05156340ccdd79b866c4df955b3713.jpg)
[matlab] 3D drawing summary

Anyrtc implements application scenarios based on webrtc

How to get palindrome number in MATLAB (using fliplr function)

Scattered knowledge of C language (unfinished)
随机推荐
1018 public bike Management (30 points)
About pickle module - 6 points that beginners must know
4.3 variables and assignments
1107 social clusters (30 points)
Web technology sharing | whiteboard toolbar encapsulation of Web
The difference between queue and stack
Management joint examination - mathematical formula
N - Is There A Second Way Left? (minimum spanning tree, Kruskal)
Matlab draws the image of the larger function value of the two functions (super simple)
Technology sharing | how to quickly realize audio and video online calls
Scattered knowledge of C language (unfinished)
At the beginning of the 2022 new year, I will send you hundreds of dry articles
Matlab function for limit, definite integral, first-order derivative, second-order derivative (classic examples)
Average and maximum values of MATLAB matrix
Oracle中的With As 子查询
The crystal ball "data insight" was officially launched: insight into the change of consumption trend and the details of interactive experience
Bucket sorting (C language)
Matlab judge palindrome number (only numbers)
Machine learning feature selection
1015 reversible primes (20 points)

