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
Ad

More Related Content

Similar to Algorithms Binary Search recursion ppt BSIT (20)

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

Recently uploaded (20)

Unlocking the Power of IVR: A Comprehensive Guide
Unlocking the Power of IVR: A Comprehensive GuideUnlocking the Power of IVR: A Comprehensive Guide
Unlocking the Power of IVR: A Comprehensive Guide
vikasascentbpo
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Web and Graphics Designing Training in Rajpura
Web and Graphics Designing Training in RajpuraWeb and Graphics Designing Training in Rajpura
Web and Graphics Designing Training in Rajpura
Erginous Technology
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
Build 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHSBuild 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHS
TECH EHS Solution
 
TrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token ListingTrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token Listing
Trs Labs
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
Vaibhav Gupta BAML: AI work flows without Hallucinations
Vaibhav Gupta BAML: AI work flows without HallucinationsVaibhav Gupta BAML: AI work flows without Hallucinations
Vaibhav Gupta BAML: AI work flows without Hallucinations
john409870
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Mastering Advance Window Functions in SQL.pdf
Mastering Advance Window Functions in SQL.pdfMastering Advance Window Functions in SQL.pdf
Mastering Advance Window Functions in SQL.pdf
Spiral Mantra
 
Unlocking the Power of IVR: A Comprehensive Guide
Unlocking the Power of IVR: A Comprehensive GuideUnlocking the Power of IVR: A Comprehensive Guide
Unlocking the Power of IVR: A Comprehensive Guide
vikasascentbpo
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Web and Graphics Designing Training in Rajpura
Web and Graphics Designing Training in RajpuraWeb and Graphics Designing Training in Rajpura
Web and Graphics Designing Training in Rajpura
Erginous Technology
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
Build 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHSBuild 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHS
TECH EHS Solution
 
TrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token ListingTrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token Listing
Trs Labs
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
Vaibhav Gupta BAML: AI work flows without Hallucinations
Vaibhav Gupta BAML: AI work flows without HallucinationsVaibhav Gupta BAML: AI work flows without Hallucinations
Vaibhav Gupta BAML: AI work flows without Hallucinations
john409870
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Mastering Advance Window Functions in SQL.pdf
Mastering Advance Window Functions in SQL.pdfMastering Advance Window Functions in SQL.pdf
Mastering Advance Window Functions in SQL.pdf
Spiral Mantra
 
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