Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Search and Sort Algorithm

The document discusses various algorithms for searching and sorting data structures. It covers linear and binary search for unsorted and sorted arrays. It also discusses searching binary trees, including binary search trees, using breadth-first traversal. For sorting, it mentions several algorithms like shell sort, merge sort, binary tree sort, radix sort, quick sort, and heap sort. It provides details on searching binary search trees and resolving collisions in hash tables using chaining or open addressing.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Search and Sort Algorithm

The document discusses various algorithms for searching and sorting data structures. It covers linear and binary search for unsorted and sorted arrays. It also discusses searching binary trees, including binary search trees, using breadth-first traversal. For sorting, it mentions several algorithms like shell sort, merge sort, binary tree sort, radix sort, quick sort, and heap sort. It provides details on searching binary search trees and resolving collisions in hash tables using chaining or open addressing.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Search & Sort

(2)
Searching Algorithms (2) • For example:
Linear • Unsorted array
Linear/sequential search search • O(n)

• For example:
Binary search Binary • Sorted array
search • O (log n)

Searching on Binary Tree

Metode Hash
Searching

• Store data in BST


Binary structure
Search a • Store data in binary tree
• Search a node in a binary search tree • The cost for:
binary tree • Search → O(log n)
tree
(BST) • Insert → O(log n)
• Remove → O(log n)
How to search a binary tree?
(1) Start at the root
(2) Search the tree level by level, until you
find the element you are searching for,
or you reach a leaf.

→ Breadth first traversal of binary tree

Performance?
How to search a binary tree?
• Depends on the tree implementation
• Adjacency matrix
O(V*V)
• Adjacency list
O(V+E)

→ O(n)
Is it better than
searching on a
linked-list?
Binary Search Trees (BSTs)

In a BST, the value


stored at the root of
a subtree is greater
than any value in its
left subtree and less
than any value in its
right subtree!
Binary Search Trees (BSTs)

Where is the
smallest element?
Ans: leftmost element

Where is the largest


element?
Ans: rightmost element
How to search a binary search tree?
(1) Start at the root
(2) Compare the value of
the item you are
searching for with
the value stored at
the root
(3) If the values are
equal, then item
found; otherwise, if it
is a leaf node, then
not found
How to search a binary search tree?
(4) If it is less than the value
stored at the root, then
search the left subtree
(5) If it is greater than the
value stored at the root,
then search the right
subtree
(6) Repeat steps 2-6 for the
root of the subtree chosen
in the previous step 4 or 5
How to search a binary search tree?

Is this better than searching


a linked list?

Yes !! ---> O(log n)


Remember some other operations on BST
• Insert
• Remove/Delete
• Delete a node with 0 child (leaf)
• Delete a node with 1 child
• Delete a node with 2 children
Balanced tree

Difference between
the height of the left
subtree and right
subtree is not more
than 1.
Insert and
remove nodes
in BST Unbalanced tree

changes the
Unbalanced
tree structure tree

This is just as
good as linked
list.
This is the
worse case of
BST search →
O(n)
• Balanced Binary Search Trees
• Example: AVL tree
• It is named for its inventors: G.M. Adelson-Velskii and E.M. Landis.
• To implement our AVL tree we need to keep track of a balance factor for each node in
the tree.
Search method: Hash
• We can also store and search data by using hash table
• Store data using hash table
• Search the data from the hash table
Before hash table
there is Direct-address Tables
• Direct-address Tables are ordinary arrays.
• Facilitate direct addressing.
• Element whose key is k is obtained by indexing into the kth
position of the array.

• Another example: make a big array and use phone numbers as


index in the array.
Before hash table
there is Direct-address Tables
• Benefits of direct-address Tables are:
• Searching in O(1)
• Insertion in O(1)
• Deletion in O(1)
• Some practical limitations:
• extra space required is huge,
• an integer in a programming language may not store n digits.

• Hashing is an improvement over Direct Access Table.


Hash Tables
• A hash table is a collection of items which are stored in such a
way as to make it easy to find them later.
• Each position of the hash table, often called a slot, can hold an
item and is named by an integer value starting at 0.
slot Named by
an integer
value

Data
item

• A hash function maps a big number or string to a small


integer that can be used as index in hash table.
• Example: Maps a 12 digits phone number into at most 2 digit
name/index
Hashing
0
U
(universe of keys)
h(k1)

h(k4)
K k1 k4
(actual k2 collision h(k2)=h(k5)
keys) k5
k3

h(k3)

m–1

Comp 122, Fall 2003


Hash Tables
• Example of hash function:
• The “remainder method,” simply takes an item and divides it by
the table size, returning the remainder as its hash value
h(x)= x mod 11

• A good hash function should have following properties


• Efficiently computable.
• Should uniformly distribute the keys (Each table position equally
likely for each key)
Issues with Hashing
• Multiple keys can hash to the
same slot – collisions are
possible.
• Design hash functions
such that collisions are
minimized.
• But avoiding collisions is
impossible.
• Design collision-
resolution techniques.
Methods of Resolution
• Chaining: 0

• Store all elements that hash to the same slot k1 k4

in a linked list. k5 k2 k6

• Store a pointer to the head of the linked list in k7 k3


the hash table slot. k8

• Open Addressing:
m–1

• All elements stored in hash table itself.


• When collisions occur, use a systematic
(consistent) procedure to store elements in
free slots of the table.
Collision Resolution by Chaining
0
U
(universe of keys)
H(k1)=H(k4)
X
k1
k4
K
(actual k2 k6 X
k5 H(k2)=H(k5)=H(k6)
keys)
k8 k7
k3 X
H(k3)=H(k7)
H(k8)
m–1
Collision Resolution by Chaining
0
U
(universe of keys)
k1 k4

k1
k4
K
(actual k2 k6
keys) k5 k5 k2 k6
k8 k7
k3
k7 k3

k8
m–1
• Chained-Hash-Search (T, k)
• Search an element with key k in list T[H(k)].
• Worst-case complexity – proportional to length of list.
• Worst-case complexity: O(n) + time to compute H(k).
Searching in hash table
Sorting Algorithms (2)

Metode Shell
Bubble sort
Metode Merge

Metode Binary Tree


Insertion sort
Metode Radix

Metode Quick
Selection sort
Metode Heap
Sort method: Shell
• It first sorts elements that are far apart from each other and
successively reduces the interval between the elements to
be sorted.
Sort method:
Merge
Sort method:
Binary search
tree
Sort method:
Radix
Sort method:
Quick
Sort method: Heap
• Heap is a special tree-based data
structure.
• A binary tree is said to follow a
heap data structure if
• it is a complete binary tree
• all nodes in the tree follow the
property that they are
• greater than their children i.e. the
largest element is at the root and
• both its children and smaller than
the root and so on.
“Heapify” a tree
• Starting from a complete binary
tree, we can modify it to
become a Max-Heap by running
a function called heapify on all
the non-leaf elements of the
heap.

You might also like