-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.html
More file actions
132 lines (120 loc) · 4.64 KB
/
search.html
File metadata and controls
132 lines (120 loc) · 4.64 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
console.log('my ctrl')
$scope.algo = 'linear';
$scope.search= function() {
var inputData = $scope.input_text.split(',');
if ($scope.algo == 'linear') {
linearSearch(inputData, $scope.search_text);
} else if ($scope.algo == 'binary') {
binarySearch(inputData, $scope.search_text);
} else if ($scope.algo == 'bubble') {
bubbleSort(angular.copy(inputData), $scope.search_text);
}
};
var linearSearch = function(inputData, searchText) {
$scope.displayResult = [];
$scope.displayList = inputData;
$scope.displaySearch = searchText;
for(var i=0; i<inputData.length; i++) {
if (inputData[i] == searchText) {
$scope.displayResult.push("ROUND " + (i+1) + " FOUND: " + inputData[i] + '==' + searchText);
break;
} else {
$scope.displayResult.push("ROUND " + (i+1) + "===> " + inputData[i] + '!=' + searchText);
}
}
}
var bubbleSort = function (inputData) {
$scope.displayResult = [];
$scope.displayList = inputData;
var sorted = false;
var n = inputData.length-1;
var round = 0;
while(!sorted) {
sorted = true;
for (var i=0; i < n; i++) {
console.log('i', i, i+1);
console.log('compare', inputData[i], inputData[i+1]);
if (parseInt(inputData[i]) > parseInt(inputData[i+1])) {
var temp = inputData[i];
inputData[i] = inputData[i+1];
inputData[i+1] = temp;
sorted = false;
console.log('swap', i, i+1, inputData[i], inputData[i+1], inputData);
}
}
console.log(inputData);
// last char sorted
n--;
}
}
var binarySearch = function (inputData, searchText) {
$scope.displayResult = [];
var sortedData = inputData.sort();
$scope.displayList = sortedData;
$scope.displaySearch = searchText;
var min = 1;
var max = sortedData.length;
var mid = Math.floor((min+max)/2);
console.log(min, max, mid, $scope.algo);
var round = 0;
var isFound = false;
while (!isFound) {
round++;
console.log('round', round, 'mid min max', mid, min, max,'value', sortedData[mid]);
if (max < min) {
if (sortedData[max] == searchText) {
$scope.displayResult.push('ROUND ' + round + "==>" + sortedData[max] + '==' + searchText);
}
break;
}
var mid = min + Math.floor((max-min)/2);
if (sortedData[mid] < searchText) {
min = mid+1;
$scope.displayResult.push("ROUND " + round + "==>" + sortedData[mid] + "!=" + searchText);
}
if (sortedData[mid] > searchText) {
max = mid-1;
$scope.displayResult.push("ROUND " + round + "==>" + sortedData[mid] + "!=" + searchText);
}
if (sortedData[mid] == searchText) {
$scope.displayResult.push('ROUND ' + round + "==>" + sortedData[mid] + '==' + searchText);
break;
}
if (round > sortedData.length) break;
}
};
});
</script>
<!DOCTYPE html>
<html lang="en-US" ng-app="myApp">
<meta charset="utf-8"/>
<body>
<div ng-controller="myCtrl">
List <input type="text" name="input" ng-model="input_text"/>
<br/>
ค้นหา <input type="text" name="search_text" ng-model="search_text"/> <button ng-click="search(algo)">ค้นหา</button>
<br/>
ประเภทการค้นหา
<br/>
{{algo}}
<select name="algo" ng-model="algo">
<option value="linear" ng-selected="algo == 'linear'">Linear Search</option>
<option value="binary" ng-selected="algo == 'binary'">Binary Search</option>
<option value="bubble" ng-selected="algo == 'bubble'">Bubble Search</option>
</select><br/>
Result <br/>
List: {{displayList}}<br/>
Search: {{displaySearch}}<br>
Result ::: <br>
<div style="border: solid 1px;">
<p ng-repeat="row in displayResult">
{{row}}
</p>
</div>
</div>
</body>
</html>