Professional Documents
Culture Documents
Hashing Hashing
def qsort
Bruno MARTIN, return self if empty?
University of Nice - Sophia Antipolis select { |x| x < first }.qsort
mailto:Bruno.Martin@unice.fr + select { |x| x==first}
http://deptinfo.unice.fr/∼bmartin/mathmods.html + select { |x| x > first }.qsort
end
Searching Searching
Hashing Hashing
Scans (index i) from the left until you find an elt ≥ v (a[i] ≥ v )
Scans (index j) from the right until you find an elt ≤ v (a[j] ≤ v )
Both elements are obviously out of place: swap a[i] and a[j]
Continue until the scan pointers cross (j ≤ i)
Exchange v (a[right]) with the element a[i]
Best seen from
until j<=i do /Users/bmartin/Documents/Enseignement/Mathmods/Programs
i+=1 until self[i]>=v #scans for i:self[i]>=v with trirapide!
j-=1 until self[j]<=v #scans for j:self[j]<=v
if i<=j
self.swap!(i,j) #exchange both elements
i+=1; j-=1 #modify indexes:clean recursion
end
end
Searching Searching
Hashing Hashing
Searching Searching
Hashing Hashing
CN : average number�of comparisons for sorting N elements: Intuition for the performance of quick sort
CN = N + 1 + N1 N k=1 (Ck−1 + CN−k ) Quicksort running time depends on whether the partitioning is
N + 1 comparisons during the two inner whiles N − 1 + 2
balanced
(2 when i and j cross)
Plus the average number of comparisons on the two sub-arrays The worst-case partitioning occurs when the partitioning produces
((C0 + CN−1 ) + (C1 + CN−2 ) + ... + (CN−1 + C0 ))/N one region with 1 element and one with N − 1 elements: O(N 2 )
2 �N The best-case partitioning occurs when the partitioning produces
By symmetry : CN = N + 1 + N k=1 Ck−1
substract NCN − (N − 1)CN−1 two regions with N/2 elements (CN = N + 2CN/2 ): O(N log N)
worst-case ^ best-case
NCN = (N + 1)CN−1 + 2N
N | N N ^
divide both side by N(N + 1) to obtain the recurrence : / \ | / \ |
�1 N+1 1 N-1 | N/2 N/2 N |
CN CN−1 2 CN−2 2 2 C2 / \ N / \ / \ log N
= + = + + = ... = +2
N +1 N N +1 N −1 N N +1 3 k 1 N-2 | N/4 N/4 N/4 N/4 N |
k=4
�N �N / \ | . |
CN 1 1
Approximation : N+1 ≈2 k=1 k ≈2 1 x dx ≈ 2 ln N 1 N-3 | . |
... | 1 1 ... 1 1 N |
CN ≈ 2NlnN ≈ 2Nln(2)Log (N) ≈ 1.38NLogN / \ | v
1 1 v
Bruno MARTIN, University of Nice - Sophia Antipolis mailto:Bruno.Martin@unice.fr
Sorting, Searching and Haching
http://deptinfo.unice.fr/ ∼bmartin/mathmods.html Bruno MARTIN, University of Nice - Sophia Antipolis mailto:Bruno.Martin@unice.fr
Sorting, Searching and Haching
http://deptinfo.unice.fr/ ∼bmartin/mathmods.html
Searching Searching
Hashing Hashing
Searching Searching
Hashing Hashing
Searching Searching
Hashing Hashing
Sequential Searching in an Array is O(N) An Elementary Searching Algorithm : the Binary Search
1
average=mean= sum of all the entries
number of entries
Bruno MARTIN, University of Nice - Sophia Antipolis mailto:Bruno.Martin@unice.fr
Sorting, Searching and Haching
http://deptinfo.unice.fr/ ∼bmartin/mathmods.html Bruno MARTIN, University of Nice - Sophia Antipolis mailto:Bruno.Martin@unice.fr
Sorting, Searching and Haching
http://deptinfo.unice.fr/ ∼bmartin/mathmods.html
Searching Searching
Hashing Hashing
Searching Searching
Hashing Hashing
k − A[left].key
position = left + (right − left)
A[right].key − A[left].key
Searching Searching
Hashing Hashing
So interpolation search should be used for large sets in Basic operations: insert, remove, search
applications where comparisons are particularly expensive or for
external methods where access costs are high
Searching Searching
Hashing Hashing
Transform Keys into Integers in [[0, M − 1]] How to Handle the Collision Process
Searching Searching
Hashing Hashing
Searching a record in a Hash Table with linked lists Alternative proof for successful search
1 �N � �N 1 �N
� �N 1
� �
Operations of insertion and removal of records in a Hash (1) = N i=1 1 + j=i+1 E [Xij ] = N i=1 1 + j=i+1 M =
Table become linked list operations �N � � � �
1 1 N N
1+ (N − i) = 1 + NM i=1 N − i=1 i =
NM � i=1 �
1+ 1
NM N 2 − N(N+1)
2 = 1 + N−1
2M
Searching Searching
Hashing Hashing
Searching Searching
Hashing Hashing
This hashing works because it guarantees that when you search for Hashing is a classical problem in CS: various algorithms have been
a particular key you look at every key that hashes to the same studied and are widely used
table address There are many empirical and analytic results that make utility of
In linear probing when the table begins to fill up, you also look to Hashing evident for a broad variety of applications
other keys: 2 different collision sets may be stuck together: Hashing is prefered to binary tree searches for many
clustering problem applications because it is simple to implement and can provide
Linear probing is very slow when tables are almost full because of very fast constant searching times when space is available for a
the clustering problem large enough table
And when the table is full you cannot continue to use it
Hashing in Ruby
zip=Hash.new
zip={"06000" => "Nice", "06100" => "Nice", "06110" => "Le Cannet",
"06130" => "Grasse", "06140" => "Coursegoules", "06140" => "Tourrettes
sur Loup", "06140" => "Vence", "06190" => "Rocquebrune Cap Martin",
"06200" => "Nice", "06230" => "Saint Jean Cap Ferrat", "06230" =>
"Villefranche sur Mer"}