# Searching Sorting Comp Sci 4 Searching Sorting 21

- Slides: 18

Searching & Sorting Comp. Sci 4 Searching & Sorting 21. 1

The Plan v v v Searching Sorting Java Context Comp. Sci 4 Searching & Sorting 21. 2

Search (Retrieval) v v v “Looking it up” One of most fundamental operations Without computer q q v Indexes Tables of content Card Catalogue Reference books Fundamental part of many computer algorithms Comp. Sci 4 Searching & Sorting 21. 3

Linear (Sequential) Search v v v Plod through material, one item at a time Always works Can be slow Sometimes the only way Phone Book Example q q v 660 -6567 Whose number is it? (How could this be done faster? ) Comp. Sci 4 Searching & Sorting 21. 4

Binary Search Often can do better than linear search: v Phone Book again (Predates Computer!) q q v v v Find midpoint Decide before or after (or direct hit) Discard half of uncertainty Repeat until there Fast! (Don’t even need computer!) What does it require (why not use all the time)? How man extra steps if double sized book? Comp. Sci 4 Searching & Sorting 21. 5

Hashing A way of storing info so we can go directly there to retrieve v Mail boxes in a mail room (know exactly where number 33 is. ) v Hashing is a way of transforming some part of info to allow such straight-forward storage v What to use for students in classroom q Age? Last name? SSN? Comp. Sci 4 Searching & Sorting 21. 6

Hashing v v Use extra space to allow for faster operation Collision Handling q q What to do if two different items map to the same bin? Many different solutions… Comp. Sci 4 Searching & Sorting 21. 7

Search Performance v Linear Search (brute force, plodding) q v ~N Binary Search (telephone book) q v Proportional to amount Proportional to log of amount ~log(N) Hashing (go directly to …) q Comp. Sci 4 Independent of amount! ~constant Searching & Sorting 21. 8

Sorting (Motivation) Fundamental part of many algorithms and procedures v Required before other operations possible q v Often a user requirement for manual use q v v E. g. , binary search E. g. , phone book, dictionary, directory, index… Get lower Postal Rates if sorted by Zip Code Implicit requirement for “orderly” operation Comp. Sci 4 Searching & Sorting 21. 9

Selection Sort q q q q q Comp. Sci 4 N items in an array named Data [2|4|7|3|1|8|5] Find smallest of elements 0 thru N-1 of Data Interchange this with 1 st element of array Data [_|_|_|_] Find smallest of elements 1 thru N-1 of Data Interchange this with 2 nd element of array Data [_|_|_|_]. . . Find smallest of elements k-1 thru N-1 of Data Interchange this with kth element of array Data [_|_|_|_|_|_|_] [_|_|_|_] Done when k-1 = N-1 [_|_|_|_] Searching & Sorting 21. 10

Selection Sort q q q q q Comp. Sci 4 N items in an array named Data [2|4|7|3|1|8|5] Find smallest of elements 0 thru N-1 of Data Interchange this with 1 st element of array Data [1|4|7|3|2|8|5] Find smallest of elements 1 thru N-1 of Data Interchange this with 2 nd element of array Data [1|2|7|3|4|8|5]. . . Find smallest of elements k-1 thru N-1 of Data Interchange this with kth element of array Data [1|2|3|7|4|8|5] [1|2|3|4|7|8|5] [1|2|3|4|5|8|7] Done when k-1 = N-1 [1|2|3|4|5|7|8] Searching & Sorting 21. 11

Selection Sort Performance (N 2) v v Assume there are N items to be sorted Notice that with each pass we have to make N comparisons q v Notice that we have to make N passes q v (actually N-1) Therefore requires N x N comparisons q v (actually N/2 on average) (actually N*(N-1)/2 ) Performance proportional to N 2 or ~N 2 Comp. Sci 4 Searching & Sorting 21. 12

Other Simple Sorts (N 2) v 2 More simple sorts like Selection Sort Insertion Sort q Bubble Sort q v All 3 have common properties Easy to write q Fairly slow for large amounts of data q Comp. Sci 4 Searching & Sorting 21. 13

Industrial Quality Sorts v v Can do much better than simple sorts Selection Sort is often used q q v Divide and conquer strategy Partitions data into two parts Partitions each of these parts into subparts Etc. Performance greatly improved over previous q Can handle any real job Comp. Sci 4 Searching & Sorting 21. 14

Other Fast Sorts v Merge Sort q q v v Stable Requires extra memory Binary Tree Sort Heap Sort Shell Sort Bucket Sort q q Can be extremely fast under special circumstances (Analogy to Hashing) Comp. Sci 4 Searching & Sorting 21. 15

Sort Performance v Slowest: ~N 2 q q v Very Fast: ~N log N q q v Quick. Sort, Binary Tree Sort Merge Sort, Heap Sort Quite Fast q v Selection Sort Insertion Sort , Bubble Sort Shell Sort Fastest (limited situations): ~N q Bucket Sort Comp. Sci 4 Searching & Sorting 21. 16

Java Context (writing your own? ) Don’t need to write your own -- Java includes: v For Collections static void sort(List list) q stable static int binary. Search(List list, Object key) v For Arrays (? ? = int, double, …, and Object) static void sort(? ? [ ] a) q Uses quicksort (not stable) static int binary. Search( ? ? [ ] a, ? ? key) Comp. Sci 4 Searching & Sorting 21. 17

Practice 1. 2. 3. In a class you design, create an array of ints, initialise with some numeric data and print it out. Utilize the sort method found in the Arrays class. Sort your array and print it out again. Write your own version of selection sort and add it to your class. Compare to the sort of the Arrays class. Comp. Sci 4 Searching & Sorting 21. 18

- Comp sci 1102
- Comp sci 1027
- Recursion ap csa
- Zjhz
- Cs 110 northwestern
- Searching and sorting in java
- Pengertian searching dalam struktur data
- Big o java
- Searching and sorting arrays in c++
- Searching and sorting in java
- Big o java
- Pengertian searching dalam struktur data
- Internal and external sort
- Sci 201
- Math sci
- Med sci itr
- What movie genre is stranger things
- Sci
- Sci communication