Bubble Sort Method
HTML5 Academy-code Carpenter: This issue continues to go into the algorithm-bubble sorting method. The bubble sorting algorithm is relatively simple, easy to get started, stability is also relatively high, is a better understanding of the algorithm, but also the interviewer high-frequency questioning one of the algorithms.
Tips: On the "algorithm" and "sort" of the basic knowledge, in the previous "Choice of sorting method" has been explained in detail, you can click on the relevant article link to view, here no longer repeat.
Fundamentals of Bubble Sorting method
Starting from the head of the sequence, 22 comparison, if the former is larger than the latter, then swap positions until the maximum number (the largest number of this order) is exchanged to the end of the unordered sequence, thus becoming part of the ordered sequence;
The next iteration, the maximum number after each traversal is no longer involved in the sorting;
Repeat this operation many times until the sequence sort is complete.
Since a decimal is always placed forward in the sorting process, the large number is placed backwards, similar to the bubbles gradually floating upward, so called bubble sort.
Schematic diagram
Tips: Blue represents waiting for an interchange in a round of sorting, the black representation has been exchanged in the round order, and the red represents the sorted finish
Steps to implement bubbling decomposition using a For loop to determine the number of sorts
Since the sequence to be sorted has been able to determine the order with only one number left, no sorting is required, so the number of sorts is the sequence length –1.
Number of comparisons per sort control
For each sort, multiple numbers in the sequence are 22 compared, and multiple comparisons need to be implemented using a for statement. The For loop is nested within the for loop of the sort count (forming a double for nesting).
The reason tips:j needs to be set to less than len-i-1, minus I is that the number of sorted finishes is no longer participating in the comparison, minus 1 because the array subscript index value starts at 0.
Core functions-22 comparison and exchange of positions as appropriate
Compare two number size, if the former is larger than the latter, then the value of the exchange, that is, the swap position.
Optimization of bubble Sort method for full code bubbling Sorting
If the data of the sequence is: [0, 1, 2, 3, 4, 5];
Using the above bubble sort method, the resulting results are certainly no problem, but the sequence to be sorted is ordered, theoretically without traversing the sort.
The current algorithm, regardless of the order of the initial sequence, will be traversed sort, the efficiency will be relatively low, so need to optimize the current sorting algorithm.
In the following algorithm, a swap variable is introduced, initialized to false before each order, and set to true if the two-digit interchange position occurs.
Determines whether the swap is false at the end of each sort, or, if it is, if the sequence is sorted or the sequence itself is an ordered sequence, the next order is no longer performed.
By this method, the performance of the algorithm is further improved by reducing unnecessary comparison and position exchange.
The efficiency time complexity of bubble sort method
Best state: The sequence to be sorted itself is ordered sequence, the number of orders according to the optimized code, can be obtained is n-1 times, the time complexity of O (n);
Worst case scenario: the sequence to be sorted is in reverse order, at which point 1 + 2 +3 is required ... (n-1) = N (n–1)/2 times
The time complexity is O (n^2).
Complexity of space
The bubble sort method requires an extra space (the TEMP variable) to swap the position of the element, so the spatial complexity is O (1).
Stability of the algorithm
When the adjacent elements are equal, there is no need to swap positions, and there will be no changes in the order of the same elements, so the stability is sorted.
o what is it?!
Time complexity, more accurately, is to describe an algorithm in the size of the problem is increasing the corresponding time growth curve. Therefore, these growth orders are not an accurate performance evaluation and can be understood as an approximation. (spatial complexity is the same)
O (n?) Indicates that when n is very large, the complexity is approximately equal to cn?,c is a constant, simply saying that when n is large enough, the linear growth complexity of n increases along the square.
O (n) indicates that n is very large when the complexity is approximately equal to cn,c is a constant. In short: Along with the linear growth of N, the complexity increases along the level of the sex.
O (1) says that when N is very large, the complexity does not increase at all. In short: With the linear growth of N, the complexity is not affected by N and grows along the constant level (the constant here is 1).
Tips: The O (1) in the picture is close to the x-axis and is not clearly visible.
Tips: This image is from the "Stack Overflow" website.
Related Articles Links
Select Sort method
Happy Heart every day
Life is hard, code is not easy, but don't forget to smile!
Copyright notice: The picture is from "Beauty" Lisa Ze Crimo (author) "Book" You are really nice today "
Algorithmic Tours | Bubble Sort Method