Data Structures Search and Sort Algorithms KarHai Chu

  • Slides: 20
Download presentation
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 n Storage n Insertion, deletion n Searching n Sorting n Big O

Data structures n Storage n Insertion, deletion n Searching n Sorting n Big O

Stacks n LIFO n Push, pop n O(1) operations

Stacks n LIFO n Push, pop n O(1) operations

Linked lists v. Arrays n Linked lists: – Resizable – Insertion/deletion n Arrays: –

Linked lists v. Arrays n Linked lists: – Resizable – Insertion/deletion n Arrays: – Faster index – O(1) lookup – Preset size

Hash tables n Keys and values n O(1) lookup n Hash function – Good

Hash tables n Keys and values n O(1) lookup n Hash function – Good v fast n Clustering n Databases

Selection sort : -( n O(n 2) n Algorithm: – Find the minimum value

Selection sort : -( n O(n 2) n Algorithm: – Find the minimum value – Swap with 1 st position value – Repeat with 2 nd position down

Insertion sort : -) n O(n 2) n O(1) space n Great with small

Insertion sort : -) n O(n 2) n O(1) space n Great with small number of elements (becomes relevant later) n Algorithm: – Move element from unsorted to sorted list

Bubble sort : -( n O(n 2) n Algorithm: – Iterate through each n,

Bubble sort : -( n O(n 2) n Algorithm: – Iterate through each n, and sort with n+1 element n Maybe go n-1 steps every iteration? n Great for big numbers, bad for small n Totally useless?

Merge sort : -) n O(nlogn) n Requires O(n) extra space n Parallelizable n

Merge sort : -) n O(nlogn) n Requires O(n) extra space n Parallelizable n Algorithm: – Break list into 2 sublists – Sort sublist – Merge

Quick sort : -) Average O(nlogn), worst O(n 2) n O(n) extra space (can

Quick sort : -) Average O(nlogn), worst O(n 2) n O(n) extra space (can optimized for O(logn)) n Algorithm: n – 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) n Variations: use fat pivot n

Linear search : -( n O(n) n Examines every item

Linear search : -( n O(n) n Examines every item

Binary search : -) n Requires n O(log a sorted list n) n Divide

Binary search : -) n Requires n O(log a sorted list n) n Divide and conquer

Trees n Almost like linked lists! n Traverse: Pre-order v. Post-order v. Inorder n

Trees n Almost like linked lists! n Traverse: Pre-order v. Post-order v. Inorder n Node, edge, sibling/parent/child, leaf

Binary trees n 0, 1, or 2 children per node n Binary Search Tree:

Binary trees n 0, 1, or 2 children per node n Binary Search Tree: a binary tree where node. left_child < node. value and node. right_child >= node. value

Balanced binary trees n Minimizes the level of nodes n Compared with “bad” binary

Balanced binary trees n Minimizes the level of nodes n Compared with “bad” binary tree? n Advantages: – Lookup, insertion, removal: O(log n) n Disadvantages: – Overhead to maintain balance

Heaps (binary) n Complete: all leafs are at n or n-1, toward the left

Heaps (binary) n Complete: all leafs are at n or n-1, toward the left n Node. value >= child. value n In binary min/max heap – Insert = O(logn). . add to bottom, bubble-up – delete. Max = O(logn). . Move last to root and bubble-down

Heapsort n O(nlogn) n Algorithm: – Build a heap – delete. Max (or Min)

Heapsort n O(nlogn) n Algorithm: – Build a heap – delete. Max (or Min) repeatedly n O(1) overhead

Why bother? n Tries (say trees) – Position determines the key – Great for

Why bother? n Tries (say trees) – Position determines the key – Great for lots of short words – Prefix matching n But. . – Long strings. . – Complex algorithms

Chess! n n Minimax: B: B 1 B: B 2 B: B 3 A:

Chess! n n Minimax: B: B 1 B: B 2 B: B 3 A: A 1 +3 -2 +2 A: A 2 -1 0 +4 A: A 3 -4 -3 +1 Alpha-beta pruning - pick a bag! – ordering

Useful n http: //www. cs. pitt. edu/~kirk/cs 1501/ani mations/Sort 3. html

Useful n http: //www. cs. pitt. edu/~kirk/cs 1501/ani mations/Sort 3. html