Skip to content

Commit e9cd528

Browse files
committed
added chapter 10
1 parent b99f882 commit e9cd528

6 files changed

+73
-140
lines changed

chapter10/01-SortingAlgorithms.js chapter10/01-SortingSearchingAlgorithms.js

+61
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function ArrayList(){
2424
var length = array.length;
2525

2626
for (var i=0; i<length; i++){
27+
console.log('--- ');
2728
for (var j=0; j<length-1; j++ ){
2829
console.log('compare ' + array[j] + ' with ' + array[j+1]);
2930
if (array[j] > array[j+1]){
@@ -38,6 +39,7 @@ function ArrayList(){
3839
var length = array.length;
3940

4041
for (var i=0; i<length; i++){
42+
console.log('--- ');
4143
for (var j=0; j<length-1-i; j++ ){
4244
console.log('compare ' + array[j] + ' with ' + array[j+1]);
4345
if (array[j] > array[j+1]){
@@ -191,4 +193,63 @@ function ArrayList(){
191193
}
192194
return array;
193195
};
196+
197+
this.sequentialSearch = function(item){
198+
199+
for (var i=0; i<array.length; i++){
200+
if (item === array[i]){
201+
return i;
202+
}
203+
}
204+
205+
return -1;
206+
};
207+
208+
this.findMaxValue = function(){
209+
var max = array[0];
210+
for (var i=1; i<array.length; i++){
211+
if (max < array[i]){
212+
max = array[i];
213+
}
214+
}
215+
216+
return max;
217+
};
218+
219+
this.findMinValue = function(){
220+
var min = array[0];
221+
for (var i=1; i<array.length; i++){
222+
if (min > array[i]){
223+
min = array[i];
224+
}
225+
}
226+
227+
return min;
228+
};
229+
230+
this.binarySearch = function(item){
231+
this.quickSort();
232+
233+
var low = 0,
234+
high = array.length - 1,
235+
mid, element;
236+
237+
while (low <= high){
238+
mid = Math.floor((low + high) / 2);
239+
element = array[mid];
240+
console.log('mid element is ' + element);
241+
if (element < item) {
242+
low = mid + 1;
243+
console.log('low is ' + low);
244+
} else if (element > item) {
245+
high = mid - 1;
246+
console.log('high is ' + high);
247+
} else {
248+
console.log('found it');
249+
return mid;
250+
}
251+
}
252+
return -1;
253+
};
254+
194255
}

chapter10/02-UsingSortingAlgorithms.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<title></title>
66
</head>
77
<body>
8-
<script type="text/javascript" src="01-SortingAlgorithms.js"></script>
8+
<script type="text/javascript" src="01-SortingSearchingAlgorithms.js"></script>
99
<script type="text/javascript" src="02-UsingSortingAlgorithms.js"></script>
1010
</body>
1111
</html>
+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-SortingSearchingAlgorithms.js"></script>
9+
<script type="text/javascript" src="03-UsingSearchingAlgorithms.js"></script>
10+
</body>
11+
</html>

chapter11/01-SearchingAlgorithms.js

-128
This file was deleted.

chapter11/02-UsingSearchingAlgorithms.html

-11
This file was deleted.

0 commit comments

Comments
 (0)