Good web programmer and sorted array share the front end, to weight and randomly named, a stack structure the stack: the stack in sequence, a data item is a data structure, only one end (referred to as a top of the stack (Top)) of data items insert and delete.
Stack: path is stored; limited capacity (after initially definition is not changed); last-out ( FILO ) First the In
Last Out
Heap: mapping data path; infinite capacity (as might have been changed); priority queue, FIFO ( the FIFO ) First the In Last Out
If we understand the array as a stack heap structure, then
was arr = [1,2,3,4,5,6];
So arr stack is that he is a path: find the data storage layer;
Data [1,2,3,4,5,6] is the stack;
If we want to change the data heap, then we can use the push () method to add data to a tail, the tail or delete data pop () method.
Array:
for-in (binding array or object);
Array concept, a group (the same general type) of the data;
Concept of index (index);
Creating an array
var arr = new Array (10); // create an array of 10 elements comprising;
var arr2 = new Array ( 'Yang Huaizhi', 100 'teacher', 'Liaoning'); // use constructor to create;
was arr3 = [1,2,3,4,5];
Static Assignment
arr[0] = 5;
arr[1] = 12;
arr[2] = 4;
Dynamic assignment
for(var i=0; i<5; i++){
arr[i] = Math.random();
}
The property comes with an array of objects: length array represents the number of data items;
Traversing the array access for general circulation and forEach loop
arr.forEach(function(element,index,Arr){
})
push/pop、unshift/shift方法
arr.push ( 'BJ'); // array element added at the end
arr.unshift ( 'TJ'); // Prepends a array element
var str = arr.pop (); // at the end of a removable element, and returns the element
var str = arr.shift (); // beginning of a removable element, and returns the element
Queue structure:
Exercise: to find among all the 1-100 numbers contain a multiple of 7 and 7
Reverse reverse method;
arr.reverse () method does not create a new array, but the array of original order was changed;
was arr = [1,2,3]
arr.reverse();
console.log(arr)//3,2,1
From small to large sort method (digital ordering)
Bubble sort algorithm:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script>
var arr = [2, 333, 1, 4, 6, 5, 7, 8];
for (var i = 0; i < arr.length - 1; i++) {
for (var j = 0; j < arr.length - i; j++) {
var ls;
if (arr[j] > arr[j + 1]) {
ls = arr[j];
arr[j] = arr[j + 1]
arr[j + 1] = ls
}
}
}
console.log(arr)
</script>
</head>
<body>
</body>
</html>复制代码
Select sorting algorithm:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script>
var arr = [3, 4, 1, 5, 2]
for (var i = 0; i < arr.length; i++) { //趟数
var min = arr[i];
var index = i;
for (var j = i; j < arr.length; j++) {
if (min > arr[j]) {
min = arr[j];
index = j;
}
}
arr[index] = arr[i];
arr[i] = min;
}
document.write(arr);
</script>
</head>
<body>
</body>
</html>复制代码
Case: Deduplication
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script>
var arr = [11, 2, 33, 4, 2, 11, 3, 44, 33, 2]
arr.sort();
var arr2 = [arr[0]]
for (var i = 0; i < arr.length; i++) {
if (arr[i] != arr2[arr2.length - 1]) {
arr2.push(arr[i]);
}
}
alert(arr2)
</script>
</head>
<body>
</body>
</html>复制代码
Randomly named program:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script>
document.onclick = function() {
var aName = ['杨怀智1', '杨怀智3', '杨怀智4']
var rad = Math.round(Math.random() * 2);
document.write(aName[rad])
}
</script>
</head>
<body>
随机点名
</body>
</html>复制代码