Data Structures and Algorithms Coursework 2020-21
Data Structures and Algorithms Coursework 2020-21
Date: 29-01-21
Submission Date
Presentation:
All Checklist Items Followed (0 or 10) 10
Brief Precise Summary of how the Selected Search Algorithm (Written; Max 100 Words):
Make this box bigger if you need to, but do not exceed the word limit
Best
Average
Worst
References:
Make this box bigger as needed
Precise Description of Shell Sort Algorithm and the Role of the Gap Sequence (Written; Max 100 Words):
Make this box bigger as needed, but do not exceed the word limit
Modified Shell Sort Function for Recording Metrics of Interest - Comparisons and Element Movements (Code):
Make this box bigger as needed
Test Harness for Generating Test Data, Running Shell Sort Function, and Aggregating Metrics of Interest (Code):
Make this box bigger as needed
References:
In this task, you are to perform an empirical comparison of the efficacy of three different hash functions (selecting
from midsquare, modulus, folding, truncation, and Fibonacci) to assess their usefulness for transforming integer keys
to array indices in a hash table with the objective of minimising collisions and clustering. This task is a little more
open-ended than the previous two, however you should build a simulation that involves the generation of keys
(random and sequential) and measure the degree of clustering (i.e., how evenly spread the keys are in the table) under
different load factors, how often collisions occur, and also evaluate the avalanche property and other desirable features
of a hash function, such as having a surjective relationship with the target array and being fast to compute. For each,
do some background reading to understand how they may be applied in practice (e.g., folding may, in practice,
involve grouping digits before summing), and state any assumptions you make.
Brief and Precise Description and Hash Tables and the Role of Hash Functions (Written, Max 100 Words):
Make this box bigger if you need to, but do not exceed the word limit
Description and Static Demonstration of Hash Functions Selected (Written, Equations, Max 50 Words):
Make this box bigger if you need to, but do not exceed the word limit
Implementation of Chosen Hash Functions as Function accepting a Key and Returning an Index (Code):
Make this box bigger if you need to, but do not exceed the word limit
Test Harness for Evaluating Hash Functions with Random and Sequential Keys (Code):
Make this box bigger as needed
Evaluation of Relative Efficacy of Hash Functions Compared (Written, Max 100 Words):
Make this box bigger as needed
References:
Make this box bigger as needed
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Title: Jump Search Function
% Author: Ian van der Linde
% Rev. Date: 27-01-21
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
numComparisons = 0;
N = length(V);
numComparisons = numComparisons + 1;
if(V(1)==target)
currentIndex = 1;
return;
end % end if
jumpSize = ceil(sqrt(N));
% Jumping Part
numComparisons = numComparisons + 1;
if(V(currentIndex)>target)
break;
end % end if
numComparisons = numComparisons + 1;
if(V(linearSearchIndex)==target)
currentIndex = linearSearchIndex;
return;
end % end if
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Title: Jump Search Test Harness
% Author: Ian van der Linde
% Rev. Date: 03-02-20
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
maxArraySize = 1024;
for N = 1:maxArraySize
array = 1:N;
best_comparisons(N) = min(comparisons);
average_comparisons(N) = mean(comparisons);
worst_comparisons(N) = max(comparisons);
clear comparisons;
% Plot Observed
figure;
plot(1:maxArraySize, best_comparisons,'g','LineWidth',3);hold on; % best
plot(1:maxArraySize, average_comparisons,'y','LineWidth',3); % avg
plot(1:maxArraySize, worst_comparisons,'r','LineWidth',3); % worst
% Plot Expected
% Annotate Chart
xlim([0 maxArraySize]);
ylim([0 max(worst_comparisons)]);
axis square;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Title: Shell (1959) Gap Generator Function
% Author: Ian van der Linde, PhD
% Date: 29-01-21
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function h = shell1959(N,k)
h = floor(N/2^k);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Title: Shell (1959) Gap Generator Function Test
% Author: Ian van der Linde, PhD
% Date: 29-01-21
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all;
close all;
clc;
while(h>1)
h = shell1959(N,k);
disp(h);
k = k + 1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Title: Shell Sort Function
% Author: Ian van der Linde, PhD
% Rev Date: 29-01-21
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
while(gap>0)
end %while
Worst-case Time
Author Sequence Formula Terms
Complexity
N N N
Shell (1959) ⌊ ⌋ ⌊ ⌋ , ⌊ ⌋ ,…,1 O(N 2 )
2k 2 4
2
Hibbard (1963) 2k −1 1,3,7,15,31,63 , … O(N ) 3
Papernov &
Stasevich (1965) 1 ,then 2k +1 1,3,5,9,17,33,65 , … O(N 1.5 )
3k −1 N 3
Knuth (1973) <⌈ ⌉ 1,4,13,40,121 ,… O(N ) 2
2 3
∏ aq , where
I
a 1=0
Incerpi & 5 q +1
Sedgewick (1985) {
a q=min n ∈ N :n ≥ ()
2 }
, ∀ p :0 ≤ p<q ⇒ gcd1,3,7,21,48,112
( a p , n ) =1 ,… O¿
1
I ={ 0 ≤ q<r| q ≠ ( r 2+r )−k }
2
r =⌊ √ 2 k + √ 2 k ⌋
4
Sedgewick (1982) 1 ,then 4 k +3 ( 2k ) +1 1,8,23,77,281 ,… O(N 3 )
Sedgewick (1986)
{ (
8 ( 2k )−6 2
)
9 2 k −2 2 +1 k mod 2=0
k+1
( )+1 k mod 2=1
2
1,5,19,41,109 , …
4
O(N 3 )
5 hk−1 5N 5 5N
Gonnet & Baeza-
Yates (1991)
h k =max ⌊ { 11 }
⌋ ,1 ; h0=N ⌊
11
⌋,⌊ ⌊
11 11
⌋ ⌋ ,…,1 unknown
k−1
1 9 9
Tokuda (1992) ⌈ 9
5 4 (() ) −4 ⌉ ; where h <N
4 k
1,4,9,20,46,103 , … unknown