30 likes | 260 Views
Searching. Example: Express the linear search algorithm as a recursive procedure. Recursive Algorithms (the linear search algorithm ). ALGORITHM 5 A Recursive Linear Search Algorithm . procedure search ( i,j,x : i,j,x integers, 1 <=i<=n , 1 <=j<=n) if a i = x then location := i
E N D
Example: Express the linear search algorithm as a recursive procedure. Recursive Algorithms (the linear search algorithm ) ALGORITHM 5 A Recursive Linear Search Algorithm. procedure search(i,j,x: i,j,x integers, 1 <=i<=n, 1<=j<=n) if ai = x then location := i else if i = j then location : = 0 else search(i+1,j, x) 2 Lecturer: Shaykhah
Example: Construct a recursive version of a binary search algorithm Recursive Algorithms (the binary search algorithm ) ALGORITHM 6 A Recursive Binary Search Algorithm. procedure binarySearch(i,j,x: i,j,x integers, 1 <= i <= n, 1<= j <= n) m := if x = am then location := m else if (x < am and i < m ) then binarySearch( x, i, m-1) else if (x > am and j > m ) then binarySearch(x, m+1, j) else location := 0 3 Lecturer: Shaykhah