Get the App
SLTechnology News&Howtos  ›  Development  › 

Case Analysis of Bubble sorting in javascript

Shulou Source: shulou.com Published: 2022-06-01 13:17:57 10月03日 Update

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

Tags: Loop sort element inner layer two size array one round instance analysis analysis maximum number position content principle right sequence more maximum Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Docker Microsoft Shulou Information vpn