CS 310 Advanced Data Structures and Algorithms Searching June 14, - - PowerPoint PPT Presentation

cs 310 advanced data structures and algorithms
SMART_READER_LITE
LIVE PREVIEW

CS 310 Advanced Data Structures and Algorithms Searching June 14, - - PowerPoint PPT Presentation

CS 310 Advanced Data Structures and Algorithms Searching June 14, 2018 Mohammad Hadian Advanced Data Structures and Algorithms June 14, 2018 1 / 6 Searching Linear Search a sequential search is made over all items one by one. Every item


slide-1
SLIDE 1

CS 310 – Advanced Data Structures and Algorithms

Searching June 14, 2018

Mohammad Hadian Advanced Data Structures and Algorithms June 14, 2018 1 / 6

slide-2
SLIDE 2

Searching

Linear Search

a sequential search is made over all items one by one. Every item is checked and if a match is found then that particular item is returned.

Hash Table

O(1) for lookup

Binary Search

Binary search finds the position of a specified value within a sorted array. Every iteration eliminates half of the remaining possibilities. This makes binary searches very efficient. Worst case performance: O(logn)

Mohammad Hadian Advanced Data Structures and Algorithms June 14, 2018 2 / 6

slide-3
SLIDE 3

Binary Search

public int binarySearch(int[] A, int key) { int start = 0; int end = A.length - 1; while (start <= end) { int mid = start + (end - start)/2; if (key == A[mid]) { return mid; } if (key < A[mid]) { end = mid - 1; } else { start = mid + 1; } } return -1; }

Mohammad Hadian Advanced Data Structures and Algorithms June 14, 2018 3 / 6

slide-4
SLIDE 4

Tries

The word trie comes from retrieval. Also known as Prefix/Radix/Digital Tree Trie is relating to Most-significant-first radix sort. Using trie, search complexities can be brought to optimal limit (key length) The root represents an empty string Every node of trie consists of multiple branches Each branch represents a possible character of keys.

Mohammad Hadian Advanced Data Structures and Algorithms June 14, 2018 4 / 6

slide-5
SLIDE 5

Tries

From wikipedia

Mohammad Hadian Advanced Data Structures and Algorithms June 14, 2018 5 / 6

slide-6
SLIDE 6

Tries

Why use tries if hash tables can do the same? Hash tables can only find in a dictionary words that match exactly with the single word that we are finding The trie allow us to find words that have a single character different, a prefix in common, a character missing

Mohammad Hadian Advanced Data Structures and Algorithms June 14, 2018 6 / 6