Ladies and gentlemen, crossing, the last time we were talking about a quick sort of example, let's not say an example, let's talk about what we said earlier.
Sort to summarize. Gossip Hugh, words return to the positive. Let's talk C chestnuts together!
Crossing, we said in the previous few time the use of various sorting algorithms, they each have their own characteristics, I am here to do a unified summary:
The easy-to- understand sorting algorithm is bubble sort, and it's easy to implement, but it's relatively low performance. The hardest to understand is the quick sort, but
Its performance is the best . Insert sort and select sort in terms of ease of understanding and performance, between bubble sort and hill sort. Hill
Sorting is an upgraded version of the sort that is inserted, and it is easy to understand the sort of hill after inserting the sort. And the performance of the hill sort is better than the insert sort
High, but lower than the fast sort.
The performance I said above is only relative , and each sort algorithm can perform its performance advantage under certain conditions. We say that some
The sort algorithm has high performance and is an average . For example, in the previous examples, the number of elements in the container is 10.
After comparing the worst-performing bubble sort and the performance of the best quick sort, we can see that they are executed the same time and will show you
Example. This is because the number of elements in the container is small, so the advantages of fast sequencing are not shown. Therefore, everyone in the work, can be based on
The actual situation to choose a different sorting algorithm, if not pay attention to performance, then choose an easy to implement the sorting algorithm can be, so write
Out of the program is also easy to read. If the sort of content is not much, the use of a medium-performance hill sort is a good choice if the sort of inside
, it is best to use fast, high-performance sorting.
Crossing, here are the results I tested with the time command in the Linux terminal for your reference. You can modify the elements in the container
Number to test the performance of each sorting algorithm.
Time./bubblesort //test bubble Sort by
The ELMT of array is:
70 27 52 96 38 83 35 0 85 50
After sort, the elmt of an array is:
0 27 35 38 50 52 70 83 85 96
Real 0m0.002s//program run time
User 0m0.000s
SYS 0m0.000s
Time./quicksort //test for quick sort
The ELMT of array is:
36 73 41 0 37 21 94 91 26 98
After sort, the elmt of an array is:
0 21 26 36 37 41 73 91 94 98
Real 0m0.002s //program run time
User 0m0.000s
SYS 0m0.000s
Crossing, if you forget the principles of these sorts, click on the links below to view them:
To find out, bubble sort, click here
To find out, insert sort, click here
To learn more about the hill sort, please click here
To find out, select Sort, click here
To find out, quick Sort, click here
You crossing, the summary of the sort we're going to talk about here. I want to know what the following example, and listen to tell.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Talk C Chestnut Bar (31st time: C-language example-General sort summary)