"14 Ways to find the maximum value of an array element" for "practice knowledge points and summaries"

Source: Internet
Author: User

The following is the result of painstaking efforts, copyright, without permission, may not be reproduced. Li Jintao; From: Aura International at:201712172024. (Easy to change again later)

<script>

Comparing the size of the values in the array is a common operation, there are several ways to compare size, the following 14 methods are described below, the principle code is as follows:

///1, Sort value: Sort comparator (common), and bubble sort (not commonly used, but method idea is important);
///2, assuming a larger value: suppose max=arr[0];index=0; if there is a larger number than Max, let Max record the large number, the index is assigned to index,
cycle done, that is, to get the condition of Max and index;
///3, bubble sort idea, inside and outside loop take big value: bubble sort idea, inside and outside circulation, three mesh compare take big value, return contrast, match can get corresponding index;
//4, exclusion method: the latter two comparisons, the smaller throw out, otherwise the exchange, while the loop ends, the remaining one is the maximum value, and finally return the comparison, matching the available corresponding index;
///5, Comparison of the sorting method: Before and after, if the former large exchange, traverse the end is a small large sort, receive the last one.
//6, Delete method: Each other, delete small, the rest is the largest;
/ * 7, use stream: Put an array inside the stream, and then call the Min or Max function directly in the stream to get the maximum or minimum value.

8, using collection: Converts an array into an array of objects, that is, int to an Integer (requires the use of toobject conversion). Then call the Min or Max inside the Collection.

9, the use of the recursive idea is: if the length of the array to the maximum value of L, that is, the L-1 length of the array before the maximum value and the last array element of the relatively large value.
The essence of recursion is the constant invocation of itself, which is calculated from the backward processing of the data. The maximum value of the array is also compared from backward to forward.

10, the maximum value in the array is searched recursively using the binary lookup method.
11,12,13,14, function method, see the last code;
*/
var arr1=[12,3,4,56,7,7];
function F1 () {//sort comparer sort
var arr=[12,3,4,56,7,7];
arr.sort (COM);
For (var i in arr1) {
if (Arr[arr.length-1]==arr1[i]) {
var index=i;
}
}
txt1.value= "Max:" +arr[arr.length-1]+ ", Index:" +index;
}
function com (A, b) {
return a-B;
}
///Two, suppose the comparison idea: suppose max=arr[0];index=0; if there is a larger number than Max, let Max record the large number,
indexes are assigned to index, and the loop is done, that is, Max and Index are satisfied; (if you make a new value, you are small)
function F2 () {
var arr=[12,3,4,56,7,7];
var max=arr[0];
var index=0;
For (Var i=1;i<arr.length;i++) {
if (Max<arr[i]) {
Max=arr[i];
index=i;
}
}
txt2.value= "Max:" +max+ ", Index:" +index;
}

//3, bubble sort thought, internal and external circulation, three-mesh comparison take large value, return the comparison, matching can get the corresponding index;
function F3 () {
var arr=[12,3,4,56,7,7];
For (var i=1;i<arr.length-1;i++) {///22 Compare large values
For (Var j=i;j<arr.length-1;j++) {
var max=arr[i-1]>arr[j]?arr[i-1]:arr[j];
}
}
For (var k in arr1) {
if (Max==arr1[k]) {
var index=k;
}
}
txt3.value= "Max:" +max+ ", Index:" +index;
}

///Two, bubble sort (22 comparison) thought: 1, the sort takes the large value and the index;
//2, after two comparisons, the smaller throw out, otherwise swap, while the loop ends, the remaining one is the maximum value, and finally returns the comparison, matching the available corresponding index;
function F4 () {
var arr=[12,3,4,56,7,7];
While (arr.length>1) {
if (Arr[arr.length-1]<=arr[arr.length-2]) {
Arr.pop ();
}else {
Arr[arr.length-2]=arr[arr.length-1];
var temp=arr[arr.length-2];
arr[arr.length-1]=temp;
Arr.pop ();
}
}
//Console.log (arr);
var arr1=[12,3,4,56,7,7];
var max=arr[0];
var index=getmax_index (Arr1,max);//Package method: Returns the comparison, matching the available corresponding index; The method is behind.
txt4.value= "Max:" +arr[0]+ ", Index:" +index;

}
//traversal before and after, if the former large on the exchange, traversing is a small large sort, receive the last one.
function f5 () {
var arr=[12,3,4,56,7,7];
var arr1=[12,3,4,56,7,7];
For (Var i=0;i<arr.length-1;i++) {
if (arr[i]>arr[i+1]) {
var temp=arr[i+1];
Arr[i+1]=arr[i];
arr[i]=temp;
}
}
var max=arr.pop ();
var index=getmax_index (Arr1,max);
txt5.value= "Max:" +max+ ", Index:" +index;
}

//Mutual ratio, delete small, the rest is the largest;
function f6 () {
var arr=[12,3,4,56,7,7];
var arr1=[12,3,4,56,7,7];
while (arr.length>1) {//to each other, remove the small
For (Var j=0;j<arr.length-1;j++) {
if (arr[j]<=arr[j+1]) {
Arr.splice (j,1);
}else {
Arr.splice (j+1,1);
}
}
}
var max=arr[0];
var index=getmax_index (Arr1,max);
txt6.value= "Max:" +max+ ", Index:" +index;
}
function Getmax_index (arr,max) {
For (var k in arr) {
if (Max==arr[k]) {
var index=k;
}
}
return index;
}

/*
Other methods:

Method 11:
//min value
Array.prototype.min = function () {
var min = this[0];
var len = this.length;
For (var i = 1; i < Len; i++) {
if (This[i] < min) {
min = this[i];
}
}
return min;
}
//Maximum value
Array.prototype.max = function () {
var max = this[0];
var len = this.length;
For (var i = 1; i < Len; i++) {
if (This[i] > max) {
max = this[i];
}
}
return max;
}
If you are introducing a class library for development, it is feared that the class library also implements a prototype method with the same name, which can be judged before the function is generated:


if (typeof array.prototype[' max '] = = ' undefined ') {
Array.prototype.max = function () {
... ...
}
}


Method 12:

results can be obtained quickly using the Math.max and Math.min methods. Apply allows a method to specify the calling object and incoming parameters, and the incoming parameters are organized in an array form. Exactly now there is a method called Math.max, which invokes the object as math, with multiple arguments


Array.max = function (Array) {
return Math.max.apply (Math, array);
};
array.min = function (Array) {
return Math.min.apply (Math, array);
};
However, John Resig is a static method that makes them into math objects, and cannot use the great God's favorite chained invocation. But this method can be more streamlined, do not forget that the Math object is also an object, we use the object's literal to write, and can save a few bits.


Array.prototype.max = function () {
return Math.max.apply ({},this)
}
Array.prototype.min = function () {
return Math.min.apply ({},this)
}
[1,2,3].max ()//= 3
[1,2,3].min ()//= 1


Method 13:


function Getmaximin (arr,maximin)
{
if (maximin== "Max")
{
return Math.max.apply (Math,arr);
}
else if (maximin== "min")
{
return Math.min.apply (Math, arr);
}
}
var a=[3,2,4,2,10];
var b=[12,4,45,786,9,78];
Console.log (Getmaximin (A, "Max"));//10
Console.log (getmaximin (b, "Min"));//04
Method 14:


var a=[1,2,3,5];
alert (Math.max.apply (null, a));//Maximum Value
alert (Math.min.apply (null, a));//Minimum Value
multidimensional arrays can be modified like this:


var a=[1,2,3,[5,6],[1,4,8]];
var ta=a.join (","). Split (",");//convert to one-dimensional array
alert (Math.max.apply (Null,ta));//Maximum Value
alert (Math.min.apply (Null,ta));//Minimum value */


</script>

<body>
<p>var arr=[12,3,4,56,7,7]
The largest number and index in an array in the output: various ways to achieve </p>
<input type= "button" value= "Method One" onclick= "F1 ()"/><input type= "text" id= "Txt1"/><br/>
<input type= "button" value= "Method Two" onclick= "F2 ()"/><input type= "text" id= "txt2"/><br/>
<input type= "button" value= "method three" onclick= "F3 ()"/><input type= "text" id= "Txt3"/><br/>
<input type= "button" value= "method four" onclick= "F4 ()"/><input type= "text" id= "TXT4"/><br/>
<input type= "button" value= "Method Five" onclick= "F5 ()"/><input type= "text" id= "TXT5"/><br/>
<input type= "button" value= "method Six" onclick= "F6 ()"/><input type= "text" id= "Txt6"/><br/>
<input type= "button" value= "Method seven" onclick= "F7 ()"/><input type= "text" id= "Txt7"/><br/>
<input type= "button" value= "Method eight" onclick= "F8 ()"/><input type= "text" id= "Txt8"/><br/>
</body>

"14 Ways to find the maximum value of an array element" for "practice knowledge points and summaries"

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.