Bubble sort and Hill sort (31)
In the previous blog, we learned about insert sorting and selection sorting, so this time we continue to learn about bubble sorting and Hill sorting. What is bubble sorting? It is the keyword of comparing V [j-1] and V [j] every time from back to forward (suppose I), j = n-1, n-2,., I; if reverse occurs, exchange V [j-1] and V [j]. Let's take a look at an example of the first bubble sort, as shown in the following figure
Let's take a look at how it is implemented, as shown below
We see that it is a pairwise comparison, with the small one in front. It's like blistering, floating lightly on it. Let's take a look at the implementation of the specific source code.
Template
< typename T >Static void Bubble (T array [], int len, bool min2max = true) {bool exchange = true; for (int iTuno; (ii; jmuri -) {if (min2max?) (array [j]
< array[j-1]) : (array[j] >Array [j-1]) {Swap (array [j], Array [j-1]); exchange = true;}
The test code is as follows
# include # include "Sort.h" using namespace std;using namespace DTLib;int main () {int array [] = {3, 2, 4, 1, 5}; Sort::Bubble (array, 5); for (int iTuno; I