<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>sort</title>
</head>
<body>
<script>
var shu=[5,2,3,1 ,9];
//Method 1: use bubble sort to achieve
/*
The first step: put the maximum number at the end;
analyze the three elements of the loop:
1. Loop condition: when the last element is obtained, it will not be compared;
2 , loop variable: subscript i starts from 0;
3. Loop body: the current element is greater than the next element, swap
// put the maximum number at the end
for(var i=0;i<shu.length-1;i++) {
if(shu[i]>shu[i+1]){
var box=shu[i];//Take out the data of i
shu[i]=shu[i+1];//Remove the data of i+1 Put the data into i;
shu[i+1]=box;//Put the retrieved data into i+1;
}
}
//Compare to find the second largest number
for(var i=0;i<shu.length- 2; i++){
if(shu[i]>shu[i+1]){
var box=shu[i];//Take out the data of i
shu[i]=shu[i+1];//Put the data of i+1 into i;
shu[i+1]=box;// Put the extracted data into i+1;
}
} //...the
nth comparison, find the maximum number of remaining elements
for(var i=0;i<shu.length-n;i++){
if (shu[i]>shu[i+1]){
var box=shu[i];//Take out the data of i
shu[i]=shu[i+1];//Put the data of i+1 Enter i;
shu[i+1]=box;//Put the retrieved data into i+1;
}
}
*/ //Get the rule: /* for(var n=1;n<shu.length-1 ;n++){ for(var i=0;i<shu.length-1;i++){ if(shu[i]>shu[i+1]){ var box=shu[i];//Set the value of i Data extraction shu[i]=shu[i+1];//Put the data of i+1 into i; shu[i+1]=box;//Put the extracted data into i+1; } } }
document.write(shu);
*/ //Method 2: Use the comparator and sort() method; function compare(a,b){ return ab;//Ab in ascending order, ba in reverse order; } document.write(shu .sort(compare)); </script> </body> </html>
<html>
<head>
<meta charset="utf-8">
<title>sort</title>
</head>
<body>
<script>
var shu=[5,2,3,1 ,9];
//Method 1: use bubble sort to achieve
/*
The first step: put the maximum number at the end;
analyze the three elements of the loop:
1. Loop condition: when the last element is obtained, it will not be compared;
2 , loop variable: subscript i starts from 0;
3. Loop body: the current element is greater than the next element, swap
// put the maximum number at the end
for(var i=0;i<shu.length-1;i++) {
if(shu[i]>shu[i+1]){
var box=shu[i];//Take out the data of i
shu[i]=shu[i+1];//Remove the data of i+1 Put the data into i;
shu[i+1]=box;//Put the retrieved data into i+1;
}
}
//Compare to find the second largest number
for(var i=0;i<shu.length- 2; i++){
if(shu[i]>shu[i+1]){
var box=shu[i];//Take out the data of i
shu[i]=shu[i+1];//Put the data of i+1 into i;
shu[i+1]=box;// Put the extracted data into i+1;
}
} //...the
nth comparison, find the maximum number of remaining elements
for(var i=0;i<shu.length-n;i++){
if (shu[i]>shu[i+1]){
var box=shu[i];//Take out the data of i
shu[i]=shu[i+1];//Put the data of i+1 Enter i;
shu[i+1]=box;//Put the retrieved data into i+1;
}
}
*/ //Get the rule: /* for(var n=1;n<shu.length-1 ;n++){ for(var i=0;i<shu.length-1;i++){ if(shu[i]>shu[i+1]){ var box=shu[i];//Set the value of i Data extraction shu[i]=shu[i+1];//Put the data of i+1 into i; shu[i+1]=box;//Put the extracted data into i+1; } } }
document.write(shu);
*/ //Method 2: Use the comparator and sort() method; function compare(a,b){ return ab;//Ab in ascending order, ba in reverse order; } document.write(shu .sort(compare)); </script> </body> </html>