Cover page images (keyboard)

Lab 11 - Searching

Travis Mayberry & Sue Evans




Objectives

Objectives for today's lab:

Step 0 - Set up












Overview

One iteration of linear search is faster than sorting the list and running a binary search, but if sequential searches are needed, then it may be worth the upfront cost of sorting the list.

Program Outline

Pseudo Code

Import random and time
From search import *

Get length of list from the user
Create a list containing the values 0 to length - 1 
Shuffle the list with random.shuffle()

Start linear search timer
Loop 1000 times:
	Generate a random integer between 0 and length - 1
	Call linear search on the list with this random number
Stop linear search timer

Start binary search timer
Sort list with list.sort()
Loop 1000 times:
	Generate a random integer between 0 and length - 1
	Call binary search on list with random number
Stop binary search timer

Print out resulting times

Step 1 - Create List

Step 2 - Testing Linear Search

Step 3 - Binary Search

Step 4 -
Timing the Execution

Here's an example of timing:

import time

t0 = time.time()
y = 1

for x in range(1000):
    y = y * x
	
t1 = time.time()

print "Execution time:", t1 - t0