Bubble sort why the best responsible degree is O (n)
As the saying goes, martial arts is high-strength or not is a person's internal skills, the duel between the real masters, are the internal organs! Programmers are the same, in order to advance, must be from the basis of the crawl, the algorithm is the most important, so recently in the study of algorithms, first from the sorting practice.
Often see the book, the blog said bubble sort the best complexity of O (n), but I look at the code has been counted O (n^2), the code is as follows:
Later, after some research, I found that the bubble sort I wrote is just a basic demo, the complexity is always O (n^2), and then after some optimization, found that the best complexity can reach O (n), the code is as follows:
When the passed in array is a positive sequence, it executes
This way, the first layer loop executes only 1 times, so the complexity is O (N).
Make a record here, hoping to help the classmates who have doubts.
,
Bubble sort why the best responsible degree is O (n)