Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Bubble sort and Hill sort (31)

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report