TCS 1011 - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

TCS 1011

Description:

Suppose we have 1000 records but 8 digit keys then perhaps: ... As an example, suppose we have an 5 digit integer as a key and that there ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 28
Provided by: raym190
Category:

less

Transcript and Presenter's Notes

Title: TCS 1011


1
TCS 1011 Tutorial 13 Week XIII
2
Question
What is the range of h(i) i 11 ?
  • 0 11
  • 1 11
  • 0 10
  • 0 11

Answer C. 0 10
3
Assume that collisions are resolved using linear
probing
4
L.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
5
L.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
6
L.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
7
L.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
8
L.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
9
Solution
26, 42, 5, 44, 92, 59, 40, 36, 12, 60, 80
44
12
80
36
26
5
92
59
40
42
60
10
Assume that collisions are resolved using
chaining
11
Chaining
26, 42, 5, 44, 92, 59, 40, 36, 12, 60, 80
i 26 H(i) i 11 H(26) 26 11 H(26) 4
12
Chaining
26, 42, 5, 44, 92, 59, 40, 36, 12, 60, 80
i 42 H(i) i 11 H(42) 42 11 H(42) 9
13
Chaining
26, 42, 5, 44, 92, 59, 40, 36, 12, 60, 80
i 5 H(i) i 11 H(5) 5 11 H(5) 5
14
Chaining
26, 42, 5, 44, 92, 59, 40, 36, 12, 60, 80
i 44 H(i) i 11 H(44) 44 11 H(44) 0
15
Chaining
26, 42, 5, 44, 92, 59, 40, 36, 12, 60, 80
i 92 H(i) i 11 H(92) 92 11 H(92) 4
16
Chaining
26, 42, 5, 44, 92, 59, 40, 36, 12, 60, 80
17
Question
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

18
Insertion Sort
Input sequence 4 , 6 , 1 , 3 , 2
Insertion Sort Java Applet http//web.engr.oregons
tate.edu/minoura/cs162/javaProgs/sort/InsertSort.
html
19
Question 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
20
Selection 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
21
Selection 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
22
Hints 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

23
Solution 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 /
24
Different 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.

25
Different 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.

26
Different 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.

27
Different 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.
Write a Comment
User Comments (0)
About PowerShow.com