Title: TCS 1011
1TCS 1011 Tutorial 13 Week XIII
2Question
What is the range of h(i) i 11 ?
Answer C. 0 10
3Assume that collisions are resolved using linear
probing
4L.Probing
26, 42, 5, 44, 92, 59, 40, 36, 12, 60, 80
i 26 H(i) i 11 H(26) 26 11 H(26) 4
26
5L.Probing
26, 42, 5, 44, 92, 59, 40, 36, 12, 60, 80
i 42 H(i) i 11 H(42) 42 11 H(42) 9
26
42
6L.Probing
26, 42, 5, 44, 92, 59, 40, 36, 12, 60, 80
i 5 H(i) i 11 H(5) 5 11 H(5) 5
26
5
42
7L.Probing
26, 42, 5, 44, 92, 59, 40, 36, 12, 60, 80
i 44 H(i) i 11 H(44) 44 11 H(44) 0
44
26
5
42
8L.Probing
26, 42, 5, 44, 92, 59, 40, 36, 12, 60, 80
i 92 H(i) i 11 H(92) 92 11 H(92) 4
44
92s hashing index clashes with 26s
26
5
92
Solution Do a sequential search through the
table for an empty location ?92 is placed at
index 6.
42
9Solution
26, 42, 5, 44, 92, 59, 40, 36, 12, 60, 80
44
12
80
36
26
5
92
59
40
42
60
10Assume that collisions are resolved using
chaining
11Chaining
26, 42, 5, 44, 92, 59, 40, 36, 12, 60, 80
i 26 H(i) i 11 H(26) 26 11 H(26) 4
12Chaining
26, 42, 5, 44, 92, 59, 40, 36, 12, 60, 80
i 42 H(i) i 11 H(42) 42 11 H(42) 9
13Chaining
26, 42, 5, 44, 92, 59, 40, 36, 12, 60, 80
i 5 H(i) i 11 H(5) 5 11 H(5) 5
14Chaining
26, 42, 5, 44, 92, 59, 40, 36, 12, 60, 80
i 44 H(i) i 11 H(44) 44 11 H(44) 0
15Chaining
26, 42, 5, 44, 92, 59, 40, 36, 12, 60, 80
i 92 H(i) i 11 H(92) 92 11 H(92) 4
16Chaining
26, 42, 5, 44, 92, 59, 40, 36, 12, 60, 80
17Question
What are the other types of hashing functions
that you learnt in the lecture? Please name the
types and briefly explain.
- Identity
- Suppose memory was limitless then we could have
the simplest hash function of all   - index  key
- More details
- Truncation
- With truncation part of the key is ignored and
the remainder used directly. - More details
- Folding
- the key is partitioned and the parts combined in
some way (maybe by adding or - multiplying them) to obtain the index.
- More details
- Modular Arithmetic
- The key is converted into an integer, the
integer is then divided by the size of the index
range and the remainder is taken as the index
position of the record.                - index  key mod size
- More details
18Insertion Sort
Input sequence 4 , 6 , 1 , 3 , 2
Insertion Sort Java Applet http//web.engr.oregons
tate.edu/minoura/cs162/javaProgs/sort/InsertSort.
html
19Question 3 Complete Insertion Sort
include ltiostreamgt using namespace std define
MAX 5 void main() int AMAX,i,j,current
coutltlt"Enter "ltltMAXltlt" elements to be
sorted"ltltendl for (i0iltMAXi)
cingtgtAi for (i1iltMAXi) current
Ai ji while
((jgt0)(currentltAj-1)) Aj
Aj-1 j-- Aj current
coutltlt"The elements after
Insertion Sort"ltltendl for (i0iltMAXi)
coutltltAiltltendl
Q3a
Q3b
20Selection Sort
Searh for the smallest
STEP 1
5
4
3
2
1
0 1 2 3 4
- Smallest 1 (at index 4)
- Swap the value at index 4 (1) with value at index
0 (5)
1
4
3
2
5
0 1 2 3 4
21Selection Sort
Searh for the smallest
STEP 2
4
3
2
5
1
0 1 2 3 4
- Smallest 2 (at index 3)
- Swap the value at index 3 (1) with value at index
1 (4)
1
2
3
4
5
0 1 2 3 4
22Hints for Q4
- Create a function, recursiveSelectionSort().
- The parameters of the function are as followed
- int list
- To receive an integer list from the driver
programme. - int last
- To receive the last index of the array.
- int first
- To receive the first index of the array.
- Declare local variables
- int smallest
- To store the index of array which has the
smallest value. - int holdData
- To temporarily hold a data before performing
swapping. - Determine the condition of stopping recursively
invoking the function. - Assume the smallest value is at the first index
of the array. - Start searching through the smallest value in the
array. - Swap the smallest value with the first one.
- Invoke the same function again with following
arguments - list
23Solution for Q4
void recursiveSelectionSort (int list , int
last, int first) / Local Declarations /
int smallest int holdData /
Statements / if (firstgt last)
return smallest first for(int i
firstiltlasti) if(
listi lt listsmallest )
smallest i holdData
listfirst listfirst listsmallest
listsmallest holdData
recursiveSelectionSort(list , last , first1)
return / recursiveSelectionSort /
24Different Methods of Hashing
Identity
- Suppose memory was limitless then we could have
the simplest hash function of all   - index  key
- this would mean declaring an array of
elements and putting the keys in those elements
with the same index! - Â
e.g.,
1
2
3
4
5
Disadvantage For this to be a viable proposition
memory would have to be limitless (and wastable).
In practice you cannot afford such extravagance.
25Different Methods of Hashing
Truncation
- With truncation part of the key is ignored and
the remainder used directly. - Suppose in a certain situation there were just 6
keys and they - were    12302   12303   12305   12307  Â
12308Â Â Â Â 12309 - The hash function here could be
- Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â index last
digit of key - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â (or index
key mod 12300 ) - Of course the above hash function would not
work if the range of keys was - Â Â Â Â Â Â Â Â Â Â 2134Â Â Â 4134Â Â Â 5134Â Â Â 7134Â Â Â
9134 or        2560   4670   6124   8435  Â
9200 - Here you would require a hash function that
removed the last 3 digits. This is why it is
necessary for you to know something about the
distribution of actual keys before deciding on a
hash function.
26Different Methods of Hashing
Folding
- Here the key is partitioned and the parts
combined in some way (maybe by adding or
multiplying them) to obtain the index. Suppose we
have 1000 records but 8 digit keys then perhaps - The 8 digit key such as              Â
62538194Â Â Â Â Â Â Â Â Â Â Â Â Â Â 62538195 may be
partitioned into                625 381Â
94Â Â Â Â Â Â Â Â Â 625Â 381Â 95 the groups now
added                   1100                   Â
   1101 and the result truncated              Â
  100                         101 - Since all the information in the key is used in
generating the - index folding often achieves a better spread than
truncation just by itself.
27Different Methods of Hashing
Modular Arithmetic
- The key is converted into an integer, the integer
is then divided by the size of the index range
and the remainder is taken as the index position
of the record. - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
- index  key mod size
- As an example, suppose we have an 5 digit
integer as a key and that there - are 1000 records (and room for 1000
records)  - The hash function would then be  index  KEY
size - e.g., 12345 1000
- If we are very lucky! our keys might be such
that there is only 1 key that maps to each
index. Of course we might still have the
situation where two keys map to the same index
(e.g. 23456, 43456) - this is called a
COLLISION.