Download presentation
1
General Computer Science for Engineers CISC 106 Lecture 08
James Atlas Computer and Information Sciences 07/01/2009
2
Lecture Overview Review Linear Search Binary Search Sorting
3
Iterative strategy Given [5 4 2 10 6 7] find which position 6 occupies
Alternatively, does the array contain the number 6?
4
Recursive strategy Split array in half Search each half of array
5
Binary Search Now, what if the array is sorted, can we search it faster?
6
Binary Search Find N in list
Pick a number X halfway between the start and end of the list If X == N we are done else if X < N search top half of list else search bottom half of list
7
Let’s do this in Matlab Recursive solution
8
Binary Search Flowchart diagram of the algorithm
Note the two stopping conditions (Exits) Note the one loop (Note: flowchart is equivalent to pseudocode) How much faster is this than linear search? If linear search takes roughly n steps for n things, Then binary search takes roughly log2(n) steps for n things. This is because we divide the n things by 2 each time.
9
Sorting If we draw a hand of cards how do we sort it from least to greatest?
11
Sorting Selection Sort Merge Sort
12
Selection Sort Find the minimum value in the list
Swap it with the value in the first position Repeat the steps above for the remainder of the list Starting at the second position and advancing each time
13
Selection Sort: Iterative
ssort.m
14
Group Sort Now group up with 3-4 neighbors and combine your cards
How can we sort these?
15
Merge Sort If the list is of length 0 or 1, then it is already sorted.
Otherwise: Divide the unsorted list into two sublists of about half the size. Sort each sublist recursively by re-applying merge sort. Merge the two sublists back into one sorted list.
16
Merge Sort Recursive mergesort.m
17
Project 1 On Sakai/course website Shark and goldfish simulator
Teams of 3-5 Form team by July 6, 9:00pm Complete project due July 20 Functions that need to be implemented are listed
18
Shark (X) vs Goldfish (O)
O ---O------ -----O---- --O ----X----- O-
19
Each team has 3 members designated as librarian, recorder, and team leader.
Grading: 1/3 individual work, 1/3 team accomplishments (with blind rating), 1/3 questions on midterm/exam about the project
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.