Presentation is loading. Please wait.

Presentation is loading. Please wait.

Searching & Sorting.

Similar presentations


Presentation on theme: "Searching & Sorting."— Presentation transcript:

1 Searching & Sorting

2 Algorithms Step by step recipe to do a task…

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

4 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

5 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….

6 Linear Search Algorithm
In pseudocode:

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

8 Algorithm

9 Binary Search Searching for 5: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 3 4

10 Binary Search Searching for 5: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 3 4

11 Binary Search Searching for 5: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 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

12 Binary Search Searching for 5: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 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

13 Binary Search Searching for 5: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 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!!!

14 Binary Search Searching for 5: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 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!!!

15 Binary Search Searching for 6: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 3 4 (value at location 3) too small, need to search higher

16 Binary Search Searching for 6: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 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

17 Binary Search Searching for 6: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 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

18 Binary Search Searching for 6: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 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

19 Binary Search Searching for 6: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 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

20 Binary Search Searching for 6: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 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!

21 Basic Sorts

22 Sorting How do we sort?

23 Selection Sort A human algorithm:

24 Selection Sort In a computer:

25 Insertion Sort For a human:

26 Selection Sort In a computer:


Download ppt "Searching & Sorting."

Similar presentations


Ads by Google