Performance test of C-language qsort function algorithm

Source: Internet
Author: User

For the complexity of the algorithm, an intuitive perception method is the algorithm running time for measuring a certain amount of data.

As an example of the qsort provided in C, the calculation time is measured with 1 million data, and the time cost of O (NLG (n)) can be sensed:

The C code is as follows:

#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 1000000//int (*comp) (const void *, const void *) int compare (const void *P1, const void *P2) {return * (float*) p1 > * (float*) P2;} int main () {float X[n];srand (time (NULL)), clock_t T1 = Clock (), for (int j = 0; J <; J + +) {for (Long i = 0; i < N; I + +) X[i] = (float) rand ()/rand_max;qsort (x, N, sizeof (float), compare);}  for (int i = 0; i < i++) printf ("%f", X[i]);p rintf ("\ n"), clock_t t2 = Clock ();p rintf ("Time for floating-point sorting algorithm:%f sec \ n", (double) (T2 -T1)/clocks_per_sec); return 0;}

Compiled and run with GCC qsort_test.c on a notebook, 10 times 1 million random data generation and sequencing results are:

~/tmp$./a.out
0.000000 0.000001 0.000001 0.000002 0.000002 0.000004 0.000004 0.000005 0.000006 0.000006
Floating-point sorting algorithm spents: 2.236941 seconds

Performance test of C-language qsort function algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.