SlideShare a Scribd company logo
Building Java Programs
Chapter 12: Recursive public/private pairs
Chapter 13: Searching
reading: 13.3
2
3
4
Recursion and cases
 Every recursive algorithm involves at least 2 cases:
 base case: simple problem that can be solved directly.
 recursive case: more complex occurrence of the problem
that cannot be directly answered, but can instead be described
in terms of smaller occurrences of the same problem.
 Some recursive algorithms have more than one base or
recursive case, but all have at least one of each.
 A crucial part of recursive programming is identifying these
cases.
5
Recursion Challenges
 Forgetting a base case
 Infinite recursion resulting in StackOverflowError
 Working away from the base case
 The recursive case must make progress towards the base case
 Infinite recursion resulting in StackOverflowError
 Running out of memory
 Even when making progress to the base case, some inputs
may require too many recursive calls: StackOverflowError
 Recomputing the same subproblem over and over again
 Refining the algorithm could save significant time
6
Exercise
 Write a method crawl accepts a File parameter and prints
information about that file.
 If the File object represents a normal file, just print its name.
 If the File object represents a directory, print its name and
information about every file/directory inside it, indented.
cse143
handouts
syllabus.doc
lecture_schedule.xls
homework
1-tiles
TileMain.java
TileManager.java
index.html
style.css
 recursive data: A directory can contain other directories.
7
File objects
 A File object (from the java.io package) represents
a file or directory on the disk.
Constructor/method Description
File(String) creates File object representing file with given name
canRead() returns whether file is able to be read
delete() removes file from disk
exists() whether this file exists on disk
getName() returns file's name
isDirectory() returns whether this object represents a directory
length() returns number of bytes in file
listFiles() returns a File[] representing files in this directory
renameTo(File) changes name of file
8
Public/private pairs
 We cannot vary the indentation without an extra
parameter:
public static void crawl(File f, String indent) {
 Often the parameters we need for our recursion do not
match those the client will want to pass.
In these cases, we instead write a pair of methods:
1) a public, non-recursive one with parameters the client wants
2) a private, recursive one with the parameters we really need
9
Exercise solution 2
// Prints information about this file,
// and (if it is a directory) any files inside it.
public static void crawl(File f) {
crawl(f, ""); // call private recursive helper
}
// Recursive helper to implement crawl/indent behavior.
private static void crawl(File f, String indent) {
System.out.println(indent + f.getName());
if (f.isDirectory()) {
// recursive case; print contained files/dirs
for (File subFile : f.listFiles()) {
crawl(subFile, indent + " ");
}
}
}
10
Recursive Data
 A file is one of
 A simple file
 A directory containing files
 Directories can be nested to an arbitrary depth
 Iterative code to crawl a directory structure requires data
structures
 In recursive solution, we use the call stack
11
Binary search (13.1)
 binary search: Locates a target value in a sorted
array/list by successively eliminating half of the array from
consideration.
 Can be implemented with a loop or recursively
 Example: Searching the array below for the value 42:
inde
x
0 1 2 3 4 5 6 7 8 9 1
0
1
1
1
2
1
3
1
4
1
5
16
valu
e
-4 2 7 1
0
1
5
2
0
2
2
2
5
3
0
3
6
4
2
5
0
5
6
6
8
8
5
9
2
10
3
min mid ma
x
12
Binary search code
// Returns the index of an occurrence of target in a,
// or a negative number if the target is not found.
// Precondition: elements of a are in sorted order
public static int binarySearch(int[] a, int target) {
int min = 0;
int max = a.length - 1;
while (min <= max) {
int mid = (min + max) / 2;
if (a[mid] < target) {
min = mid + 1;
} else if (a[mid] > target) {
max = mid - 1;
} else {
return mid; // target found
}
}
return -(min + 1); // target not found
}
13
Recursive binary search (13.3)
 Write a recursive binarySearch method.
 If the target value is not found, return its negative insertion
point.
int index = binarySearch(data, 42); // 10
int index2 = binarySearch(data, 66); // -14
inde
x
0 1 2 3 4 5 6 7 8 9 1
0
1
1
1
2
1
3
1
4
1
5
16
valu
e
-4 2 7 1
0
1
5
2
0
2
2
2
5
3
0
3
6
4
2
5
0
5
6
6
8
8
5
9
2
10
3
14
Ordering and objects
 Can we compare Strings?
 Operators like < and > do not work with String objects.
 But we do think of strings as having an alphabetical ordering.
 natural ordering: Rules governing the relative placement
of all values of a given type.
 comparison function: Code that, when given two values
A and B of a given type, decides their relative ordering:
 A < B, A == B, A > B
15
The compareTo method (10.2)
 The standard way for a Java class to define a comparison
function for its objects is to define a compareTo method.
 Example: in the String class, there is a method:
public int compareTo(String other)
 A call of A.compareTo(B) will return:
a value < 0 if A comes "before" B in the ordering,
a value > 0 if A comes "after" B in the ordering,
or 0 if A and B are considered "equal" in the
ordering.
16
Using compareTo
 compareTo can be used as a test in an if statement.
String a = "alice";
String b = "bob";
if (a.compareTo(b) < 0) { // true
...
}
Primitives Objects
if (a < b) { ... if (a.compareTo(b) < 0) { ...
if (a <= b) { ... if (a.compareTo(b) <= 0) { ...
if (a == b) { ... if (a.compareTo(b) == 0) { ...
if (a != b) { ... if (a.compareTo(b) != 0) { ...
if (a >= b) { ... if (a.compareTo(b) >= 0) { ...
if (a > b) { ... if (a.compareTo(b) > 0) { ...
17
Exercise solution
// Returns the index of an occurrence of the given value in
// the given array, or a negative number if not found.
// Precondition: elements of a are in sorted order
public static int binarySearch(int[] a, int target) {
return binarySearch(a, target, 0, a.length - 1);
}
// Recursive helper to implement search behavior.
private static int binarySearch(int[] a, int target,
int min, int max) {
if (min > max) {
return -1; // target not found
} else {
int mid = (min + max) / 2;
if (a[mid] < target) { // too small; go right
return binarySearch(a, target, mid + 1, max);
} else if (a[mid] > target) { // too large; go left
return binarySearch(a, target, min, mid - 1);
} else {
return mid; // target found; a[mid] == target
}
}
}
18
Binary search runtime
 For an array of size N, it eliminates ½ until 1 element
remains.
N, N/2, N/4, N/8, ..., 4, 2, 1
 How many divisions does it take?
 Think of it from the other direction:
 How many times do I have to multiply by 2 to reach N?
1, 2, 4, 8, ..., N/4, N/2, N
 Call this number of multiplications "x".
2x
= N
x = log2 N
 Binary search is in the logarithmic complexity class.
19
Complexity classes
http://recursive-design.com/blog/2010/12/07/comp-sci-101-big-o-notation/ - post about a Google interview
20
 See section 12.4
Recursive Graphics

More Related Content

Similar to Algorithms Binary Search recursion ppt BSIT (20)

DOCX
Consider this code using the ArrayBag of Section 5.2 and the Locat.docx
maxinesmith73660
 
PDF
Mario Fusco - Lazy Java - Codemotion Milan 2018
Codemotion
 
PDF
Lazy java
Mario Fusco
 
PDF
Lazy Java
J On The Beach
 
PDF
Lazy Java
Nicola Pedot
 
PDF
4. The size of instructions can be fixed or variable. What are advant.pdf
mumnesh
 
PPTX
Introduction to Client-Side Javascript
Julie Iskander
 
PDF
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
QalandarBux2
 
PPTX
M251_Meeting_ jAVAAAAAAAAAAAAAAAAAA.pptx
smartashammari
 
PDF
Ds lab handouts
Ayesha Bhatti
 
PDF
Algorithms: I
Joyjit Choudhury
 
PPT
Tower of Hanoi.ppt
SACHINVERMA255386
 
PPT
DATA STRUCTURE AND APPLICATIONS -MODULE1.PPT
AIET
 
PPTX
Python programming workshop
BAINIDA
 
PPT
Chapter 7 - Defining Your Own Classes - Part II
Eduardo Bergavera
 
PDF
Assignment of Advanced data structure and algorithms..pdf
vishuv3466
 
PDF
Assignment of Advanced data structure and algorithms ..pdf
vishuv3466
 
PDF
Assignment of Advanced data structure and algorithm ..pdf
vishuv3466
 
PDF
RubyMiniGuide-v1.0_0
tutorialsruby
 
PDF
RubyMiniGuide-v1.0_0
tutorialsruby
 
Consider this code using the ArrayBag of Section 5.2 and the Locat.docx
maxinesmith73660
 
Mario Fusco - Lazy Java - Codemotion Milan 2018
Codemotion
 
Lazy java
Mario Fusco
 
Lazy Java
J On The Beach
 
Lazy Java
Nicola Pedot
 
4. The size of instructions can be fixed or variable. What are advant.pdf
mumnesh
 
Introduction to Client-Side Javascript
Julie Iskander
 
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
QalandarBux2
 
M251_Meeting_ jAVAAAAAAAAAAAAAAAAAA.pptx
smartashammari
 
Ds lab handouts
Ayesha Bhatti
 
Algorithms: I
Joyjit Choudhury
 
Tower of Hanoi.ppt
SACHINVERMA255386
 
DATA STRUCTURE AND APPLICATIONS -MODULE1.PPT
AIET
 
Python programming workshop
BAINIDA
 
Chapter 7 - Defining Your Own Classes - Part II
Eduardo Bergavera
 
Assignment of Advanced data structure and algorithms..pdf
vishuv3466
 
Assignment of Advanced data structure and algorithms ..pdf
vishuv3466
 
Assignment of Advanced data structure and algorithm ..pdf
vishuv3466
 
RubyMiniGuide-v1.0_0
tutorialsruby
 
RubyMiniGuide-v1.0_0
tutorialsruby
 

Recently uploaded (20)

PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Ad

Algorithms Binary Search recursion ppt BSIT

  • 1. Building Java Programs Chapter 12: Recursive public/private pairs Chapter 13: Searching reading: 13.3
  • 2. 2
  • 3. 3
  • 4. 4 Recursion and cases  Every recursive algorithm involves at least 2 cases:  base case: simple problem that can be solved directly.  recursive case: more complex occurrence of the problem that cannot be directly answered, but can instead be described in terms of smaller occurrences of the same problem.  Some recursive algorithms have more than one base or recursive case, but all have at least one of each.  A crucial part of recursive programming is identifying these cases.
  • 5. 5 Recursion Challenges  Forgetting a base case  Infinite recursion resulting in StackOverflowError  Working away from the base case  The recursive case must make progress towards the base case  Infinite recursion resulting in StackOverflowError  Running out of memory  Even when making progress to the base case, some inputs may require too many recursive calls: StackOverflowError  Recomputing the same subproblem over and over again  Refining the algorithm could save significant time
  • 6. 6 Exercise  Write a method crawl accepts a File parameter and prints information about that file.  If the File object represents a normal file, just print its name.  If the File object represents a directory, print its name and information about every file/directory inside it, indented. cse143 handouts syllabus.doc lecture_schedule.xls homework 1-tiles TileMain.java TileManager.java index.html style.css  recursive data: A directory can contain other directories.
  • 7. 7 File objects  A File object (from the java.io package) represents a file or directory on the disk. Constructor/method Description File(String) creates File object representing file with given name canRead() returns whether file is able to be read delete() removes file from disk exists() whether this file exists on disk getName() returns file's name isDirectory() returns whether this object represents a directory length() returns number of bytes in file listFiles() returns a File[] representing files in this directory renameTo(File) changes name of file
  • 8. 8 Public/private pairs  We cannot vary the indentation without an extra parameter: public static void crawl(File f, String indent) {  Often the parameters we need for our recursion do not match those the client will want to pass. In these cases, we instead write a pair of methods: 1) a public, non-recursive one with parameters the client wants 2) a private, recursive one with the parameters we really need
  • 9. 9 Exercise solution 2 // Prints information about this file, // and (if it is a directory) any files inside it. public static void crawl(File f) { crawl(f, ""); // call private recursive helper } // Recursive helper to implement crawl/indent behavior. private static void crawl(File f, String indent) { System.out.println(indent + f.getName()); if (f.isDirectory()) { // recursive case; print contained files/dirs for (File subFile : f.listFiles()) { crawl(subFile, indent + " "); } } }
  • 10. 10 Recursive Data  A file is one of  A simple file  A directory containing files  Directories can be nested to an arbitrary depth  Iterative code to crawl a directory structure requires data structures  In recursive solution, we use the call stack
  • 11. 11 Binary search (13.1)  binary search: Locates a target value in a sorted array/list by successively eliminating half of the array from consideration.  Can be implemented with a loop or recursively  Example: Searching the array below for the value 42: inde x 0 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 16 valu e -4 2 7 1 0 1 5 2 0 2 2 2 5 3 0 3 6 4 2 5 0 5 6 6 8 8 5 9 2 10 3 min mid ma x
  • 12. 12 Binary search code // Returns the index of an occurrence of target in a, // or a negative number if the target is not found. // Precondition: elements of a are in sorted order public static int binarySearch(int[] a, int target) { int min = 0; int max = a.length - 1; while (min <= max) { int mid = (min + max) / 2; if (a[mid] < target) { min = mid + 1; } else if (a[mid] > target) { max = mid - 1; } else { return mid; // target found } } return -(min + 1); // target not found }
  • 13. 13 Recursive binary search (13.3)  Write a recursive binarySearch method.  If the target value is not found, return its negative insertion point. int index = binarySearch(data, 42); // 10 int index2 = binarySearch(data, 66); // -14 inde x 0 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 16 valu e -4 2 7 1 0 1 5 2 0 2 2 2 5 3 0 3 6 4 2 5 0 5 6 6 8 8 5 9 2 10 3
  • 14. 14 Ordering and objects  Can we compare Strings?  Operators like < and > do not work with String objects.  But we do think of strings as having an alphabetical ordering.  natural ordering: Rules governing the relative placement of all values of a given type.  comparison function: Code that, when given two values A and B of a given type, decides their relative ordering:  A < B, A == B, A > B
  • 15. 15 The compareTo method (10.2)  The standard way for a Java class to define a comparison function for its objects is to define a compareTo method.  Example: in the String class, there is a method: public int compareTo(String other)  A call of A.compareTo(B) will return: a value < 0 if A comes "before" B in the ordering, a value > 0 if A comes "after" B in the ordering, or 0 if A and B are considered "equal" in the ordering.
  • 16. 16 Using compareTo  compareTo can be used as a test in an if statement. String a = "alice"; String b = "bob"; if (a.compareTo(b) < 0) { // true ... } Primitives Objects if (a < b) { ... if (a.compareTo(b) < 0) { ... if (a <= b) { ... if (a.compareTo(b) <= 0) { ... if (a == b) { ... if (a.compareTo(b) == 0) { ... if (a != b) { ... if (a.compareTo(b) != 0) { ... if (a >= b) { ... if (a.compareTo(b) >= 0) { ... if (a > b) { ... if (a.compareTo(b) > 0) { ...
  • 17. 17 Exercise solution // Returns the index of an occurrence of the given value in // the given array, or a negative number if not found. // Precondition: elements of a are in sorted order public static int binarySearch(int[] a, int target) { return binarySearch(a, target, 0, a.length - 1); } // Recursive helper to implement search behavior. private static int binarySearch(int[] a, int target, int min, int max) { if (min > max) { return -1; // target not found } else { int mid = (min + max) / 2; if (a[mid] < target) { // too small; go right return binarySearch(a, target, mid + 1, max); } else if (a[mid] > target) { // too large; go left return binarySearch(a, target, min, mid - 1); } else { return mid; // target found; a[mid] == target } } }
  • 18. 18 Binary search runtime  For an array of size N, it eliminates ½ until 1 element remains. N, N/2, N/4, N/8, ..., 4, 2, 1  How many divisions does it take?  Think of it from the other direction:  How many times do I have to multiply by 2 to reach N? 1, 2, 4, 8, ..., N/4, N/2, N  Call this number of multiplications "x". 2x = N x = log2 N  Binary search is in the logarithmic complexity class.
  • 20. 20  See section 12.4 Recursive Graphics