Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Analysis of Sequential and Binary Search Algorithms, Study notes of Computer Science

This document compares the performance of sequential and binary search algorithms through various examples. It discusses the time complexity of both algorithms, their efficiency, and the advantages and disadvantages of each. The document also includes a comparison table and an analysis of the worst-case scenario for binary search.

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-fa8-2
koofers-user-fa8-2 🇺🇸

10 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
12/1/08&
1&
CMSC 150: Introduction to Computing
0 1 2 3
Problem,
Bea O.
Hugginkiss,
Amanda
Jass,
Hugh
Rotch,
Mike
Strappe,
Jacques
Tabooger,
Ollie
4 5
Search for a Name
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Analysis of Sequential and Binary Search Algorithms and more Study notes Computer Science in PDF only on Docsity!

CMSC 150: Introduction to Computing

Problem, Bea O. Hugginkiss, Amanda Jass, Hugh Rotch, Mike Strappe, Jacques Tabooger, Ollie 4 5

Search for a Name

Sequential Search

public int search( String[] array, String nameToFind )

for ( int i = 0; i < array.length; i++ )

if ( array[i].equals( nameToFind ) )

return i;

return -1; // not found

Solves the problem, but…

 Is it a "good" algorithm?

Problem, Bea O. Hugginkiss, Amanda Jass, Hugh Rotch, Mike Strappe, Jacques Tabooger, Ollie 4 5 Bea O. Problem

Solves the problem, but…

 Is it a "good" algorithm?

Problem, Bea O. Hugginkiss, Amanda Jass, Hugh Rotch, Mike Tabooger, Ollie 999999

Ollie Tabooger Found in 1,000,000 steps…

(Worst Case)

Solves the problem, but…

 Is it a "good" algorithm?

Problem, Bea O. Hugginkiss, Amanda Jass, Hugh Rotch, Mike Tabooger, Ollie

n - 1

Ollie Tabooger Found in n steps…

(Worst Case)

On Average…

 About n /2 steps required

Problem, Bea O. Hugginkiss, Amanda Jass, Hugh Rotch, Mike Tabooger, Ollie

Maya Buttreeks Found in ~ n/2 steps…

(Average Case)

n - 1

Big-Oh: Algorithm Complexity

 We group algorithms into classes of similar

complexity (i.e., how many steps required)

 Sequential search is a O( n ) algorithm (linear)

O(1) O(log n) O(n) O(n^2 ) O(2n) constant-time logarithmic linear quadratic exponential Sequential search Traveling Salesman (brute force) Modifying a 2D image

Problem, Bea O. Hugginkiss, Amanda Jass, Hugh Rotch, Mike Strappe, Jacques Tabooger, Ollie 4 5

A Better Algorithm

Buttreeks, Maya 6 Maya Buttreeks

Not equal, but whaddaya know?

Problem, Bea O. Hugginkiss, Amanda Jass, Hugh Rotch, Mike Strappe, Jacques Tabooger, Ollie 4 5

A Better Algorithm

Buttreeks, Maya 6 Maya Buttreeks

"Half" of the list is eliminated!

Problem, Bea O. Hugginkiss, Amanda Jass, Hugh Rotch, Mike Strappe, Jacques Tabooger, Ollie 4 5

A Better Algorithm

Buttreeks, Maya 6 Maya Buttreeks

Not equal, but whaddaya know?

Problem, Bea O. Hugginkiss, Amanda Jass, Hugh Rotch, Mike Strappe, Jacques Tabooger, Ollie 4 5

A Better Algorithm

Buttreeks, Maya 6 Maya Buttreeks

"Half" of the front "half"

is eliminated!

Problem, Bea O. Hugginkiss, Amanda Jass, Hugh Rotch, Mike Strappe, Jacques Tabooger, Ollie 4 5

But Wait…

Buttreeks, Maya 6 Ollie Tabooger 0 1 2 3 Problem, Bea O. Hugginkiss, Amanda Jass, Hugh Rotch, Mike Strappe, Jacques Tabooger, Ollie 4 5

But Wait…

Buttreeks, Maya 6 Ollie Tabooger

 Sequential search: 7 steps

 Binary search: 3 steps

Analyzing binary search

 Consider worst case for an array of size 32:

32 elements 16 elements 8 elements 4 elements 2 elements 1 element

Analyzing binary search

 Consider worst case for an array of size 32:

32 elements 16 elements 8 elements 4 elements 2 elements 1 element

 Ignore the last (1-element array) comparison

 Start with 32 elements

 5 comparisons total until found

 Note 2^5 = 32  log 2 32 = 5

 Log 2 of input size gives ~ number of comparisons

Sequential vs. Binary Search

 Sequential

 O(n): linear

 Easy to implement

 Inefficient for large input size

 Binary: O(log n)

 O(log n): logarithmic

 Reasonably efficient for large input size

 Requires the list to be sorted