Skip to content

Commit 648abe9

Browse files
committedAug 8, 2014
added chapter 11
1 parent e7b7709 commit 648abe9

3 files changed

+162
-0
lines changed
 

‎chapter11/01-SearchingAlgorithms.js

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
function ArrayList(){
2+
3+
var array = [];
4+
5+
this.insert = function(item){
6+
array.push(item);
7+
};
8+
9+
this.toString = function(){
10+
var s = array[0] ? array[0] : '';
11+
for (var i=1; i<array.length; i++){
12+
s += ', ' + array[i];
13+
}
14+
return s;
15+
};
16+
17+
this.sequentialSearch = function(item){
18+
19+
for (var i=0; i<array.length; i++){
20+
if (item === array[i]){
21+
return i;
22+
}
23+
}
24+
25+
return -1;
26+
};
27+
28+
this.findMaxValue = function(){
29+
var max = array[0];
30+
for (var i=1; i<array.length; i++){
31+
if (max < array[i]){
32+
max = array[i];
33+
}
34+
}
35+
36+
return max;
37+
};
38+
39+
this.findMinValue = function(){
40+
var min = array[0];
41+
for (var i=1; i<array.length; i++){
42+
if (min > array[i]){
43+
min = array[i];
44+
}
45+
}
46+
47+
return min;
48+
};
49+
50+
this.binarySearch = function(item){
51+
this.quickSort();
52+
53+
var low = 0,
54+
high = array.length - 1,
55+
mid, element;
56+
57+
while (low <= high){
58+
mid = Math.floor((low + high) / 2);
59+
element = array[mid];
60+
if (element < item) {
61+
low = mid + 1;
62+
} else if (element > item) {
63+
high = mid - 1;
64+
} else {
65+
return mid;
66+
}
67+
}
68+
return -1;
69+
};
70+
71+
this.quickSort = function(){
72+
quick(array, 0, array.length - 1);
73+
};
74+
75+
var partition = function(array, left, right) {
76+
77+
var pivot = array[Math.floor((right + left) / 2)],
78+
i = left,
79+
j = right;
80+
81+
while (i <= j) {
82+
while (array[i] < pivot) {
83+
i++;
84+
}
85+
86+
while (array[j] > pivot) {
87+
j--;
88+
}
89+
90+
if (i <= j) {
91+
swapQuickStort(array, i, j);
92+
i++;
93+
j--;
94+
}
95+
}
96+
97+
return i;
98+
};
99+
100+
var swapQuickStort = function(array, index1, index2){
101+
var aux = array[index1];
102+
array[index1] = array[index2];
103+
array[index2] = aux;
104+
};
105+
106+
var quick = function(array, left, right){
107+
108+
var index;
109+
110+
if (array.length > 1) {
111+
112+
index = partition(array, left, right);
113+
114+
if (left < index - 1) {
115+
quick(array, left, index - 1);
116+
}
117+
118+
if (index < right) {
119+
quick(array, index, right);
120+
}
121+
}
122+
return array;
123+
};
124+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="01-SearchingAlgorithms.js"></script>
9+
<script type="text/javascript" src="02-UsingSearchingAlgorithms.js"></script>
10+
</body>
11+
</html>
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function createNonSortedArray(){
2+
var array = new ArrayList();
3+
4+
for (var i = 5; i> 0; i--){
5+
array.insert(i);
6+
}
7+
8+
return array;
9+
}
10+
11+
var array = createNonSortedArray();
12+
13+
console.log('********** Sequential Sort #3 **********');
14+
15+
console.log(array.sequentialSearch(3));
16+
17+
console.log('********** Min **********');
18+
19+
console.log(array.findMinValue());
20+
21+
console.log('********** Max **********');
22+
23+
console.log(array.findMaxValue());
24+
25+
console.log('********** Binary Search #3 **********');
26+
27+
console.log(array.binarySearch(3));

0 commit comments

Comments
 (0)
Please sign in to comment.