Simple understanding of js Bubble sorting and js Bubble Sorting

Source: Internet
Author: User
Tags keyword list rounds

Simple understanding of js Bubble sorting and js Bubble Sorting

Concerning Sorting, no matter which language actually has its built-in sorting function, we can call it when we use it. In this case, why should we talk about this? I think, in fact, sorting is more about the ideological algorithms included in sorting, because algorithms are very important to computers, A good algorithm can make the efficiency of computers get twice the result with half the effort. Therefore, algorithms are a very popular course in computer languages, the computer thinking represented by it is also worth further research.

I also know that many authors in my blog have written detailed explanations about the sorting of my titles, I personally think that my understanding can better reflect the working principle of this sort. Therefore, I will write an article about the bubble sort again here, readers who need it can take a look.

Before proceeding to the topic, I would like to introduce a very useful function of Google browser for debugging program code. If you already know, skip this step.

First, open the Google browser and enter our code script:

Right-click and choose check,

Click in order to obtain the script Running code:

My script is bubble.html, which is stored under the js/f directory under www.test.com domain name. The scripts for each person are different and the directories for storing are different. Please refer to your own situation.

After the script content is called up, the following is the debugging code.

After the breakpoint is selected, run the script again in Google browser, that is, run the following www.test.com/js/f/bubble.html and run it again.

After running it again, you will see something like this:

Do you see the blue rectangle above? The blue rectangle is the position where the script is running. How can we run the script code step by step?

Here, we perform breakpoint monitoring on the for loop code of this script. In fact, I want to see how the bubble sort is operated cyclically. for Beginners, in this way, you can intuitively view the running status of the bubble sort code to better understand the Algorithm Execution Process.

I will introduce this debugging feature here. Next I will go to the topic. If you cannot directly imagine the execution of the bubble sort, you can call up Google's debugging function following the steps above, intuitively view the entire process of Bubble sorting.

This article describes how two variables exchange each other. We use intermediate variables to exchange them:

// Interaction element. The code here is truncated from the script. This is so simple, it should not affect the understanding of if (arr [j]> arr [j + 1]). {mark = false; var temp = arr [j]; // temp is an intermediate variable. Assign the first element arr [j] to the intermediate variable, it is also used to store the first element. arr [j] = arr [j + 1]; // The second element is assigned to the first element, because the first element is already stored in the intermediate variable, we don't have to worry that its value will be overwritten by arr [j + 1] = temp; // temp stores the value of the first element and assigns it to the second element, that is, assigning the first element to the second element. In this step, the two elements have been switched}

Next, we will introduce the algorithm process of Bubble Sorting:

Bubble Sorting: Compares Adjacent Elements and exchanges positions to select an element step by step.

For example, sort the following Arrays:

This is an unordered array :,

Comparison rules: greater than>

First round:

First comparison: We compare 2 and 9: 2> 9? 2 and 9 are false, so we don't care about it, continue forward.

The second comparison: 9 and 4, 9> 4? Is it true, so if we exchange values and values, their positions will not change, right? How to exchange values has been discussed above, and we will not repeat it here.

After this exchange value, the original array has been changed to: 2, 4, 9, 8, 5, 1, 3, 6

Third comparison: Compare 9 and 8, 9> 8? True, then the two continue to exchange values. At this time, the array has become :,

......................

Have you seen the position 9 here? Does it move back step by step? For the first time, because 9 is big, it should not be switched with 2. Therefore, 9 is not put in front. For the second comparison, 9 is bigger than 4, according to the comparison rules, they should swap positions, that is, 9 shifted to the back one, the third comparison, still conforms to the comparison rules, so 9 and 8 swap positions, 9. I moved one step back. The comparison is the same as the comparison above. You can imagine it yourself. I will not repeat it here.

Compare to the last time: the original array should be :,

After the first round of comparison, we have put the largest element at the end, right?

Next, the second round:

In the second round, the original array, we compared the array that has been bubbling once. First confirm this. If you still think it is the original array, then, you will be confused about the following comparison. Haha.

Array at the moment: 2, 4, 8, 5, 1, 0, 7, 3, 6, 9

According to the comparison rules: 2> 4? Is false, regardless of it, continue the forward comparison.

4> 8? Is false, regardless of it, continue the forward comparison.

8> 5? Is true, the exchange value of the two, that is, the swap position, the array at the moment: 2, 4, 5, 8, 3, 6, 9

Continue forward, 8> 1? Is true, the two exchange positions, at the moment array: 2, 4, 5, 1, 8, 0, 7, 3, 6, 9

.......

At the end of the comparison, the original array has changed :,

After two rounds of comparison, has the original array become more ordered? Well, there is no error. After two rounds, the last two digits are already ordered.

Since the last two rounds are already in order, what about ten rounds later? Imagine it yourself.

After ten rounds, the array must have been sorted. This is the working process of the bubble sort. When the adjacent elements are compared, an ordered value is generated for each bubble.

So, how can we use code to Implement Bubble sorting?

Here, I think everyone should know how to do it?

We use a two-layer nested for loop to implement this process, that is, to Implement Bubble Sorting:

// Number of outer control wheels for (var I = 0; I <len; I ++) {// The inner layer bubbles the array elements and selects for (var j = 0; j <len-1-i; j ++) {// interaction element if (arr [j]> arr [j + 1]) {var temp = arr [j]; arr [j] = arr [j + 1]; arr [j + 1] = temp ;}}}

Have you seen the two nested for loops above? The outer for loop is used to control the comparison,

Inner for loop, we use it to control the number of comparisons in each round ·············, what should we do in this for loop? Have you understood the above description? In the text description above, do we have to exchange the positions of array elements according to the comparison rules during every comparison? Then, the procedure is the same. here we need to exchange positions for the elements of the array according to the comparison rules, in order to bubble out the elements we need.

The following is the complete code of the bubble sort. I have optimized it. Of course, if you can optimize it, you can continue to optimize it.

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

Here is just a brief introduction of the working principle of Bubble sorting. If there is time, I will explain in detail the other three sorting, fast sorting, select sorting, and insert sorting.

In fact, the working principles of these three sorting are similar to those of Bubble sorting. There are also many articles on the Internet, so you can study them on your own.

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.