Home Web Front-end JS Tutorial How to implement bubble sort

How to implement bubble sort

Jun 26, 2017 pm 03:15 PM
bubble how accomplish sort

 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>Title</title> 6 </head> 7  8 <body> 9 <script>10     var arr = [3,2,4,1,5];11 12     /*13     * 每次循环比较,取出当前的值和他的下一位进行大小的比较,如果当前值比下一个要大(小),交换位置,每次循环确定一个最大(最小)数14     *15     * 因为每次比较的数列中,最后一个没有下一位,所以最后一次没有必要再参与比较,所以每次循环比较的次数是要比较的数列元素个数-1次16     * */17     for (var i=0; i<arr.length-1; i++) {18 19         // 获取当前位的值20         var a = arr[i];21         // 获取下一位的值22         var b = arr[i+1];23 24         // 这里我们以小值在后,如果a小于b交换位置25         if (a < b) {26             arr[i] = b;27             arr[i+1] = a;28         }29 30     }31 32     // 进过上面的一轮循环,就确定这个数列中需要比较的值中最小的值33     console.log(arr);34 35 </script>36 </body>37 </html>
Copy after login
 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>Title</title> 6 </head> 7  8 <body> 9 <script>10 //    var arr = [3,2,4,1,5];11 12     var arr = [];13     for (var i=0; i<30000; i++) {14         arr.push(i);15     }16     arr.sort(function () {17         return Math.random() - 0.5;18     });19 20     /*21     * 每一轮的比较确定一个值,整个比较过程需要比较的次数是 长度-1,以为最后一轮的值,只有一个了,没有比较在比较了22     * */23 24     /*25     * 统计循环的总次数26     * */27     var n = 0;28 29     console.time(&#39;a&#39;);30     for ( var j=0; j<arr.length-1; j++ ) {31 32         for (var i=0; i<arr.length-1; i++) {33             var a = arr[i];34             var b = arr[i+1];35             if (a < b) {36                 arr[i] = b;37                 arr[i+1] = a;38             }39 40             n++;41         }42 43     }44     console.timeEnd(&#39;a&#39;);45 46     console.log(n);47     console.log(arr);48 49 </script>50 </body>51 </html>
Copy after login
 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>Title</title> 6 </head> 7  8 <body> 9 <script>10     //var arr = [3,2,4,1,5];11 12     var arr = [];13     for (var i=0; i<30000; i++) {14         arr.push(i);15     }16     arr.sort(function () {17         return Math.random() - 0.5;18     });19 20     var n = 0;21 22     console.time(&#39;a&#39;);23     for ( var j=0; j<arr.length-1; j++ ) {24 25         /*26         * 随着大的循环的次数的增加,对应的小的循环就应该减少,减少j次27         * */28         for (var i=0; i<arr.length-1-j; i++) {29             var a = arr[i];30             var b = arr[i+1];31             if (a < b) {32                 arr[i] = b;33                 arr[i+1] = a;34             }35 36             n++;37 38         }39 40     }41     console.timeEnd(&#39;a&#39;);42 43     console.log(n);44     console.log(arr);45 46 </script>47 </body>48 </html>
Copy after login





Title


< script>
// var arr = [3,2,4,1,5];
// var arr = [5,4,3,1,2];

var arr = [];
for (var i=0; i<30000; i++) {
arr.unshift(i);
}
arr[29999] = 1;
arr[ 29998] = 0;

// arr.sort(function () {
// return Math.random() - 0.5;
// });

var n = 0;

console.time('a');
for ( var j=0; j

/*
* As the number of large loops increases, the corresponding small loops should be reduced, reducing by j times
* */

/*
* Each time a smaller loop is made , set the flag to true, indicating that it has been sorted, assuming that the sorting is ok
  * */
  var flag = true;

for (var i=0; i var a = arr[i];
var b = arr[i+1];
if (a < * If the if condition is left during the comparison process, it means that the comparison is not completed and needs to be compared next time. Otherwise, the exchange of positions has not occurred, indicating that the data has been sorted
                                                             /*
            * As long as the if is left, indicating that there is an exchange, set the flag to false
                                                                                                                                                                   arr[i+ 1] = a;

}


n++;

}

/*
, it means that the above loop occurs without exchange, indicating that the sorting is ok. If it is false, it means that there has been exchange.

##}

console.timeEnd('a');

console.log(n);

console.log(arr);





The above is the detailed content of How to implement bubble sort. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to sort photos by date taken in Windows 11/10 How to sort photos by date taken in Windows 11/10 Feb 19, 2024 pm 08:45 PM

This article will introduce how to sort pictures according to shooting date in Windows 11/10, and also discuss what to do if Windows does not sort pictures by date. In Windows systems, organizing photos properly is crucial to making it easy to find image files. Users can manage folders containing photos based on different sorting methods such as date, size, and name. In addition, you can set ascending or descending order as needed to organize files more flexibly. How to Sort Photos by Date Taken in Windows 11/10 To sort photos by date taken in Windows, follow these steps: Open Pictures, Desktop, or any folder where you place photos In the Ribbon menu, click

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

PHP Programming Guide: Methods to Implement Fibonacci Sequence PHP Programming Guide: Methods to Implement Fibonacci Sequence Mar 20, 2024 pm 04:54 PM

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

How to sort WPS scores How to sort WPS scores Mar 20, 2024 am 11:28 AM

In our work, we often use wps software. There are many ways to process data in wps software, and the functions are also very powerful. We often use functions to find averages, summaries, etc. It can be said that as long as The methods that can be used for statistical data have been prepared for everyone in the WPS software library. Below we will introduce the steps of how to sort the scores in WPS. After reading this, you can learn from the experience. 1. First open the table that needs to be ranked. As shown below. 2. Then enter the formula =rank(B2, B2: B5, 0), and be sure to enter 0. As shown below. 3. After entering the formula, press the F4 key on the computer keyboard. This step is to change the relative reference into an absolute reference.

Master how Golang enables game development possibilities Master how Golang enables game development possibilities Mar 16, 2024 pm 12:57 PM

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide PHP Game Requirements Implementation Guide Mar 11, 2024 am 08:45 AM

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

How to implement exact division operation in Golang How to implement exact division operation in Golang Feb 20, 2024 pm 10:51 PM

Implementing exact division operations in Golang is a common need, especially in scenarios involving financial calculations or other scenarios that require high-precision calculations. Golang's built-in division operator "/" is calculated for floating point numbers, and sometimes there is a problem of precision loss. In order to solve this problem, we can use third-party libraries or custom functions to implement exact division operations. A common approach is to use the Rat type from the math/big package, which provides a representation of fractions and can be used to implement exact division operations.

See all articles