-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharray.o.js
103 lines (94 loc) · 2.14 KB
/
array.o.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
Array.randomSelect = function(){
return this[Math.randomNumberRange(0,this.length-1)];
};
Array.searchHaystack = function(needle){
for(var x=0;x<this.length;x++){
if(needle == this[x]){
return x;
}
}
return false;
};
Array.bubbleSort = function(){
var swapped;
do{
swapped = false;
for (var i=0; i < this.length-1; i++) {
if (this[i] > this[i+1]) {
var temp = this[i];
this[i] = this[i+1];
this[i+1] = temp;
swapped = true;
}
}
}while(swapped);
}
/*
Array.quickSort = function(){
var stack = [this];
var sorted = [];
while (stack.length) {
var temp = stack.pop(), tl = temp.length;
if (tl == 1) {
sorted.push(temp[0]);
continue;
}
var pivot = temp[0];
var left = [], right = [];
for (var i = 1; i < tl; i++) {
if (temp[i] < pivot) {
left.push(temp[i]);
} else {
right.push(temp[i]);
}
}
left.push(pivot);
if (right.length)
stack.push(right);
if (left.length)
stack.push(left);
}
this=sorted;
}
*/
/*
Array.mergeSort = function(){
if (this.length < 2)
return this;
var middle = parseInt(this.length / 2);
var left = this.slice(0, middle);
var right = this.slice(middle, this.length);
this = this.merge(this.mergeSort(left), this.mergeSort(right));
return this;
}
Array.merge = function(left, right){
var result = [];
while (left.length && right.length) {
if (left[0] <= right[0]) {
result.push(left.shift());
} else {
result.push(right.shift());
}
}
while (left.length)
result.push(left.shift());
while (right.length)
result.push(right.shift());
return result;
}
*/
Array.prototype.clear = function(){
while(this.length>0){
this.pop();
}
return 0;
};
Array.prototype.move = function (old_index, new_index) {
if (new_index >= this.length) {
var k = new_index - this.length;
while ((k--) + 1) {
this.push(undefined);
}
}
this.splice(new_index, 0, this.splice(old_index, 1)[0]);
};