searching & sorting

26
Searching & Sorting

Upload: neron

Post on 22-Feb-2016

40 views

Category:

Documents


0 download

DESCRIPTION

Searching & Sorting. Linear Search. I'm thinking of a number between 1 and 100 You try to guess it I'll give too low/too high hints. Linear Search. I'm thinking of a number between 1 and 100 You try to guess it I'll give too low/too high hints Method #1 – Linear Search 1, 2, 3…. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Searching & Sorting

Searching & Sorting

Page 2: Searching & Sorting

Algorithms

• Step by step recipe to do a task…

Page 3: Searching & Sorting

Algorithms

• Step by step recipe to do a task where:– Operations are computable– Operations are unambiguous– Operations are well ordered– Finite number of operations

Page 4: Searching & Sorting

Linear Search

• I'm thinking of a number between 1 and 100– You try to guess it– I'll give too low/too high hints

Page 5: Searching & Sorting

Linear Search

• I'm thinking of a number between 1 and 100– You try to guess it– I'll give too low/too high hints

• Method #1 – Linear Search– 1, 2, 3….

Page 6: Searching & Sorting

Linear Search Algorithm

• In pseudocode:

Page 7: Searching & Sorting

Binary Search

• Method #2 – Binary Search– Pick middle of remaining search space– Too high? Eliminate middle and above– Too low? Eliminate middle and below

Page 8: Searching & Sorting

Algorithm

Page 9: Searching & Sorting

Binary Search

Searching for 5:Location: 1 2 3 4 5 6 Value:

Step minLocation maxLocation middleLocation middleValue1 1 6 (1 + 6) / 2

= 3.5 = 3

4

Page 10: Searching & Sorting

Binary Search

Searching for 5:Location: 1 2 3 4 5 6 Value:

Step minLocation maxLocation middleLocation middleValue1 1 6 (1 + 6) / 2

= 3.5 = 3

4

Page 11: Searching & Sorting

Binary Search

Searching for 5:Location: 1 2 3 4 5 6 Value:

Step minLocation maxLocation middleLocation middleValue1 1 6 (1 + 6) / 2

= 3.5 = 3

4

2 4 (one more than old middleLocation)

6 (unchanged)

(4 + 6) / 2= 5

7 (value at location 5)This is too big, need to search lower

Page 12: Searching & Sorting

Binary Search

Searching for 5:Location: 1 2 3 4 5 6 Value:

Step minLocation maxLocation middleLocation middleValue1 1 6 (1 + 6) / 2

= 3.5 = 3

4

2 4 (one more than old middleLocation)

6 (unchanged)

(4 + 6) / 2= 5

7 (value at location 5)This is too big, need to search lower

Page 13: Searching & Sorting

Binary Search

Searching for 5:Location: 1 2 3 4 5 6 Value:

Step minLocation maxLocation middleLocation middleValue1 1 6 (1 + 6) / 2

= 3.5 = 3

4

2 4 (one more than old middleLocation)

6 (unchanged)

(4 + 6) / 2= 5

7 (value at location 5)This is too big, need to search lower

3 4 (unchanged)

4 (one less than old middleLocation)

(4 + 4) / 2= 8 / 2= 4

5 Found it!!!

Page 14: Searching & Sorting

Binary Search

Searching for 5:Location: 1 2 3 4 5 6 Value:

Step minLocation maxLocation middleLocation middleValue1 1 6 (1 + 6) / 2

= 3.5 = 3

4

2 4 (one more than old middleLocation)

6 (unchanged)

(4 + 6) / 2= 5

7 (value at location 5)This is too big, need to search lower

3 4 (unchanged)

4 (one less than old middleLocation)

(4 + 4) / 2= 8 / 2= 4

5 Found it!!!

Page 15: Searching & Sorting

Binary Search

Searching for 6:Location: 1 2 3 4 5 6 Value:

Step minLocation maxLocation middleLocation middleValue1 1 6 (1 + 6) / 2

= 3.5 = 3

4 (value at location 3)too small, need to search higher

Page 16: Searching & Sorting

Binary Search

Searching for 6:Location: 1 2 3 4 5 6 Value:

Step minLocation maxLocation middleLocation middleValue1 1 6 (1 + 6) / 2

= 3.5 = 3

4 (value at location 3)too small, need to search higher

2 4 (one more than old middleLocation)

6 (unchanged)

(4 + 6) / 2= 10 / 2= 5

7(value at location 5)too big, need to search lower

Page 17: Searching & Sorting

Binary Search

Searching for 6:Location: 1 2 3 4 5 6 Value:

Step minLocation maxLocation middleLocation middleValue1 1 6 (1 + 6) / 2

= 3.5 = 3

4 (value at location 3)too small, need to search higher

2 4 (one more than old middleLocation)

6 (unchanged)

(4 + 6) / 2= 10 / 2= 5

7(value at location 5)too big, need to search lower

Page 18: Searching & Sorting

Binary Search

Searching for 6:Location: 1 2 3 4 5 6 Value:

Step minLocation maxLocation middleLocation middleValue1 1 6 (1 + 6) / 2

= 3.5 = 3

4 (value at location 3)too small, need to search higher

2 4 (one more than old middleLocation)

6 (unchanged)

(4 + 6) / 2= 10 / 2= 5

7(value at location 5)too big, need to search lower

3 4 (unchanged)

4 (one less than old middleLocation)

(4 + 4) / 2= 8 / 2= 4

5 (value at location 3)too small, need to search higher

Page 19: Searching & Sorting

Binary Search

Searching for 6:Location: 1 2 3 4 5 6 Value:

Step minLocation maxLocation middleLocation middleValue1 1 6 (1 + 6) / 2

= 3.5 = 3

4 (value at location 3)too small, need to search higher

2 4 (one more than old middleLocation)

6 (unchanged)

(4 + 6) / 2= 10 / 2= 5

7(value at location 5)too big, need to search lower

3 4 (unchanged)

4 (one less than old middleLocation)

(4 + 4) / 2= 8 / 2= 4

5 (value at location 3)too small, need to search higher

Page 20: Searching & Sorting

Binary Search

Searching for 6:Location: 1 2 3 4 5 6 Value:

Step minLocation maxLocation middleLocation middleValue1 1 6 (1 + 6) / 2

= 3.5 = 3

4 (value at location 3)too small, need to search higher

2 4 (one more than old middleLocation)

6 (unchanged)

(4 + 6) / 2= 10 / 2= 5

7(value at location 5)too big, need to search lower

3 4 (unchanged)

4 (one less than old middleLocation)

(4 + 4) / 2= 8 / 2= 4

5 (value at location 3)too small, need to search higher

4 5 (one more than old middleLocation)

4(unchanged)

minLocation > maxLocation - we have nothing left to check - value is not there!

Page 21: Searching & Sorting

Basic Sorts

Page 22: Searching & Sorting

Sorting

• How do we sort?

Page 23: Searching & Sorting

Selection Sort

• A human algorithm:

Page 24: Searching & Sorting

Selection Sort

• In a computer:http://computerscience.chemeketa.edu/cs160Reader/Algorithms/SelectionSort2.html

Page 25: Searching & Sorting

Insertion Sort

• For a human: