Data Structures, Search and Sort Algorithms: Kar-Hai Chu Karhai@hawaii - Edu
Data Structures, Search and Sort Algorithms: Kar-Hai Chu Karhai@hawaii - Edu
Data Structures, Search and Sort Algorithms: Kar-Hai Chu Karhai@hawaii - Edu
Algorithms
Kar-Hai Chu
[email protected]
Data structures
Storage
Insertion, deletion
Searching
Sorting
Big O
Stacks
LIFO
Push, pop
O(1) operations
Linked lists v. Arrays
Linked lists:
– Resizable
– Insertion/deletion
Arrays:
– Faster index
– O(1) lookup
– Preset size
Hash tables
O(n2)
Algorithm:
– Find the minimum value
– Swap with 1st position value
– Repeat with 2nd position down
Insertion sort :-)
O(n2)
O(1) space
Great with small number of elements
(becomes relevant later)
Algorithm:
– Move element from unsorted to sorted list
Bubble sort :-(
O(n2)
Algorithm:
– Iterate through each n, and sort with n+1
element
Maybe go n-1 steps every iteration?
Great for big numbers, bad for small
Totally useless?
Merge sort :-)
O(nlogn)
Requires O(n) extra space
Parallelizable
Algorithm:
– Break list into 2 sublists
– Sort sublist
– Merge
Quick sort :-)
Average O(nlogn), worst O(n2)
O(n) extra space (can optimized for O(logn))
Algorithm:
– pick a pivot
– put all x < pivot in less, all x > pivot in more
– Concat and recurse through less, pivot, and more
Advantages also based on caching, registry
(single pivot comparison)
Variations: use fat pivot
Linear search :-(
O(n)
Examines every item
Binary search :-)
O(nlogn)
Algorithm:
– Build a heap
– deleteMax (or Min) repeatedly
O(1) overhead
Why bother?
B: B1 B: B2 B: B3
A: A1 +3 -2 +2
A: A2 -1 0 +4
A: A3 -4 -3 +1
Alpha-beta pruning - pick a bag!
– ordering
Useful
http://www.cs.pitt.edu/~kirk/cs1501/anim
ations/Sort3.html