Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Bubble sort and Hill sort (31)

Shulou Source: shulou.com Published: 2022-06-03 01:02:21 09月10日 Update

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

Tags: Sorting Hill learning complexity reason parameter complexity sequence effect efficiency source code example highest ordered from small to big small code element key keyword Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Apple MySQL Huawei MariaDB