Searching & Sorting
Algorithms Step by step recipe to do a task…
Algorithms Step by step recipe to do a task where: – Operations are computable – Operations are unambiguous – Operations are well ordered – Finite number of operations
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….
Linear Search Algorithm In pseudocode:
Binary Search Method #2 – Binary Search – Pick middle of remaining search space – Too high? Eliminate middle and above – Too low? Eliminate middle and below
Algorithm
Binary Search Searching for 5: StepminLocationmaxLocationmiddleLocationmiddleValue 116(1 + 6) / 2 = 3.5 = 3 4
Binary Search Searching for 5: StepminLocationmaxLocationmiddleLocationmiddleValue 116(1 + 6) / 2 = 3.5 = 3 4
Binary Search Searching for 5: StepminLocationmaxLocationmiddleLocationmiddleValue 116 (1 + 6) / 2 = 3.5 = (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 5 7 (value at location 5) This is too big, need to search lower
Binary Search Searching for 5: StepminLocationmaxLocationmiddleLocationmiddleValue 116 (1 + 6) / 2 = 3.5 = (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 5 7 (value at location 5) This is too big, need to search lower
Binary Search Searching for 5: StepminLocationmaxLocationmiddleLocationmiddleValue 116 (1 + 6) / 2 = 3.5 = (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 5 7 (value at location 5) This is too big, need to search lower 34 (unchanged) 4 (one less than old middleLocation) (4 + 4) / 2 = 8 / 2 = 4 5 Found it!!!
Binary Search Searching for 5: StepminLocationmaxLocationmiddleLocationmiddleValue 116 (1 + 6) / 2 = 3.5 = (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 5 7 (value at location 5) This is too big, need to search lower 34 (unchanged) 4 (one less than old middleLocation) (4 + 4) / 2 = 8 / 2 = 4 5 Found it!!!
Binary Search Searching for 6: StepminLocationmaxLocationmiddleLocationmiddleValue 116(1 + 6) / 2 = 3.5 = 3 4 (value at location 3) too small, need to search higher
Binary Search Searching for 6: StepminLocationmaxLocationmiddleLocationmiddleValue 116 (1 + 6) / 2 = 3.5 = 3 4 (value at location 3) too small, need to search higher 24 (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 10 / 2 = 5 7 (value at location 5) too big, need to search lower
Binary Search Searching for 6: StepminLocationmaxLocationmiddleLocationmiddleValue 116 (1 + 6) / 2 = 3.5 = 3 4 (value at location 3) too small, need to search higher 24 (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 10 / 2 = 5 7 (value at location 5) too big, need to search lower
Binary Search Searching for 6: StepminLocationmaxLocationmiddleLocationmiddleValue 116 (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 34 (unchanged) 4 (one less than old middleLocation) (4 + 4) / 2 = 8 / 2 = 4 5 (value at location 3) too small, need to search higher
Binary Search Searching for 6: StepminLocationmaxLocationmiddleLocationmiddleValue 116 (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 34 (unchanged) 4 (one less than old middleLocation) (4 + 4) / 2 = 8 / 2 = 4 5 (value at location 3) too small, need to search higher
Binary Search Searching for 6: StepminLocationmaxLocationmiddleLocationmiddleValue 116 (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 45 (one more than old middleLocation) 4 (unchanged) minLocation > maxLocation - we have nothing left to check - value is not there!
Basic Sorts
Sorting How do we sort?
Selection Sort A human algorithm:
Selection Sort In a computer:
Insertion Sort For a human:
Selection Sort In a computer: