intro to sorting intro to computer science cs1510 dr. sarah diesburg

19
Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

Upload: kathryn-reynolds

Post on 04-Jan-2016

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

Intro to Sorting

Intro to Computer Science

CS1510

Dr. Sarah Diesburg

Page 2: Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

Last Time

We looked at two basic algorithms for searching Linear search Binary search

Linear search was the easiest to write But perhaps not the best from a complexity

standpoint

2

Page 3: Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

Last Time

Big “O” measures how badly the problem grows as the data set grows Study of complexity of algorithms Worst case of linear search was N, where N is the

number of comparisons that we need to perform Double the number of items in list, double the

amount of time needed to complete the search in the worst case

3

Page 4: Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

Last Time

The binary search was another solution that incurred less comparisons in the worst case Only works on sorted list

4

Page 5: Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

Binary Search

Binary search algorithm Try the guess at middle index of the range If the value we are searching for is higher than

number at the index, then adjust your low range bound to be your guess+1

If the value we are searching for is lower than number at the index, then adjust your high range bound to be your guess-1

Repeat

5

Page 6: Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

Binary Search

What is the worst-case scenario of the binary search?

Thinking of a number between 1 and 100 7 guesses in total – why?

1 guesses – cut down to 50 possibilities 2 guesses – cut down to 25 3 guesses – cut down to 12 4 guesses – cut down to 6 5 guesses – cut down to 3 6 guesses – cut down to 1 7 guesses – to figure out if last guess is right

6

Page 7: Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

Binary Search

What is the complexity of a binary search? Big O value of log2 N This is “log base 2”

log2(100) = x What is this saying?

7

Page 8: Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

Binary Search

What is the complexity of a binary search? Big O value of log2 N This is “log base 2”

log2(100) = x What is this saying? 2x = 100 Go “to the next power” when not exact

8

Page 9: Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

Binary Search

How does that relate to our binary search? Let’s say there are 16 items in our list. What is

the worst case number of guesses? 32? 34? 64? One million?

9

Page 10: Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

Binary Search

How does that relate to our binary search? Let’s say there are 16 items in our list. What is

the worst case number of guesses? 32? 34? 64? One million?

One million is about 20 guesses 2^10 = 1024 One million is 1000 squared, so twice as much

10

Page 11: Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

Searching

So which kind of search would amazon.com use to search their databases?

11

Page 12: Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

Demo

binarySearch() on different types of lists Ordered Odd Reverse

12

Page 13: Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

Demo

binarySearch() on different types of lists Ordered Odd Reverse

The reverse list doesn’t work because the list needs to be sorted in ascending order. How do we sort?

13

Page 14: Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

Group Time!

Let’s get into 4 big groups Put the cards in order You can only look at two cards at a time

14

Page 15: Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

Sorting Methods

Insertion Sort Two chunks of data (sorted and unsorted) Go through unsorted data and insert it in order

into sorted pile

As humans, if we could look at all cards at once, we would probably perform an insertion sort

15

Page 16: Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

Sorting Methods

Bubble Sort Compare two cards Move the higher card to the top Pick out another card Repeat

Higher cards “bubble” to the top After each run, one more high card is in order

Lower cards slowly “bubble” to the bottom

16

Page 17: Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

Sorting Methods

Selection Sort Find smallest card by

Comparing two cards at a time Saving out the current smallest card Repeat until reach end of pile

Put smallest card in sorted pile Repeat

17

Page 18: Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

Sorting

Humans will tend to want to fan out all the cards and scan them With 13 cards, this works But what if I gave you 10,000 student ID cards?

Computers can only compare a finite number of cards together at a time

Let’s start to think about how long each of these will take in the worst case

18

Page 19: Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

Big O (Worst Case)

Selection sort First pass – compare 13 cards and set aside

lowest Second pass – compare 12 cards and set aside

lowest Etc…. How many passes do I make? – 13 N^2 = 169 but actually 91 As you double your data, you quadruple your

time.

19