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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Chapter 7: Searching"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 7.1: Sequential_Search.sci"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"function[]=search(a,n,ele)\n",
" i=1;\n",
" j=0;\n",
" for i=1:n\n",
" if(a(i)==ele)\n",
" printf('Found %d AT %d\n',ele,i);\n",
" j=1;\n",
" end\n",
" end\n",
" if(j==0)\n",
" disp('%d NOT FOUND',ele);\n",
" end\n",
"endfunction\n",
"//Calling Routine:\n",
"a=[2 33 22 121 23 233 222]\n",
"disp(a,'Given array');\n",
"search(a,7,23)"
]
}
,
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 7.2: Sorted_sequential_search.sci"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"function[]=sortedsearch(a,n,ele)\n",
" if(a(1)>ele|a(n)<ele)\n",
" disp('NOT IN THE LIST');\n",
" else\n",
" i=1;\n",
" j=0;\n",
" for i=1:n\n",
" if(a(i)==ele)\n",
" printf('FOUND %d AT %d',ele,i);\n",
" j=1;\n",
" else\n",
" if(a(i)>ele)\n",
" break;\n",
" end\n",
" end\n",
" end\n",
" if(j==0)\n",
" disp('%d NOT FOUND',ele);\n",
" end\n",
" end\n",
"endfunction\n",
"//Calling Routine:\n",
"a=[2 22 23 33 121 222 233]//a should be sorted\n",
"disp(a,'Given array');\n",
"search(a,7,23)"
]
}
,
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 7.3: Binary_Search.sci"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"function[]=binsearch(a,n,i)\n",
" l=1;\n",
" h=n;\n",
" while(l<=h)\n",
" mid=int((l+h)/2);\n",
" if(a(mid)==i)\n",
" printf('FOUND %d AT %d',i,mid);\n",
" break;\n",
" else\n",
" if(a(mid)>i)\n",
" h=mid-1;\n",
" else\n",
" l=mid+1;\n",
" end\n",
" end\n",
" end\n",
"endfunction\n",
"//Calling Routine:\n",
"a=[2 22 23 33 121 222 233]//a should be sorted\n",
"disp(a,'Given array');\n",
"search(a,7,23)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Scilab",
"language": "scilab",
"name": "scilab"
},
"language_info": {
"file_extension": ".sce",
"help_links": [
{
"text": "MetaKernel Magics",
"url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md"
}
],
"mimetype": "text/x-octave",
"name": "scilab",
"version": "0.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
|