<H3> function: sort numeric array size <Script language = "JavaScript">
VaR array = [54, 23,38, 4,234, 9, 11];
Document. Write ("the original array is: [" + array. tostring () + "]" + "<br/> ");
// Calculate the maximum value:
VaR max = number. negative_infinity;
Document. Write ("negative infinity:" + MAX + "; Type:" + typeof MAX + "; <br/> ");
For (VAR I = 0, Len = array. length; I <Len; I ++ ){
If (array [I]> MAX ){
Max = array [I];
}
}
Document. Write ("the maximum value in the array is:" + MAX + "; <br/> ");
// Sort:
Function sortfn (inarray ){
VaR newarray = [],
Index,
M;
Document. Write ("the original array is:" + inarray + "<br/> ");
Do {
M = number. negative_infinity;
For (VAR I = 0, Len = inarray. length; I <Len; I ++ ){
If (inarray [I]> m ){
M = inarray [I];
}
}
Document. Write ("->" + M + "<br/> ");
Newarray. Push (m );
Document. Write ("The newarray array is:" + newarray. tostring () + "<br/> ");
Index = inarray. indexof (m );
Inarray. splice (index, 1 );
Document. Write ("The inarray array is:" + inarray. tostring () + "<br/> ");
} While (inarray. length! = 0 );
Return newarray. tostring ();
}
VaR result = sortfn (array). Split (",");
Document. Write ("in ascending order:" + Result + "<br/> ");
Document. Write ("in ascending order:" + result. Reverse () + "<br/> ");
// Bubble Sorting:
VaR secarray = [45, 23, 99, 11,233, 87];
Document. Write ("bubble comparison method: original array _" + secarray + "<br/> ");
VaR Len = secarray. length,
Empty;
For (var k = 0; k <len-1; k ++ ){
For (VAR I = 0; I <len-k-1; I ++ ){
If (secarray [I]> secarray [I + 1]) {
Empty = secarray [I + 1];
Secarray [I + 1] = secarray [I];
Secarray [I] = empty
}
}
}
Document. Write ("bubble comparison method: The result is (from small to large by default) _" + secarray + "<br/> ");
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