Arrays: Collections of data of the same type
Strongly typed language: 1, the array can only hold data of the same data type.
2, you need to define the length of the array (can hold the number of elements)
Collection: 1, can store any type of data,
2, the definition of time does not need to set the length
3, memory space is not contiguous
Js
Array: 1, which can hold any type of data.
2, the definition of time does not need to set the length
Defines an array of var attr = array (1,3.14, "AA"); The data in the array is arranged in order 0 1 2 3 4 ...
Each data in the middle is separated by commas,
Var attr= attr (5);
Var attr=[1,3.14, "AA"] defines the output directly with square brackets.
Alert (attr[2]);
Property
length is the array length,
Alert (attr.length); Output array length,
Push Append Element
Attr.push ("BB"); is to add BB to the attr number set,
Array traversal
for (Var i=0;i<attr.length;i++)
{
Alert (Attr[i]);
}
foreach () Way Traversal a is an index.
For (Var a in attr)
{
Alert (Attr[a]);
}
Example
Ten scores, total score, highest score, lowest score
var attr =[89,80,76,49,90,25,85,76,59,40];
var sum = 0;
for (Var i=0;i<attr.length;i++)
{
Sum + = Attr[i];
}
alert (sum); Sum of results
var ZG = 0;
for (Var i=0;i<attr.length;i++)
{
if (ATTR[I]>ZG)
{
ZG = Attr[i];
}
}
alert (ZG); highest score
var zd = ZG;
for (Var i=0;i<attr.length;i++)
{
if (ATTR[I]<ZD)
{
ZD = Attr[i];
}
}
alert (ZD); Minimum score.
Add to Weight
var attr = [2,5,10,16,27];
var sj = 27;
var CF = true;
for (var i=0; i<attr.length;i++)
{
if (SJ = = Attr[i])
{
CF = false;
Break
}
}
if (CF)
{
Attr.push (SJ);
alert (attr.length);
}
Else
{
Alert ("With duplicate value");
}
var attr = [1,8,26,4,15,3,7,42,9];
sorting An array element
Bubble sort
8 6 4 5 3 7 2 9 1 8
8 6 5 4 7 3 9 2 1 7
8 6 5 7 4 9 3 2 1 6
8 6 7 5 9 4 3 2 1 5
8 7 6 9 5 4 3 2 1 4
8 7 9 6 5 4 3 2 1 3
8 9 7 6 5 4 3 2 1 2
9 8 7 6 5 4 3 2 1 1
Two adjacent elements are compared to meet the conditional element interchange
The number of wheels to compare is the length of the array minus one
Define an intermediate variable used by an interchange
var zj = 0;
Control the number of wheels to compare
for (Var i=0;i<attr.length-1;i++)
{
Control the number of comparisons per round
for (Var j=0;j<attr.length-1-i;j++)
{
If the next element is greater than the current element
if (attr[j]<attr[j+1])
{
ZJ = Attr[j]; Swap
ATTR[J] = attr[j+1];
ATTR[J+1] = ZJ;
}
}
}
Alert (attr[0]);
Attr.sort ();
Alert (attr[3]);
Find data inside an array
var attr = [1,2,3,4,5,6,7,8,9];
The value to find
var v = 0;
How loops are traversed
var sy =-1;
for (Var i=0;i<attr.length;i++)
{
if (ATTR[I]==V)
{
sy = i;
}
}
if (sy = =-1)
{
Alert ("No data Found");
}
Else
{
Alert ("The data is indexed inside the array:" +sy);
}
Finding data by dichotomy
Minimum index
var minsy = 0;
var maxsy = attr.length-1;
var Midsy;
Cycle comparison
while (true)
{
Calculate Intermediate Index
Midsy = parseint ((minsy+maxsy)/2);
Compare intermediate values and user values
Determines whether the value of the intermediate index equals the value that the user is looking for
if (attr[midsy] = = v)
{
If it's equal, exit the loop and find the data
Break
}
Determine if there are only two of data left
if (Midsy = = Minsy)
{
Determine if another two data is equal to the value that the user is looking for
if (ATTR[MIDSY+1]==V)
{
The value is found, exiting the loop
Midsy = midsy+1;
Break
}
Else
{
No value found, exit loop
Midsy =-1;
Break
}
}
To change the range.
if (ATTR[MIDSY]>V)
{
Maxsy = Midsy;
}
Else
{
Minsy = Midsy;
}
}
alert (MIDSY);
9.19 array bubbling sorting and dichotomy