Home Web Front-end JS Tutorial Simple bubble and two-way bubble sort cases

Simple bubble and two-way bubble sort cases

Mar 08, 2018 pm 01:44 PM
bubble

This time I will bring you a simple bubble and two-way bubble sort case. What are the precautions for using bubble sequence? , the following is a practical case, let’s take a look.

Bubble sorting example, two-way bubble sorting and a slightly improved visualization of two-way bubble sorting.

Bubble sorting example

var ls=[ 98,13,6,25,38,36,30,44,38,80,61,28,47,34,95,18,85,58,89,85,42,61,74,35,13,14,80,7,10,44,10,47,13,11,52,25,24,48,34,12,88,80,33,8,80,45,64,52,79,77 ];
 
for(var i=0;i<ls.length;i++){
            for(var j=i+1;j<ls.length;j++){
                if(ls[i]>ls[j]){
                    ls[i]=ls[i]+ls[j];
                    ls[j]=ls[i]-ls[j];
                    ls[i]=ls[i]-ls[j];
                }
            }
        }
Copy after login

Two-way bubble sorting example

var ls=[ 6,13,98,25,38,36,30,44,38,80,61,28,47,34,95,18,85,58,89,85,42,61,74,35,13,14,80,7,10,44,10,47,13,11,52,25,24,48,34,12,88,80,33,8,80,45,64,52,79,77 ];
 
for(var i=0;i<ls.length;i++){
            for(var j=i+1;j<ls.length-i;j++){
                if(ls[lent-1-i]<ls[lent-j]){
                    ls[lent-1-i]=ls[lent-1-i]+ls[lent-j];
                    ls[lent-j]=ls[lent-1-i]-ls[lent-j];
                    ls[lent-1-i]=ls[lent-1-i]-ls[lent-j];
                }//后面的比较
                if(ls[i]>ls[j]){
                    ls[i]=ls[i]+ls[j];
                    ls[j]=ls[i]-ls[j];
                    ls[i]=ls[i]-ls[j];
                }//前面的比较
            }
        }
Copy after login

Two-way bubble sorting slightly improved example

var ls=[ 98,13,6,25,38,36,30,44,38,80,61,28,47,34,95,18,85,58,89,85,42,61,74,35,13,14,80,7,10,44,10,47,13,11,52,25,24,48,34,12,88,80,33,8,80,45,64,52,79,77 ];
 
var lent=ls.length;
for(var i=0;i<ls.length;i++){
            for(var j=i*2;j<ls.length-2*i;j++){
                if(ls[i*2]>ls[j+1]){
                    ls[i*2]=ls[i*2]+ls[j+1];
                    ls[j+1]=ls[i*2]-ls[j+1];
                    ls[i*2]=ls[i*2]-ls[j+1];
                }//保持内层第一个数为循环最小
                 
                if(ls[lent-i*2-1]<ls[lent-j-1]){
                    ls[lent-i*2-1]=ls[lent-i*2-1]+ls[lent-j-1];
                    ls[lent-j-1]=ls[lent-i*2-1]-ls[lent-j-1];
                    ls[lent-i*2-1]=ls[lent-i*2-1]-ls[lent-j-1];
                }////保持内层倒数第一个数为循环最大
                     
                    if(ls[lent-2-i*2]<ls[lent-j-1]){
                        ls[lent-2-i*2]=ls[lent-2-i*2]+ls[lent-j-1];
                        ls[lent-j-1]=ls[lent-2-i*2]-ls[lent-j-1];
                        ls[lent-2-i*2]=ls[lent-2-i*2]-ls[lent-j-1];
                    }//倒数上一个
                     
                    if(ls[i*2+1]>ls[j+1]){
                        ls[i*2+1]=ls[i*2+1]+ls[j+1];
                        ls[j+1]=ls[i*2+1]-ls[j+1];
                        ls[i*2+1]=ls[i*2+1]-ls[j+1];
                    }//下一个
            }
}
Copy after login

I believe you have read this article You have mastered the case method. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Related reading:

How to quickly save the content in a web page and remove unwanted elements

Simple image click Upload function

The above is the detailed content of Simple bubble and two-way bubble sort cases. 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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
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)

Master the common event bubbling mechanism in JavaScript Master the common event bubbling mechanism in JavaScript Feb 19, 2024 pm 04:43 PM

Common bubbling events in JavaScript: To master the bubbling characteristics of common events, specific code examples are required. Introduction: In JavaScript, event bubbling means that the event will start from the element with the deepest nesting level and propagate to the outer element until it propagates to The outermost parent element. Understanding and mastering common bubbling events can help us better handle user interaction and event handling. This article will introduce some common bubbling events and provide specific code examples to help readers better understand. 1. Click event (click

Which JS events are not propagated upward? Which JS events are not propagated upward? Feb 19, 2024 am 08:17 AM

Which JS events will not bubble? In JavaScript, event bubbling means that when an element triggers an event, the event will bubble up to higher-level elements until it bubbles to the document root node. The event handlers are then executed in the order they bubble up. However, not all events bubble up. Some events will only execute the event handler on the target element after being triggered, without bubbling up to higher-level elements. Here are some common events that do not bubble: focus and blur events:

Why does the same event trigger bubbling twice? Why does the same event trigger bubbling twice? Feb 19, 2024 pm 10:34 PM

Why does the same bubbling event happen twice? Event bubbling is a common event delivery mechanism in browsers. When an element triggers an event, the event will be passed from the triggered element to the upper elements in sequence until it is passed to the root element of the document. This process is like bubbles bubbling in water, so it is called event bubbling. However, sometimes we find that the same bubbling event occurs twice. Why is this? There are two main reasons: event registration and event processing. First, we need to make it clear that the event

Capture first or bubble first? Analyze the advantages and disadvantages of event processes Capture first or bubble first? Analyze the advantages and disadvantages of event processes Feb 21, 2024 pm 02:36 PM

Capture first or bubble first? Analyzing the advantages and disadvantages of event process Event process is an important concept in web development. It describes the process of events from occurrence to processing. There are two main process models when handling events: capture then bubble and bubble then capture. These two models have their own advantages and disadvantages in different scenarios, and you need to choose the appropriate model based on the actual situation. Capturing first and then bubbling means that the event capturing phase is executed before the event bubbling phase. The event capture phase starts from the root node of the event target and passes down step by step until it reaches the target element.

Explore click event bubbling and master the key principles of front-end development Explore click event bubbling and master the key principles of front-end development Jan 13, 2024 am 10:56 AM

Learn click event bubbling and master key concepts in front-end development. Specific code examples are required. Front-end development is an important field in today's Internet era, and event bubbling is one of the key concepts in front-end development. Understanding and mastering event bubbling is critical to writing efficient front-end code. This article will introduce what event bubbling is and how to use the concept of event bubbling in front-end development. 1. What is event bubbling? Event bubbling means that when an event on an element is triggered, it will start from the innermost element first, and then proceed to the parent element step by step.

Learn event bubbling to easily achieve complex interactive effects Learn event bubbling to easily achieve complex interactive effects Jan 13, 2024 am 08:01 AM

Master event bubbling and easily achieve complex interactive effects. Specific code examples are required. Event bubbling (Event Bubbling) is an important concept in front-end development. It means that when an event on an element is triggered, the event will automatically report to the parent. level elements until it reaches the document root element. By mastering the principles and applications of event bubbling, we can easily implement complex interactive effects and improve user experience. The following will use specific code examples to help readers better understand and apply event bubbling. Code example 1: Click to expand

The bubbling mechanism of click events and its impact on web page interaction The bubbling mechanism of click events and its impact on web page interaction Jan 13, 2024 pm 02:34 PM

The role of click event bubbling and its impact on web page interaction In web development, events are the key to realizing interaction and responding to user operations. Among them, event bubbling is a common event mechanism that allows events in a nested element hierarchy to be responded to by multiple elements at the same time. This article will explain in detail the role of click event bubbling, its impact on web page interaction, and provide some specific code examples. 1. The concept of click event bubbling Click event bubbling (ClickEvent Bubbling) refers to when an element

How to enhance web page interaction experience by utilizing click event bubbling How to enhance web page interaction experience by utilizing click event bubbling Jan 13, 2024 pm 02:23 PM

How to use click event bubbling to achieve a more flexible web page interaction experience Introduction: In front-end development, we often encounter situations where we need to add click events to some elements of the web page. However, if there are many elements in the page, adding click events to each element will become very tedious and inefficient. Click event bubbling can help us solve this problem by adding click events to public parent elements to achieve a more flexible web page interaction experience. 1. The principle of click event bubbling. Click event bubbling refers to when a click event on an element is triggered.

See all articles