The qsort() function of C

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了The qsort() function of C相关的知识,希望对你有一定的参考价值。

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

#define MAX 5000

void print_arr(int *a, int size){
	int i;
	for(i = 0; i < size; ++i)
		printf("%d ", *(a + i));
	putchar(‘\n‘);
}

int compare(const void *a, const void *b){
	return (*(int *)a - *(int *)b);
}

int main(){
	srand(time(0));
	int arr[MAX];
	int i;
	
	/* initialize array */
	for(i = 0; i < MAX; ++i){
		arr[i] = rand() % MAX;
	}
	putchar(‘\n‘);
	
	clock_t begin = clock();
	qsort(arr, MAX, sizeof(int), compare);
	clock_t end = clock();

	print_arr(arr, MAX);
	puts("Done!");
	printf("Use time: %f\n", (double)(end - begin));
	
	return 0;
}

 

以上是关于The qsort() function of C的主要内容,如果未能解决你的问题,请参考以下文章

The log use to debug the function of ChangeTemplate

The difference of Methods and Functions

如何在C中对指向char的指针数组进行qsort?

Some Main Functionalities of the Dental Chair

Window Function--the function of window function

How to use the function of assembly.