What does bubble sort mean in javascript
In JavaScript, bubble sorting is a sorting method. The principle is to compare each number in an array from front to back, and exchange positions according to size. Each round of comparison determines a When comparing the maximum value in the round, the size of the array is finally sorted.
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
What does bubble sorting mean in javascript
1. What is bubble sorting
Bubble sorting, Bubble Sort compares the sizes of two adjacent elements in sequence. During each comparison, the two elements are exchanged to achieve ordering.
If you want to sort a set of unordered arrays from small to large, then two elements are compared and implemented through exchange. The element on the left is smaller than the element on the right.
If a set of unordered arrays is to be sorted from large to small, then two elements are compared and implemented through exchange. The element on the left is larger than the element on the right.
Just like the bubbles in a carbonated drink, they bubble from the bottom to the top.
The principle is to compare each number in an array from front to back, and then exchange the positions according to the size. Each round of comparison determines the maximum value of the current round of comparison, and finally realizes the array sorted by size.
2. For example
If there is a set of numbers 2,4,7,5,3,6,1
First round:
i=0;
j (inner loop) loops 6 times. The inner loop does the work: compare two adjacent numbers, and the larger one ends up It will be placed at the back, with the small ones in front, one cycle
The outer loop controls the number of times, and the inner loop makes judgment
j=0 1 2 3 4 5
2 2 2 2 2 2 2 4 4 4 4 4 4 4 7 7 7 5 5 5 5 5 5 5 7 3 3 3 3 3 3 3 7 6 6 6 6 6 6 6 7 1 1 1 1 1 1 1 7 arr[0] arr[1] arr[2] arr[1] arr[2] arr[3]
Second round:
i=1;
j (inner loop) loop 5 times
j=0 1 2 3 4 5
2 2 2 2 2 2 4 4 4 4 4 4 5 5 5 3 3 3 3 3 3 5 5 5 6 6 6 6 6 1 1 1 1 1 1 6 7 7 7 7 7 7 arr[0] arr[1] arr[2] arr[1] arr[2] arr[3]
Third round:
i=2;
j (inner loop) loop 4 times
2 2 2 2 2 4 4 3 3 3 3 3 4 4 4 5 5 5 5 1 1 1 1 1 5 6 6 6 6 6 7 7 7 7 7
Fourth round:
i=3;
j (inner loop) loop 3 times
2 2 2 2 3 3 3 3 4 4 4 1 1 1 1 4 5 5 5 5 6 6 6 6 7 7 7 7
Fifth round:
i=4;
2 2 2 3 3 1 1 1 3 4 4 4 5 5 5 6 6 6 7 7 7
Sixth round:
i=5;
2 1 1 2 3 3 4 4 5 5 6 6 7 7
<script type="text/javascript" > // 示例1: function show(){ var arr=[2,4,7,5,3,6,1]; for(var i=0;i<arr.length-1;i++){ for(var j=0;j<arr.length-1-i;j++){ //1、比较相邻的两个数;大的在后,小的在前 if(arr[j] > arr[j+1] ){ var temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } console.log(arr); } // 示例2: <body> <input type="text" id="test"> <button type="button" onclick="show()">按我</button> <input type="text" id="sc"> </body> function show() { let oT=document.getElementById("test").value; let sc=document.getElementById("sc"); // console.log(sc); // console.log(oT); let arr=oT.split(""); console.log(arr.length); for (var i = 0; i < arr.length - 1; i++) { for (var j = 0; j < arr.length - 1 - i; j++) { //1、比较相邻的两个数;大的在后,小的在前 if (arr[j] > arr[j + 1]) { var temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } // console.log(arr); sc.value=arr; } </script>
Related recommendations: javascript learning tutorial
The above is the detailed content of What does bubble sort mean in javascript. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
