Вы находитесь на странице: 1из 6

D. S. R.

Murthy DSA-5 Basic Data Structures (Contd…) 1

Priority Queues
PQ-Sort

Selection Sort

Insertion Sort

Heap Data Structure


Insertion (Up-Heap Bubbling)

Deletion (Down-Heap Bubbling)

Dictionaries and Hash Tables


Hash Functions
Compression Maps
Collision Handling
Open Addressing Methods
Linear Probing

Quadratic Probing

Double Hashing
D. S. R. Murthy DSA-5 Basic Data Structures (Contd…) 2

Priority Queues
Key
Object assigned to an element as a specific attribute
for that element.
Used to identify rank / weight of the element.
Assigned to an element by user or application.

Total order relation


Must satisfy the properties:
Reflexive property
k ≤ k.
Antisymmetric property
If k1 ≤ k2 and k2 ≤ k1, then k1 = k2.
Transitive property
If k1 ≤ k2 and k2 ≤ k3, then k1 ≤ k3.

Smallest key (kmin)


Key kmin ≤ k, for any other key k.

Priority queue
Container of elements.
Each element has an associated key provided at the
time of insertion.
Elements are inserted and removed based on the
keys.

Sorting
Arranging the given list in ascending / descending
order, with respect to a given key field.
D. S. R. Murthy DSA-5 Basic Data Structures (Contd…) 3

PQ-Sort (Sorting using Priority Queue)


Given
Collection C of n elements.
Priority Queue P.
1. Put the elements of C into an initially empty priority
queue P.
2. Extract the elements from P in non-decreasing
order, putting them back into C in order.

Using a Priority Queue implemented with an Unordered


Sequence – Selection Sort
Unsorted Pass
i ki 1 2 3 4 5 6 7 8 9
1 42 11 11 11 11 11 11 11 11 11
2 23 23 23 23 23 23 23 23 23 23
3 74 74 74 36 36 36 36 36 36 36
4 11 42 42 42 42 42 42 42 42 42
5 65 65 65 65 65 58 58 58 58 58
6 58 58 58 58 58 65 65 65 65 65
7 94 94 94 94 94 94 94 74 74 74
8 36 36 36 74 74 74 74 94 87 87
9 99 99 99 99 99 99 99 99 99 94
10 87 87 87 87 87 87 87 87 94 99

Using a Priority Queue implemented with a Sorted


Sequence – Insertion Sort
Unsorted Pass number (i) Sorted
j kj 1 2 3 4 5 6 7 8 9 10 -
1 42 42 23 23 11 11 11 11 11 11 11 11
2 23 23 42 42 23 23 23 23 23 23 23 23
3 74 74 74 74 42 42 42 42 36 36 36 36
4 11 11 11 11 74 65 58 58 42 42 42 42
5 65 65 65 65 65 74 65 65 58 58 58 58
6 58 58 58 58 58 58 74 74 65 65 65 65
7 94 94 94 94 94 94 94 94 74 74 74 74
8 36 36 36 36 36 36 36 36 94 94 87 87
9 99 99 99 99 99 99 99 99 99 99 94 94
10 87 87 87 87 87 87 87 87 87 87 99 99
D. S. R. Murthy DSA-5 Basic Data Structures (Contd…) 4

Heap Data Structure

Heap
Kj ≤ Ki for 2 ≤ j ≤ n and i = j/2.

Heap sort
Construction phase (Up-Heap Bubbling)

Traversal phase (Down-Heap Bubbling)

Construction Phase
Inserting a new element in a tree structure.

Trace of the Construction of the heap for the elements


42, 23, 74, 11, 65, 58, 94, 36, 99, 87.

Traversal Phase
Trace of the traversal phase of the heap for the elements
42, 23, 74, 11, 65, 58, 94, 36, 99, 87.
D. S. R. Murthy DSA-5 Basic Data Structures (Contd…) 5

Dictionaries and Hash Tables

Computer Dictionary
Similar to Paper Dictionary of words.
Users can assign keys to elements and then use later
to look up or remove elements.

Hash Function h(k)


Maps each key k in the dictionary to an integer in
the range [0, N – 1]
N capacity of array

Hash code
Mapping the key k to an integer.

Compression map
Mapping the hash code to an integer within the
range of indices of an array.

Compression Map Methods


Division Method
h(k) = |k| mod N
N Prime no.

Multiply Add and Divide (MAD) Method


h(k) = |ak + b| mod N
N Prime no.
a, b Nonnegative integers (randomly chosen)
a mod N ≠ 0
D. S. R. Murthy DSA-5 Basic Data Structures (Contd…) 6

Collision Handling Schemes

Linear Probing
To insert an item (k, e) into a slot
If A[i] is already occupied, where i = h(k)
then try next at A[(i+1) mod N].
If A[(i+1) mod N] is also occupied,
then try A[(i+2) mod N]
and so on
until an empty slot in A is found
and insert the item (k, e).

Quadratic Probing
Involves iteratively trying the slots
A[(i + f(j)) mod N], for j = 0, 1, 2, …,
where f(j) = j2
until finding an empty slot.

Double Hashing
Choose a secondary hash function h’.
if h maps some key k to a slot A[i], with i = h(k),
that is already occupied
then iteratively try the slots
A[(i +f(j) mod N] next, for j = 1, 2, 3, …,
where f(j) = j.h’(k).

The secondary hash function is not allowed to


evaluate to zero.

Common choice is h’(k) = q – (k mod q)


for some prime no. q < prime no. N.

Вам также может понравиться