What does bubble sort mean in javascript

WBOY
Release: 2022-02-21 16:39:22
Original
1693 people have browsed it

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.

What does bubble sort mean in javascript

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]
Copy after login

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]
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

Sixth round:

i=5;

2 1
1 2
3 3
4 4
5 5
6 6
7 7
Copy after login
<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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template