Example of javascript bubble sort

Example of javascript bubble sort

1. What is Bubble Sort

Bubble Sort compares the sizes of two adjacent elements in turn. In each comparison process, the two elements are exchanged to achieve the purpose of order.

If an unordered sequence of numbers needs to be sorted from small to large, then when two elements are compared, it can be achieved by exchanging them, and the element on the left must be smaller than the element on the right.

If an unordered sequence of numbers needs to be sorted from largest to smallest, then when two elements are compared, this can be achieved by exchanging them, so that the element on the left is larger than the element on the right.

Just like the bubbles in a carbonated drink, bubbling from the bottom all the way to the top.

2. Give an example

If there is a set of numbers 2,4,7,5,3,6,1

Round 1:

i=0;

j (inner loop) loops 6 times. The work done by the inner loop is: compare two adjacent numbers, the larger one will be placed at the end and the smaller one at the front. The outer loop controls the number of times in one loop, and the inner loop makes judgments

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]

Round 2:

i=1;

j (inner loop) loops 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]

Round 3:

i=2;

j (inner loop) loops 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

Round 4:

i=3;

j (inner loop) loops 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

Round 5:

i=4;

2 2 2
3 3 1
1 1 3
4 4 4
5 5 5
6 6 6
7 7 7

Round 6:

i=5;

twenty one
1 2
3 3
4 4
5 5
6 6
7 7
*/

<script type="text/javascript" >
// Example 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. Compare two adjacent numbers; the larger one is at the back, the smaller one is at the front if (arr[j] > arr[j+1] ) {
				var temp = arr[j];
				arr[j] = arr[j+1];
				arr[j+1] = temp;
			}
		}
	}
	console.log(arr);
}

// Example 2:
	<body>
	    <input type="text" id="test">
	    <button type="button" onclick="show()">Press me</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. Compare two adjacent numbers; the larger one is at the back, the smaller one is at the front 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>

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • JS front-end interview essential - basic sorting algorithm principles and implementation methods detailed explanation [insert/select/merge/bubble/quick sort]
  • JavaScript algorithm learning: bubble sort and selection sort
  • JavaScript data structure and algorithm basic sorting algorithm definition and efficiency comparison [bubble, selection, insertion sort]
  • Bubble sorting implemented in JavaScript and examples of counting the number of exchanges of adjacent numbers
  • Detailed explanation of quick sort and bubbling in JS
  • JavaScript implements the classic sorting algorithm bubble sort

<<:  HTML+css to create a simple progress bar

>>:  Javascript tree menu (11 items)

Recommend

Nginx tp3.2.3 404 problem solution

Recently I changed Apache to nginx. When I moved ...

Press Enter to automatically submit the form. Unexpected discovery

Copy code The code is as follows: <!DOCTYPE ht...

Centos7.5 installs mysql5.7.24 binary package deployment

1. Environmental preparation: Operating system: C...

Two ways to connect WeChat mini program to Tencent Maps

I've been writing a WeChat applet recently an...

JS achieves five-star praise effect

Use JS to implement object-oriented methods to ac...

CSS performance optimization - detailed explanation of will-change usage

will-change tells the browser what changes will h...

How to deploy redis in linux environment and install it in docker

Installation Steps 1. Install Redis Download the ...

Table related arrangement and Javascript operation table, tr, td

Table property settings that work well: Copy code ...

Problem analysis of using idea to build springboot initializer server

Problem Description Recently, when I was building...

Several methods of implementing two fixed columns and one adaptive column in CSS

This article introduces several methods of implem...

A brief discussion on logic extraction and field display of Vue3 in projects

Table of contents Logical Layering Separate busin...

The rel attribute of the HTML link tag

The <link> tag defines the relationship bet...

How to use positioning to center elements (web page layout tips)

How to center an element in the browser window He...

How to use custom tags in html

Custom tags can be used freely in XML files and HT...