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