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

Case Analysis of Bubble sorting in javascript

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "Bubble sorting case Analysis in javascript". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In JavaScript, bubble sorting is a sort method, the principle is that each number in an array is compared from the back to the back, and according to the size exchange position, each round of comparison determines a maximum value of the round comparison, and finally realizes the size sorting of the array.

The operating environment of this tutorial: windows10 system, javascript1.8.5 version, Dell G3 computer.

What does bubbling sort mean in javascript

What is Bubble sorting

Bubble sort, Bubble Sort, by comparing the size of two adjacent elements in turn, in each comparison process, the two elements are exchanged to achieve the purpose of order.

If a set of unordered sequences wants to be sorted from small to large, then the comparison of the two elements is achieved by exchange, and the element on the left is smaller than the element on the right.

If an unordered sequence wants to sort from large to small, then the comparison of the two elements is achieved by swapping, and the element on the left is larger than the element on the right.

Like a bubble in a carbonated drink, it bubbles from the bottom to the top.

The principle is to compare each number in an array from the back to the back, and then according to the size of the exchange position, each round of comparison determines a maximum value of the round comparison, and finally realizes the size sorting of the array.

Second, give an example

If there are 2, 4, 7, 5, 5, 3, 6, 1, this group of numbers

First round:

ITunes 0

J (inner loop) cycle 6 times, the work done by the inner loop: comparing the two adjacent numbers, the large one will eventually be put in the back, the small one in front, and a cycle.

The outer loop controls the number of times, and the inner loop makes a judgment.

Jennifer 0 1 2 3 4 5

2 2 2 24 4 4 4 47 7 7 55 5 55 55 7 33 33 33 3 7 6 66 66 66 7 11 11 11 1 7arr [0] arr [1] arr [2] arr [1] arr [2] arr [3]

Second round:

ITunes 1

J (inner loop) cycle 5 times

Jennifer 0 1 2 3 4 5

2 2 2 24 4 4 4 45 5 5 33 33 33 5 5 56 6 6 6 11 11 11 67 7 7 7 7arr [0] arr [1] arr [2] arr [1] arr [2] arr [3]

Round 3:

ITunes 2

J (inner loop) 4 cycles

2 2 2 24 4 33 33 3 4 4 45 5 5 5 11 11 1 56 6 6 6 67 7 7 7

Round 4:

ITunes 3

J (inner loop) cycle 3 times

2 2 2 23 3 3 34 4 4 11 11 45 5 5 56 6 6 67 7 7 7

Round 5:

ITunes 4

2 2 23 3 11 1 34 4 45 5 56 6 67 7 7

Round 6:

ITunes 5

2 11 23 34 45 56 67 7 1:function show () {var arr= (2) 4, 7, 5, 5, 3, 6, 1]; for (var iMago

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

Development

Wechat

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

12
Report