Home > Web Front-end > JS Tutorial > body text

Simple bubble and two-way bubble sort cases

php中世界最好的语言
Release: 2018-03-08 13:44:38
Original
1943 people have browsed it

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!