Академический Документы
Профессиональный Документы
Культура Документы
Use Hashing.
In addition:
choice of programming language
choice of compiler (even if the same language is used)
different C compilers may translate same program into different
sequences of machine code
In addition:
choice of programming language
choice of compiler (even if the same language is used)
different C compilers may translate same program into different
sequences of machine code
3000 f1
2000
1000 f2
Problem size n
125 500 1000 2000
3000 f1
2000
1000 f2
Problem size n
125 500 1000 2000
In general:
Analysis of running time as function of problem size.
Come up with general shape of curve (don’t care about
coefficients).
Kerstin Eder 8– ii University of Bristol December 2, 2003
What about different Algorithms?
Running times of different algorithms fall into different complexity
classes.
Each class is characterised by a different family of curves.
All curves in one class share same basic shape.
Shape is characterised by an equation (running time as function of
problem size).
Different curves in one family represent special cases of this
equation.
What does mean when we say “the running time of alg X is O(n2)”?
What does mean when we say “the running time of alg X is O(n2)”?
X runs in an amount of time no greater than n2 times some
coefficient, provided the problem size is big enough.
Goal:
– Be confident that you have a “feeling” for what O-notation
means.
– Know the limitations of O-notations.
Exponential algorithms take very long for all but small problems.
Quicksort algorithm
Some arrays take longer than others.
Time depends on how much the array is out of order at start.
Distinguish:
Average running time
Worst case
Best case
Operations on a table:
– Initialise to the empty table.
– Determine whether or not table is full.
– Retrieving, updating, inserting and deleting table entries.
(Typically no traversal operation!)
Key K: 25X3Z
ASCII: 0110010 0110101 1011000 0110011 1011010
Equivalent base 10 value: 13,534,370,266
Task 2: Convert any numeric key into an integer that identifies where
the corresponding data record is stored in the table, i.e. the table
index of the data record.
Key: 13,534,370,266
Hash function: h(K) = K mod 40
Hash address: 26 = h(13,534,370,266)
Folding method:
Divide key into sections and add these together.
K = 031 782, h(031 782) = 48
How to find an empty table entry in which to store the data record
with key K2, if the table location given by the hash address h(K2) is
already occupied by another data record with key K1?
Direct chaining:
– Allow more than one entry per page.
– All names starting with the same letter go into one page.
Open addressing:
– Allow only one entry per page (or a fixed number).
– If a page is already occupied (full), use the next free one.
#12
linked list of table elements
with same hash address
#13 133 93
#14 94 54 14
#15 135
#16
one-element list
hash addresses
Animation at:
http://swww.ee.uwa.edu.au/˜plsd210/ds/hash_tables.html
Aim
Efficient (constant time) access to data records over a long time of
insertions and deletions of data records.