## Algorithm Analysis

A blog post of about 100 words on algorithm efficiency. The blog post is about “An analysis of the efficiency and correctness of an algorithm”. The instruction sheet explains everything and has the specific blog post topic that needs to be written about.

CMP4272 Data Structures and Algorithms UG1

Final blog post: Algorithm analysis

Write a blog post analysing the efficiency and correctness of an algorithm.

This could become the post you submit for the fourth part of the coursework, “An analysis of the efficiency and correctness of an algorithm”. You should choose either a standard algorithm, or an algorithm you have written yourself.

Blog post topic:

• Analyse the linear search algorithm.

◦ Briefly explain the problem of searching for an index, and how the algorithm works.

◦ Show code for the algorithm – this can be found in the Week 8 self-study material.

◦ Analyse the efficiency – see the previous page. For more marks, consider the separate cases when the list does contain the target, and when it doesn’t.

◦ Analyse the correctness – this is partly done in Q1b from the Week 13 exercises.

▪ Identify the loop invariant.

▪ How does the invariant guarantee the algorithm finds the first index of the target, in case it occurs more than once?

▪ What is the loop variant?

◦ Compare with the binary search algorithm.

A good blog post for the assessment should include:

• A brief explanation of what problem the algorithm solves, and how the algorithm works.

• An implementation of the algorithm, including code.

• An analysis of the algorithm’s efficiency:

◦ Measure the running time for a range of input sizes.

◦ Determine the algorithm’s complexity in ‘big O’ notation, by static analysis.

◦ Explain the average case, and the best and worst cases.

• An analysis of the algorithm’s correctness:

◦ Specify the algorithm’s postcondition, and preconditions (if any).

◦ Specify any invariants.

◦ Specify any variants required to show the algorithm terminates.

• A comparison with an alternative algorithm:

◦ Which is faster, and why?

◦ Does one algorithm have a lower complexity?

An excellent blog post should also include:

• An analysis for various kinds of inputs – e.g. if the input is a list, does it matter whether the list is random, ordered, contains duplicates, etc.?

• Code used to analyse the algorithm, which your explanations should refer to. (Code should be included to help your explanation – not just for the sake of it.)

You are encouraged to read and refer to information and code from other sources. Other sources are especially likely to help you find code, and information about the efficiency, complexity and correctness of standard algorithms. However, information from other sources must be referenced appropriately. This doesn’t need to be a formal referencing style (e.g. Harvard) but it should be completely clear from your blog post which information or code came from another source, and what source it came from.

Only use information from trustworthy sources – Wikipedia is actually good for data structures and algorithms, and textbooks are very likely to be correct, but e.g. answers on StackOverflow may contain errors.

• Do the analysis before you start writing the blog post.

• You can use the framework code from the Week 12 exercises to do a dynamic analysis of the efficiency; https://dsaa.werp.site/post/exercises-algorithm-efficiency/

• Use assertions and tracing to analyse correctness, but not when analysing the efficiency – they will make the algorithm slower.

• If you need to assert something about part of a list, use slices (e.g. lst[:i] or lst[i:]).

• Trace the algorithm to help find invariants and variants, by writing print(locals()) before a loop and at the end of the loop body.

• The loop variant is not necessary a variable, it could be e.g. the difference between two variables, or the length of a list.

Your blog post should be about 1000 words long.