searching & sorting
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 PresentationTRANSCRIPT
![Page 1: Searching & Sorting](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/1.jpg)
Searching & Sorting
![Page 2: Searching & Sorting](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/2.jpg)
Algorithms
• Step by step recipe to do a task…
![Page 3: Searching & Sorting](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/3.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/4.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/5.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/6.jpg)
Linear Search Algorithm
• In pseudocode:
![Page 7: Searching & Sorting](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/7.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/8.jpg)
Algorithm
![Page 9: Searching & Sorting](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/9.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/10.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/11.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/12.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/13.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/14.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/15.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/16.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/17.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/18.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/19.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/20.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/21.jpg)
Basic Sorts
![Page 22: Searching & Sorting](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/22.jpg)
Sorting
• How do we sort?
![Page 23: Searching & Sorting](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/23.jpg)
Selection Sort
• A human algorithm:
![Page 24: Searching & Sorting](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/24.jpg)
Selection Sort
• In a computer:http://computerscience.chemeketa.edu/cs160Reader/Algorithms/SelectionSort2.html
![Page 25: Searching & Sorting](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/25.jpg)
Insertion Sort
• For a human:
![Page 26: Searching & Sorting](https://reader035.vdocument.in/reader035/viewer/2022062218/56816143550346895dd0bb55/html5/thumbnails/26.jpg)
Selection Sort
• In a computer:http://computerscience.chemeketa.edu/cs160Reader/Algorithms/InsertionSort2.html