C language - use of qsort

qsort can sort any data

Usage form

qsort(void* base,size_t num,size_t size,int (*compar)(const void*,const void*));

             |                        |              |                                     |

             |                        |              |                                     |

        The address of the first element of the array |

                                      |

                                  The number of elements in the array Function pointer

The function pointer points to a function

The function pointed to is used to compare two elements of the array to be sorted

#include<stdio.h>
#include<stdlib.h>
int PaiXu(const void* p1, const void* p2)
{
	return (*(int*)p1 - *(int*)p2);
}
int main()
{
	int arr[] = { 1,3,5,7,9,2,4,6,8,0 };
	int rs = sizeof(arr) / sizeof(arr[0]);
	qsort(arr, rs, sizeof(int), PaiXu);
	for (int i = 0; i < rs; i++)
	{
		printf( "%d ", arr[i]);
	}
	return 0;
}

               

Guess you like

Origin blog.csdn.net/2301_79201049/article/details/134478907