CSLTR Week10
CSLTR Week10
1
CONTENTS
Introduction
Linear search
Binary search
2
INTRODUCTION
This problem is very popular
Inputs of the problem are the information
needed and output is optimized solution
satisfying constraint condition
Two methods of searching are linear and binary
Search problem includes:
◦ Search space/solution space
◦ Constraint condition
Solution space may include:
◦ Explicit: only choose and check
◦ Implicit: must create to continue processing
3
INTRODUCTION
The process of choosing solutions includes
following steps:
◦ Step 0: create candidate solution (if not)
◦ Step 1: check if candidate solution satisfies constrain
condition or not
◦ Step 2: Among the candidate solutions satisfying
constrain condition, there are some standards to
choose the best one
Example: Find the even biggest number in array
a with n whole distinct numbers (n > 0)
◦ Search space: n elements
◦ Constraint condition: even number
4
LINEAR SEARCH
(IN 1D ARRAY)
Problem 1: Let a be an array of n integers.
Write a function finding a value of the biggest
element 1 int FindMaxValue(int a[], int n){
2 int res = a[0];
3 for(int i = 1; i < n; i++){
4 if(a[i] > res) res = a[i];
5 }
6 return res;
7 }
1 int Find(int a[], int n, int x){ int Find(int a[], int n, int x){
2 int i = 0; int i = 0;
3 while((i < n) && (a[i] != x)) a[n] = x;
4 i++; while(a[i] != x) i++;
5 if(i < n) return i; if(i < n) return i;
6 return -1; return -1;
7 } }
◦ Cost: the luckiest is loop 1 time-looping. The average is n/2-
looping and the worst is n-looping O(n)
◦ Search space: {0, n – 1}
◦ Constrain condition: value equal to x
◦ Standard: the element with the smallest index 6
LINEAR SEARCH
(IN 1D ARRAY)
Problem 3: Let a be an array of n integers. Write a
function finding the position of the smallest square
number
1 bool iSquare(int number){ 8 if(iSquare(a[i]) && (idx==-1 || a[i]<lc){
2 int i = (int)sqrt((float)number); 9 lc = a[i];
3 return (i*i == number); 10 idx = i;
4 } 11 }
5 int IdxOfMinSquareNumber(int a[], int n){ 12 }
6 int idx = -1, lc = 0; 13 return idx;
7 for(int i = 0; i < n; i++){ 14 }
<???> <???>
2750f3c 2750f54
name name
<61ff1c> <2750f3c> <2750f54>
2750f38 2750f50 null
h id price next id price next
<2750f38> <2750f40> <2750f44> <2750f50> <2750f58>
13
LINEAR SEARCH
(IN LINKED LIST)
Problem 9: Let a linked list of students. Each student includes: code,
name, faculty and GPA. Write a function counting a number of
students with GPA in [min, max]
1 #define DEPT 50 10 float count(STUDENT* s, char* strDept, float min, float max){
2 #define NAME 100 11 STUDENT* t = s; int c = 0;
3 struct STUDENT{ 12 while(t){
4 int ID; 13 if(strcmp(t->strDept, strDept) ==0 )
5 char strDept[DEPT+1]; 14 if(t->GPA>=min && t->GPA<=max) c++;
6 char strName[NAME+1]; 15 t = t->next;
7 float GPA; 16 }
8 STUDENT* next; 17 return c;
9 }; 18 }
Bai1.docx
6 else to = mid – 1;
7 }
x = 99
5
8 a.insert(a.begin + from, x);
9 }
20
BINARY SEARCH
(IN 1D ARRAY)
Problem 15: Let a be an unimodal array
with n integer elements. Write a function
finding the biggest element
1 int BinarySearchMax(int a[], int n){
2 int from = 0, to = n – 1, mid;
from = 203 to = 36 mid = 32
1
3 while(from < to){
4 mid = (from + to)/2; 0 1 2 3 4 5 6
5 if(a[mid] < a[mid + 1]) from = mid + 1; 1 4 8 9 7 6 2
6 else to = mid;
7 }
8 return a[from];
9 }
21
BINARY SEARCH
(IN 1D STRUCTURAL ARRAY)
Problem 16: Any library has the books with the names following the alphabetically increasing order.
Write a function determining the position of the book needed
24
BINARY SEARCH
(IN 2D ARRAY)
Problem 19: Let a be integer array (m n). Array a has the numbers left-to-
right increasing in each row. Write a function checking if array a contains the
element with x value or not (for example x = 14).
1 bool search2D(int** a, int m, int n, int x){
2 int from, to, mid;
3 for(int i = 0; i < m; i++){
4 from = 0; to = n – 1; 4 7 9 21 loop log24
5 while(from <= to){ 7 9 11 43 loop log24
6 mid = (from + to)/2; 8 9 44 67 loop log24
loop log24 6log24
7 if(a[i][mid] == x) return true; 1 3 4 6
8 else{ 2 4 6 7 loop log24
9 if(a[i][mid] < x) from = mid + 1; 1 1 6 7 loop log24
10 else to = mid – 1;
11 }}}
12 return false; } 25
BINARY SEARCH
(IN 2D ARRAY)
Problem 20: Let a be integer array (m n). Array a has the
numbers left-to-right increasing in each row, and numbers
bottom-to-up increasing in each column. Write a function
checking if array a contains the element with x value or not (for
example x = 14)
1 bool search2D(int** a, int m, int n, int x){ j
2 int i = 0, j = 0;
0 1 2 3 4
3 while(i < m && j < n){
i 0 7 12 16 20 95
4 if(a[i][j] == x) return true;
1 5 9 14 15 19
5 else{
2 3 5 7 9 11
6 if(a[i][j] < x) j++;
3 1 2 3 4 5
7 else i++;
8 }
Loop cost O(m + n)
9 return false;
10 } 26