final year project report -...

54
Henry Rapley, 2013/14 Final Year Project Report Full Unit - Final Report Sorting and Searching Testbed Henry Rapley A report submitted in part fulfilment of the degree of BSc (Hons) in Computer Science Supervisor: Hugh Shanahan Department of Computer Science Royal Holloway, University of London March 26, 2014

Upload: others

Post on 01-Jun-2020

27 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Henry Rapley, 2013/14

Final Year Project ReportFull Unit - Final Report

Sorting and Searching Testbed

Henry Rapley

A report submitted in part fulfilment of the degree of

BSc (Hons) in Computer Science

Supervisor: Hugh Shanahan

Department of Computer Science

Royal Holloway, University of London

March 26, 2014

Page 2: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

Chapter 1: Abstract

Sorting and searching through arrays can take a long time depending on the algorithms used.A sorting and searching testbed provides a comparison of these algorithms and shows howthey work through animations, which should help improve a person’s understanding of thealgorithms. This report presents a discussion of computational complexity before examiningmultiple sorting and searching algorithms. Of the sorting algorithms, quicksort is usuallythe fastest. For searching, binary search is optimal. The report also discusses softwareengineering techniques before presenting the development process for the final program.

1

Page 3: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

Word Count: 12821

The word count for this report has been obtained by running the report through detex, andprocessing the output with wc -w.

SVN Folder Structure

In the root of my SVN repository, there are two folders, project and tags. The project

folder is a working copy of the project, and includes reports, accompanying documents, testprograms, and the final program “Tout Suite”. The tags folder contains snapshots of theproject folder.

The project folder uses the folder structure below:

• .

• staging/ - folder containing tex files and associated files

• Documents/ - folder containing reports, manuals, and documentation

• program/ - root folder for the final program

• proofs/ - folder containing test programs

– algorithms/ - folder containing tests of algorithms

– gui/ - folder containing tests of GUI techniques

The final program can be run from a terminal by changing directory to the program folderand executing the command ‘python main.py’.

Unit tests for the final program code can be run by executing the following command:

python -m toutsuite.test.test_all

An installation manual and user manual for the final program is included in the Documents

folder, as well as a copy of the final report.

2

Page 4: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

Table of Contents

1 Abstract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

3 Complexity, NP Hardness, and Big O Notation . . . . . . . . . . . . . . . . . . . . . 6

3.1 Big-O Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

3.2 NP-Hardness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

3.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

4 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

4.1 Bubble Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

4.2 Quicksort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

4.3 Linear Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

4.4 Binary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

4.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

5 Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

5.1 What Are Design Patterns? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

5.2 Why Use Design Patterns? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

5.3 Creational Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

5.4 Structural Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

5.5 Behavioural Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

5.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

6 Software Engineering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

6.1 General Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

6.2 The Spiral Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

6.3 Agile Development and Extreme Programming . . . . . . . . . . . . . . . . . . . 23

6.4 Test Driven Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

3

Page 5: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

6.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

7 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

7.1 Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

7.2 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

7.3 Use Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

8 Program Specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

9 Program Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

9.1 GUI Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

9.2 GUI Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

9.3 Program Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

9.4 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

9.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

10 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

10.1 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

11 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

11.1 Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

12 Professional Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

12.1 Usability and Safety . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

12.2 Licensing and Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

12.3 Coding Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

12.4 Impact . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

13 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

4

Page 6: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

Chapter 2: Introduction

Sorting and searching algorithms are useful for organising and retrieving data in simpleand complex data structures. When using such algorithms, efficiency is of concern. Whenretrieving data, ideally, the algorithms used need to be reasonably fast and avoid wastingCPU time. Wasteful algorithms can result in programs becoming incredibly slow, to the pointof being unusable. A testbed, then, is useful for comparing different techniques for searchingsorting data sets.

When choosing which algorithms to use, it is worth considering how, exactly, a data structureis going to be used. In most cases we can assume that searching is going to happen a lotmore often than modifications to the data set. Considering this, it may be reasonable to usea sorting algorithm that is not most optimal, and rely on searching being fast. While optimalsolutions should always be the end goal, when developing any given application there maybe time constraints limiting the time available to implement optimal algorithms.

This project aims to implement, compare, and provide a useful overview of various search andsort algorithms. These algorithms will include Bubble Sort, QuickSort, and Binary Search.Many languages include implementations of these algorithms, however, custom implementa-tions will be needed to provide a proper discussion, as well as providing the opportunity toproperly animate the processes involved.

The sorting and searching testbed itself will be implemented in Python. Faster implemen-tations may be achievable with compiled languages, such as Java, C, and C++. However,Python allows for faster development, as compilation is not required.

5

Page 7: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

Chapter 3: Complexity, NP Hardness, and Big

O Notation

In computer science, complexity relates to the difficulty of a computational problem. Acomputational problem is one that a computer may be used to solve. The complexity of aproblem is determined by the computational resources required to solve it. This includesCPU time and memory. The time complexity of an algorithm can be defined using Big Onotation, and is estimed by counting the number of copy and compare operations in thealgorithm[7].

Being able to define the time complexity of an algorithm allows for optimisation. Algorithmswith a lower time complexity are more efficient. Different approaches to solving a problemcan be explored, and comparing the time complexity of these solutions we can determinewhich to use in practical applications.

Optimisation is important as computer resources have a finite limit. Programs should nottake up too many resources, as this can lead to the program running slowly, crashing, or evenslowing down the computer that the program is running on.

3.1 Big-O Notation

System performance between computers can vary greatly. Algorithms which run slowly onthe average computer will likely run faster on a more powerful server. Because of this, usingtime as a measurement of an algorithm’s performance is unreliable. Instead, we define theefficiency using Big-O notation. This is calculated by counting the number of copy andcompare operations in a given algorithm.

For positive-valued functions f and g, f(n) is O(g(n)) if there exist positive numbers c and Nsuch that f(n) ≤ cg(n) for all n ≥ N [4]. From this we can determine that, at most, f growsas fast as g.

When considering algorithms, g is a function defining the number of assignment and com-parison operations in the algorithm. Further, Big-O for a given algorithm f uses a simplifiedform. Only the terms with the largest growth rate are kept, and factors that do not dependon n are left out.

3.1.1 Example

Consider the following function, sumOf.

function sumOf( arr ) {

sum = 0;

for( i = 0; i < arr.length; i++ ) {

sum+= arr[i];

}

return sum;

}

6

Page 8: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

To begin with, there are two assignments which only occur once, sum and i. The loop isexecuted once for every item in the array arr, or n times. Within this loop, there are twoassignments for i and sum. In total, there are 2 + 2n assignment operations in the algorithm.Following the simplification rules mentioned above, we determine that the Big-O of sumOf isO(n).

This is a simple example. Complexity analysis becomes more difficult with nested loops andif statements[4].

3.2 NP-Hardness

NP is a class of problems in computational complexity theory which can be solved in poly-nomial time using non-deterministic algorithms. Non-deterministic algorithms can take adifferent set of steps for the same input, on any given run. Such algorithms make use ofguesses. NP-hard problems are as complex as the most complex problems in NP[4].

As an example of non-determinism, binary search can be implemented as a non-deterministicalgorithm. In binary search, the search term is checked against the item in the middle of thearray. If the items do not match, the same check is made within the first or second half ofthe array, and so on. The algorithm becomes polynomial if both halves are checked, creatinga decision tree. If we assume the array provided is already sorted, then a deterministicimplementation can be used. In fact, most NP problems can be solved using deterministicalgorithms[18].

An algorithm has a polynomial time complexity if it takes O(nk) steps to complete. Bubblesort has a complexity of O(n2), and quick sort has a complexity of O(n2) in the worst case,so they are both, at least potentially, polynomial algorithms.

3.3 Summary

Analysing the complexity of algorithms can help developers make a decision regarding when touse a given algorithm. The efficiency of an algorithm can change depending on the numberof elements in the structure being worked on. As such, algorithms with a more simpleimplementation may be permissible, or even preferable, in some cases. The final program forthis project should be useful in demonstrating the complexity of the algorithms used, andhow the time complexity changes based on their input.

7

Page 9: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

Chapter 4: Algorithms

The final program for this project will feature several algorithms. More specifically, sortingand searching algorithms. This chapter features discussions and analyses of these algorithms.

For any given problem, there are multiple solutions. When deciding which algorithm to use,considering the time complexity of the available algorithms is important. Using more efficientalgorithms allows developers to create programs which run in a reasonable time frame. Whileanalysing the time complexity of algorithms is a useful method of optimisation, there are otheroptions available, such as concurrency.

Featured in this project are the following algorithms:

• Bubble Sort

• Quick Sort

• Linear Search

• Binary Search

4.1 Bubble Sort

Bubble sort is a very simple sorting algorithm. The algorithm passes over the array, andcompares adjacent elements. If the elements are out of order, then the elements are swapped.This process is repeated until the elements no longer have to be swapped.

8

Page 10: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

4.1.1 Implementation

For this project, the following implementation of bubble sort will be used. This implemen-tation is adapted from code found on wikipedia (http://en.wikipedia.org/wiki/Bubble_sort).

def bubble sort(data):swapped = Truetmp = Noneprev = 0

while swapped:swapped = False

for i in range(1, len(data)):prev = i − 1

if data[prev] > data[i]:tmp = data[prev]data[prev] = data[i]data[i] = tmpswapped = True

return data

4.1.2 Demonstration

This section demonstrates how the algorithm works by showing the changes made on eachpass. Here, we will sort the array [5,1,4,3,2].

The algorithm features a while loop with a nested for loop. Before entering these loops, thealgorithm initialises three variables: swapped; tmp; prev. We use swapped to determine if anyelements were swapped while passing over the array. The tmp variable is used to temporarilystore an element while swapping elements. The prev variable is used to indicate the indexof the previous element of the array while passing over it.

First pass

The for loop starts and index 1, and prev is set to i - 1 so we can compare the currentelement with the one preceding it. So, in the first step, we have i = 1 and prev = 0. Thismeans we compare 5 and 1. Because 5 is bigger than 1, the algorithm first sets tmp to 5,sets element 0 to 1, sets element 1 to 5, and then sets swapped to True. The result is thefollowing transformation.

[5,1, 4, 3, 2]→ [1,5, 4, 3, 2]

As the for loop continues, we get the following transformations.

9

Page 11: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

[1,5,4, 3, 2]→ [1,4,5, 3, 2] (4.1)

[1, 4,5,3, 2]→ [1, 4,3,5, 2] (4.2)

[1, 4, 3,5,2]→ [1, 4, 3,2,5] (4.3)

As is shown, the number 5 has “bubbled” to the end of the array, hence the name for thealgorithm. Further, because swapped has been set to True, the while loop continues, so wemake another pass over the array.

Further Passes

The process works the same for successive passes, and results in the transformations shownbelow. Note that on the first step of the second pass, no changes are made, as 1 and 4 arein the correct order.

Second Pass

[1,4, 3, 2, 5]→ [1,4, 3, 2, 5] (4.4)

[1,4,3, 2, 5]→ [1,3,4, 2, 5] (4.5)

[1, 3,4,2, 5]→ [1, 3,2,4, 5] (4.6)

[1, 3, 2,4,5]→ [1, 3, 2,4,5] (4.7)

Third Pass

[1,3, 2, 4, 5]→ [1,3, 2, 4, 5] (4.8)

[1,3,2, 4, 5]→ [1,2,3, 4, 5] (4.9)

[1, 2,3,4, 5]→ [1, 2,3,4, 5] (4.10)

[1, 2, 3,4,5]→ [1, 2, 3,4,5] (4.11)

Fourth Pass

[1,2, 3, 4, 5]→ [1,2, 3, 4, 5] (4.12)

[1,2,3, 4, 5]→ [1,2,3, 4, 5] (4.13)

[1, 2,3,4, 5]→ [1, 2,3,4, 5] (4.14)

[1, 2, 3,4,5]→ [1, 2, 3,4,5] (4.15)

As no changes were made in the fourth pass, the array is sorted, so the algorithm finishes.

4.1.3 Complexity Analysis

In order to find the complexity of bubble sort, we can focus on the inner for loop of thealgorithm. Within the inner loop, there 4 assignments and 1 compare operation. So we havec = 5. The inner loop runs n - 1 times on each pass of the outer loop. The array is sortedafter n - 1 passes of the outer loop; other implementations of bubble sort involve replacing

10

Page 12: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

the while loop with a for loop which executes n - 1 times. Knowing this, we can calculatethe run time as follows.

c((n− 1) + (n− 2) + ... + 1)

Which is equivalent to

cn(n− 1)

2

and can be further reduced to

cn2 − n

2

Thus, the time complexity of bubble sort is O(n2). However, if the array is already sorted,then the time complexity of the algorithm is O(n), and would be the best case. In general,we want to know the average case, which is O(n2).

4.2 Quicksort

Quicksort is more complicated in its operation. First, the algorithm chooses a pivot, usuallyan element in the middle of the array. Then the algorithm creates two partitions of the array,one containing elements lower than the pivot, and the other containing elements larger thanthe pivot. After creating these partitions, the algorithm performs the same operations onthe partitions using recursion, and returns the results along with the pivot, assembled intoan array as partition 1 + pivot + partition 2.

4.2.1 Implementation

For this project, the following implementation of quicksort will be used. This implementationis adapted from code found on wikipedia (http://en.wikipedia.org/wiki/Quicksort).

def quicksort(data):l = len(data)

if l <= 1:return data

pivot = data.pop(l / 2)less = []more = []

for n in data:if n <= pivot:

less.append(n)else:

more.append(n)

11

Page 13: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

return quicksort(less) + [pivot] + quicksort(more)

4.2.2 Demonstration

This section demonstrates how the algorithm works by showing the changes made on eachpass. Here, we will sort the array [5,1,4,3,2].

First, the algorithm chooses a pivot in the middle of the array. Element array length / 2 ischosen, so pivot = 4. The pivot is removed from the array. Next, two arrays are created,less and more. The algorithm iterates through the original array, placing each item in adifferent partition. If the item is smaller than the pivot, it is placed in less. If the item islarger than the pivot, it is placed in more.

So, the first pass of the algorithm produces the following changes.

step data less more0 5, 1, 3, 21 1, 3, 2 52 3, 2 1 53 2 1, 3 54 1, 3, 2 5

The algorithm then applies itself to the arrays less and more. The results of the recursionis concatenated along with the pivot before being returned.

Algorithm being applied to partition [1, 3, 2]. pivot = 3.

step data less more0 1, 21 2 12 1, 2

Algorithm being applied to partition [1, 2]. pivot = 2.

step data less more0 11 1

Algorithm being applied to partition [1]. The partition length is ≤ 1 so the algorithmreturns the input unmodified. Parent call of the algorithm then concatenates this with itsown pivot, 2, and applies the algorithm to the partition []. As this partition also has nomore than 1 element, we get the partition back and it is concatenated with the array in whatis now the current run of the algorithm, leaving us with the array [1, 2]. This array isreturned, and concatenated with the parent pivot, 3, giving us [1, 2, 3]. The algorithm isthen applied to the partition [], which is returned unmodified and concatenated, resultingin no changes.

12

Page 14: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

The array [1, 2, 3] is returned to the parent call. It is concatenated with the pivot, 4, givingus [1, 2, 3, 4]. The algorithm is applied to the partition [5], which is returned unmodifiedand concatenated. The algorithm finally completes with the array [1, 2, 3, 4, 5].

4.2.3 Complexity Analysis

Analysing quicksort is more complicated than analysing bubble sort, as recursion is used onpartitions that can vary in size based on the distribution of the values. To begin with, wecan evaluate the steps taken over a single run of the algorithm for one partition.

For the for loop, there are 4 assignments and 1 comparison. Here we have a constant numberof operations per run, 5. Within the for loop, there are 2 assignments and 1 comparisonon every iteration. The loop runs n times. We can refer to the number of operations in thefor loop as c. So, for one run of the algorithm, there are 5 + cn operations, which gives usa time complexity of O(n).

Next we can determine the number of operations performed when applying recursion. If thealgorithm splits the array into two partitions of size 0 and n - 1 on every call, then we havethe worst case. In this case, the algorithm recurses n - 1 times. With this, we have thefollowing equation for the number of operations performed.

5 + cn + cn(n− 1)

2

This equivalent to

5 + cn + cn2 − n

2

which leaves us with a time complexity of O(n2) in the worst case. From this, we determinethat the time complexity of quicksort is based on the depth of the call tree.

In the best case, the algorithm creates partitions of almost equal size. This would result ina call tree with a depth of log2 n. As each call has a time complexity of O(n), the total timecomplexity in the best case would be O(n log n).

On average, the algorithm may consistently choose a pivot which is in the middle 50% of thevalues in the array. In this case, the algorithm creates partitions containing between 25%and 75% of the elements in the array. This would result in a call tree with a depth no greaterthan log4/3 n, leaving us with a time complexity of O(n log n).

So, in the worst case, quicksort has a time complexity of O(n2). In the best and averagecases, the algorithm has a time complexity of O(n log n).

4.3 Linear Search

Linear search is a very simple algorithm. The algorithm iterates through every element ofthe array in order, and compares it against the search term. When a match is found, theindex of the element is returned. If the search term is not found in the array, -1 is returned.

13

Page 15: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

4.3.1 Implementation

As the algorithm is very simple, implementing it is very easy. The following implementationis used in this project.

def linear search(data, item):for i in range(0, len(data)):

if data[i] == item:return i

return −1

4.3.2 Demonstration

As mentioned above, the operation of the algorithm is simple. Searching for 5 in the array[1, 2, 3, 4, 5] results in the following comparisons being made.

step array find index value found0 1, 2, 3, 4, 5 5 0 1 False1 1, 2, 3, 4, 5 5 1 2 False2 1, 2, 3, 4, 5 5 2 3 False3 1, 2, 3, 4, 5 5 3 4 False4 1, 2, 3, 4, 5 5 4 5 True

As shown in the table above, the search term is found at index 4, so the algorithm returns 4.

4.3.3 Complexity Analysis

The linear search algorithm is a simple for loop containing 1 assignment and 1 comparisonoperation. So c = 2. The loop executes n times. So for each run of the algorithm, thereare, at most, cn operations performed. Thus, linear search has a time complexity of O(n).However, if the search term is located at the start of the array, then the time complexity isO(1).

4.4 Binary Search

Binary search assumes the given array is sorted. To begin with, the algorithm compares thesearch term with the element in the middle of the array. If the items match, the index isreturned. If the search term is smaller or larger than the middle element, then the sameoperations are performed in the lower or higher half of the array, and so on, until a matchis found. If no match is found, the algorithm returns -1. Otherwise, the index of the item isreturned.

14

Page 16: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

4.4.1 Implementation

For this project, the following implementation of binary search will be used. This implemen-tation is adapted from code found on wikipedia (http://en.wikipedia.org/wiki/Binary_search_algorithm).

def binary search(data, item):min = 0max = len(data) − 1mid = 0

while min < max:mid = min + ( ( max − min ) / 2 )

if data[mid] == item:return mid

if data[mid] < item:min = mid + 1

else:max = mid − 1

return −1

4.4.2 Demonstration

Searching for 5 in the array [1, 2, 3, 4, 5] results in the following comparisons beingmade.

step array find low index index high index value found0 1, 2, 3, 4, 5 5 0 2 4 3 False0 1, 2, 3, 4, 5 5 3 3 4 4 False0 1, 2, 3, 4, 5 5 4 4 4 5 True

As shown in the table above, the search term is found at index 4, so the algorithm returns 4.

4.4.3 Complexity Analysis

In the best case, the item is found in the middle of the array, and is found in the first try,meaning the algorithm would have a time complexity of O(1). Otherwise, the algorithmcontinues, only considering half of the working array after each comparison.

If the first attempt fails, the remaining n/2 items are considered. If the second attempt fails,the remaining n/4 items are considered. This continues until the search term is found, oruntil there are no items left to check against. The maximum number of iterations in thealgorithm is log2(n) + 1. Thus, binary search has a time complexity of O(log n).

15

Page 17: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

4.5 Summary

Figure 4.1: Sorting Algorithm Complexities

For the sorting algorithms, the time complexities are as follows:

• Bubble sort

– Best case: O(n)

– Average and worst case: O(n2)

• Quicksort

– Best and average case: O(n log n)

– Worst case: O(n2)

Figure 4.1 shows a graph comparing the average cases of the sorting algorithms when appliedto arrays of various lengths. From this, we can see that, in general, quicksort is faster.However, for smaller arrays, the difference is mostly negligible. So, in some cases, usingbubble sort may be fine.

The searching algorithms have the following time complexities:

• Linear search

– Best case: O(1)

– Average and worst case: O(n)

• Binary search

– Best case: O(1)

– Average and worst case: O(log n)

On average, binary search is faster, but only works with sorted arrays. Knowing this, devel-opers must consider whether or not it is worth sorting an array in a given program. We candetermine the need for a sorted array judging from how often a program will search the array

16

Page 18: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

for an item. If searching is frequent, then a decision should be made regarding which sortingalgorithm to use.

When considering which sorting algorithm to use, if choosing between quicksort and bubblesort, the size of the array should be taken into account. For small arrays, bubble sort issufficient. For larger arrays, quicksort is more beneficial.

Analysing the computational complexity of algorithms and problems grants developers greaterinsight. This in turn allows developers to create programs which are more efficient.

17

Page 19: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

Chapter 5: Design Patterns

5.1 What Are Design Patterns?

Design patterns are a set of principles and concepts that exist to provide guidance whendesigning and implementing applications and code. There are many different types of designpatterns, all appropriate for different problems. However, as an Object Oriented solution isrequired for the search and sort testbed, this document will mainly focus on Object Orienteddesign patterns.

Usually, design patterns are generic practices which can be applied to a broad spectrum ofproblems. Patterns are generally concerned with the structure of source code, and the way inwhich different components perform and interact. Patterns are usually defined as a simple setof rules or principles for a developer to follow[17]. Though we are focusing mainly on ObjectOriented designs, design patterns are usually generic enough to work with other paradigms.

This chapter will discuss three main types of design patterns. Namely:

• Creational Patterns

• Structural Patterns

• Behavioural Patterns

5.2 Why Use Design Patterns?

It is advisable to use design patterns as they can speed up the development of an applica-tion. Using tried and tested patterns allows developers to focus more on the content of theimplementation, without having to worry too much about the structure of the program[17].

Using known design patterns also makes code more readable for other developers who arefamiliar with the design patterns in use. This makes solutions easier to maintain, and alsomakes it easier to introduce new developers into existing projects.

5.3 Creational Patterns

Creational design patterns are concerned with object creation. In particular, these patternsdetermine what objects should be created, how they should be created, and when. Using thesedesign patterns allows developers to separate object creation from the rest of the system[17].

Creational patterns include the abstract factory pattern, the builder pattern, the factorymethod pattern, the prototype pattern, and the singleton pattern. All of these are concernedwith object creation.

18

Page 20: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

5.3.1 Object Factories

A factory is an object which is used specifically for creating other objects. Object factoriesfeature heavily in creational patterns, and also in Python. In particular, the Python package”twisted” uses object factories for creating protocol objects, as well as in various other areasof the library.

Abstracting object creation in this way allows a system to create the appropriate object, andin a way that is appropriate for the current system.

5.3.2 Abstract Factory and Factory Method Patterns

Abstract factory patterns is a pattern that provides a method of grouping related factoriesusing a single abstract object factory. An abstract factory object defines a type of factoryobject, which details the different methods available. A developer can then create a morespecific factory object using the abstract factory as an interface.

As a brief example of this, a system may have an abstract factory VehicleFactory, and anadditional object CarFactory. The CarFactory object would then be used to create differenttypes of cars.

The factory method pattern is fairly similar, but object creation is delegated to methods ina given object[8]. These methods can be overridden by subclasses.

5.3.3 Singleton Pattern

The factory patterns are often used in conjunction with the singleton pattern. The singletonpattern restricts object creation so that there is only ever one instance of a given class[8].When an appropriate constructor method is called, if an instance of the object already exists,the constructor simply returns a reference to the existing object. If the constructor methodis in the global scope, then the object can be considered global.

5.4 Structural Patterns

Structural design patterns are concerned with identifying relationships between different com-ponents in a system[5]. These patterns aid developers in defining interfaces for the compo-nents used.

5.4.1 Adapter Pattern

Adapters are interfaces for classes that allow classes which are not normally compatible witheach other to work together. Adapter classes map method calls on its own interface to callson the original class’ interface. These adapter classes also handle mapping data into formsmore appropriate for their destinations. The adapter pattern is concerned with identifyingwhere adapter classes are required, and implementing them[1].

Using adapter classes may be required if using libraries created by other developers to performtasks where the solution already exists. They can also be useful when there is an incompati-

19

Page 21: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

bility between interfaces within a system due to changes in the design of the system part waythrough development.

5.4.2 Bridge Pattern

The bridge pattern provides a means for the use of different implementations for the sameinterface. This is typically achieved using abstract classes, interfaces, inheritance, andadapters. Generally, this pattern involves creating ‘Abstractions‘, ‘Implementors‘, and ‘Con-crete Implementors‘[17].

Abstractions are classes that provide an interface to be used within a system, while allow-ing the implementation of the interfaces to be changed as required. Implementors definean interface for abstractions to make use of. Concrete implementors are classes which pro-vide an implementation for a given implementor. So, Abstractions make use of ConcreteImplementors, using interfaces defined by Implementors.

This can be useful, for instance, in a system that requires the use of a database for storingdata. Abstract interfaces could be used to perform operations on different types of databases,depending on the databases that are available in the system.

The bridge pattern is similar to the adapter pattern, and can involve using adapter classes.

5.5 Behavioural Patterns

Behavioural patterns relate to the way in which different components communicate witheach other. Such design patterns help in identifying and defining communication patterns ina system[5]. This provides some guidance on what interfaces to define, how to provide accessto different resources, and how to delegate processes.

5.5.1 Command Pattern

The command pattern provides a method of delegating work to different components at atime of a system’s choosing. Command systems involve the use of Command, Receiver,Invoker, and Client objects. A Command object holds a reference to a Receiver object, andinvokes a method on the Receiver. Receiver objects contain methods that perform tasks.Invoker objects hold references to Command objects, and invokes these Command objectswhen requested. Invokers may also record details regarding Command invocations. Clientobjects typically hold a reference to an Invoker object, and one or more Command objects.The Client object will pass Command objects to an Invoker, and determine when the Invokershould execute commands[1].

5.6 Summary

Creating a sorting and searching testbed with a GUI will involve creating several subsystems,with several objects. These subsystems and objects need to be able to work with each otherwith relative ease. Loosely coupled objects are most ideal in object oriented programming.

20

Page 22: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

This reduces the number of possible bugs, and ensures the code is easier to maintain andupdate.

The algorithms themselves should ideally run in their own threads, so the GUI can operatewithout locking up. However, the algorithms must be able to communicate with the GUI,as the animations are based on the algorithm’s operations. Achieving communication will bedone using event propagation, using designs similar to the command and observer patterns.

The Factory Method Pattern will be used in several places. Animations will be created basedon events received by the GUI. Loading and working with searching and sorting algorithmsworks slightly differently in each case, requiring different implementations for the threads andthe GUIs.

Further, the bridge or adapter patterns will be made use of to properly exchange search andsort data between the core application and its subsystems. Algorithms and their respectivethreads can be grouped together in collections, which would follow the composite pattern.Working with Tkinter will also require use of the adapter pattern to make sure the applicationcan properly interact any widgets used for the GUI.

21

Page 23: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

Chapter 6: Software Engineering

As this project involves developing software, it is worth making note of software engineeringpractices and the development process used while creating the final program.

6.1 General Process

In general, software engineering is split into several stages. These stages include writing aspecification, an analysis of the problem and program, program specification, program design,implementating, and testing. Traditionally, an linear approach was used, and this is oftenreferred to as the waterfall model. However, software engineering projects do not tend tostick to this, as it is more beneficial to be able to move between the different stages freely[11].

In traditional software engineering models, the client is only involved in the first stage ofsoftware engineering, and the final stage[3]. This means they are not involved in the actualdevelopment of the product. Thus, clients are often unable to give input during the morecritical stages of the project.

6.1.1 Analysis

System analysis aims to identify a system’s requirements, limitations, and goals[14]. Theremay be constraints that the program has to work within, mostly based on the target platformand intended usage. Programs are usually required to perform specific tasks, which can aidin identifying the end goals of the program. These things can be determined by questioningthe intended users, as well as analysing the problem itself.

6.1.2 Specification

Before working on a program, there needs to be a specification. Specifications define thebehaviour of the final product. The specification acts as a basis for all of the work thatfollows. When designing and implementing a program, the specification should be used as areference[11].

6.1.3 Design

Program design involves GUI design and code design. Before building a GUI, it is useful tohave some imagery which shows how the program should look. GUI designs are be modifiedas the development of the program progresses if the original designs become a hindrance[11].

For the code, we define what classes the program is going to have, how they behave, theinterfaces they expose, and the relationships between each class. To show the relationshipsbetween each class, UML class diagrams can be used[14]. More explicit designs for classescan be written out with mock function signatures and brief descriptions for functions andclasses.

22

Page 24: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

6.1.4 Implementation

Most of the time is spent on writing program code according to the design and specification[11].During the implementation phase, it may become apparent that changes need to be made tothe program designs[14].

6.1.5 Testing

Software testing provides evidence that the program performs as specified. Testing canalso reveal errors in the implementation which can then be fixed. This phase is incrediblyimportant in delivering a product that meets the requirements of the user[14].

6.2 The Spiral Model

Spiral software engineering involves continuously cycling through four major phases of de-velopment. Planning, risk analysis, engineering, and evaluation[11]. These stages are stillsequential, however, the client is provided with far greater opportunities to give input in thedevelopment of the product.

The planning stage involves analysis and specification writing, requirements are determinedduring this phase. Risk analysis involves researching different approaches to implementingthe product. Risk analysis is based on input from the client. The Engineering phase involvesprototyping the final product on each iteration until a final product is reached. The evaluationphase provides the client with an opportunity to give feedback on the progress made[11].

While the spiral model is more flexible than the waterfall model, movement from one phaseto the next is still fairly linear.

6.3 Agile Development and Extreme Programming

Agile development and extreme programming are both fairly similar to spiral development.The main differences are the frequency of meetings with the client, and the fluidity of the dif-ferent phases[13]. In these models, the client essentially acts as a member of the developmentteam, reviewing progress and providing feedback at regular intervals[3]. Agile developmentis fairly common in open source projects[13].

6.4 Test Driven Development

Test driven development, or TDD, is a programming practice used in the implementation andtesting stages of software engineering. Before any program code is written, automated testsare written. Specific test cases should be simple, only covering a small part of the system.Writing test cases provides a way of more efficiently determining how complete a system is,and aids in streamlining the development process[2].

Python provides a framework for writing automated test cases, unittest. I will be using

23

Page 25: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

this to write test cases for the final program. Unfortunately, automated test cases cannot bewritten for GUIs in Python. Because of this, I will still have to manually test the GUI.

6.5 Summary

Software engineering practices aim to ensure the delivery of a sufficient final product. Dueto the nature of the course that the final program of this project has been created for, agiledevelopment is a requirement. While working on this project, I have had weekly meetingswith a project supervisor. During meetings, progress has been reviewed, and feedback given.I will also be using test driven development, as Python provides a framework for this, and itshould make the development process easier to manage.

24

Page 26: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

Chapter 7: Analysis

The final program must be able to run algorithms while providing graphical animations ofthe steps taken by the algorithms. Such a system has two main parts. The algorithms, andthe GUI/animations. Both parts of the system will influence the design and implementationof the other.

7.1 Constraints

The program must be able to run on Linux. More specifically, the teaching environmentprovided by the Computer Science department at Royal Holloway, University of London. Across-platform solution is ideal to allow people not associated with the university to run theprogram on an operating system of their choosing.

The GUI must remain responsive at all times. This may be a concern when animating thealgorithms.

Python will be used for the final program. This provides a cross-platform environment forapplications, but comes with limitations of its own. Programs written in Python can runmore slowly than programs written in compiled languages such as Java and C++. TheGlobal Interpreter Lock can cause threaded solutions to run more slowly than expected.

Python includes a GUI toolkit called Tkinter. This toolkit is cross-platform, but must run inthe main thread of the application. As such, threading the animations will be more difficult,if not impossible. Therefore, pushing the algorithms themselves into their own threads shouldhelp keep the application responsive.

Screen size is limited, so the number of elements in the arrays operated on must also belimited. For this, a reasonable lower and upper bound must be established.

7.2 Algorithms

There are two different types of algorithm the program will work with: sorting and searchingalgorithms. To make things easier to implement and use, the program can be split intotwo different windows. One window showing the animations for the sorting algorithms, andanother for the searching algorithm animations.

Each algorithm works with an array of numbers. This can be represented using a bar graph,where each bar in the graph represents an item in the array. The differing numbers can berepresented by having bars of different heights.

The program should be able to display the amount of time taken for each algorithm tocomplete. Animating the process takes considerably longer than running the algorithmsthemselves. The time taken for the animations to complete can mostly be ignored, as thetestbed is mostly concerned with comparing and demonstrating the algorithms.

25

Page 27: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

7.3 Use Cases

This section details the following use cases for the sorting and searching testbed.

• Open Sorting Algorithms Window

• Open Searching Algorithms Window

• Switch To Sorting Algorithms Window

• Switch To Searching Algorithms Window

• Configure Array

• Configure Random Array

• Configure Array From File

• Configure Search Term

• Start Algorithms

• Pause Algorithms

• Stop Algorithms

7.3.1 Open Sorting Algorithms Window

1. Application opens splash window.

2. User clicks the “Sorting Algorithms” button.

3. Application closes splash window.

4. Application opens sorting algorithms window.

7.3.2 Open Searching Algorithm Window

1. Application opens splash window.

2. User clicks the “Searching Algorithms” button.

3. Application closes splash window.

4. Application opens searching algorithms window.

7.3.3 Switch To Sorting Algorithms Window

This use case assumes the searching algorithms window is currently open.

1. User clicks the “Sorting Algorithms” button.

2. Application stops any running algorithms and animations.

3. Application closes searching algorithms window.

4. Application opens sorting algorithms window.

26

Page 28: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

7.3.4 Switch To Searching Algorithms Window

This use case assumes the sorting algorithms window is currently open.

1. User clicks the “Searching Algorithms” button.

2. Application stops any running algorithms and animations.

3. Application closes sorting algorithms window.

4. Application opens searching algorithms window.

7.3.5 Configure Array

This use case assumes either the sorting algorithms window or searching algorithms is alreadyopen.

1. User clicks the “Configure” button.

2. Application opens the Configure window.

3. User edits the array in the Array text field.

4. User clicks the “Done” button.

5. Application processes the data in the Array text field and uses it to create an array.

6. Application closes the Configure window.

7. Application is updated with the new array.

Variation 1

1.1. In step 3, the user enters invalid data.

1.2. User clicks the “Done” button.

1.3. Application processes the data and catches an error.

1.4. Application opens an error dialog detailing the error.

1.5. User clicks the “Ok” button in the error dialog.

1.6. Application closes the error dialog.

1.7. Continue with step 3.

7.3.6 Configure Random Array

This use case assumes either the sorting algorithms window or searching algorithms is alreadyopen.

1. User clicks the “Configure” button.

27

Page 29: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

2. Application opens the Configure window.

3. User clicks the “Random” button.

4. Application opens the Random Array window.

5. User enters a number in the Array Size text field.

6. User clicks the “Ok” button.

7. Application generates a random array of the size defined by the user.

8. Application closes the Random Array window.

9. Application updates the Configure window with the random array.

10. User clicks the “Done” button.

11. Application processes the data in the Array text field and uses it to create an array.

12. Application closes the Configure window.

13. Application is updated with the new array.

Variation 1

1.1. In step 5, user enters a number lower than 10 or larger than 50 in the Array Size textfield.

1.2. User clicks the “Ok” button.

1.3. Application opens an error dialog with the message “Array size must be between 10and 50”.

1.4. User clicks the “Ok” button in the error dialog.

1.5. Application closes the error dialog.

1.6. Continue with step 5.

Variation 2

2.1. In step 5, user enters something other than a number in the Array Size text field.

2.2. User clicks the “Ok” button.

2.3. Application opens an error dialog with the message “Array size must be a number”.

2.4. User clicks the “Ok” button in the error dialog.

2.5. Application closes the error dialog.

2.6. Continue with step 5.

Variation 3

3.1. In step 5, user clicks the “Cancel” button.

3.2. Application closes the Random Array window.

3.3. Continue with step 10.

28

Page 30: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

7.3.7 Configure Array From File

This use case assumes either the sorting algorithms window or searching algorithms is alreadyopen.

1. User clicks the “Configure” button.

2. Application opens the Configure window.

3. User clicks the “Load” button.

4. Application opens a file selection dialog.

5. User chooses a file containing a JSON array of numbers.

6. User clicks the “Open” button in the file selection dialog.

7. Application closes the file selection dialog.

8. Application reads data from selected file.

9. Application uses array from the file to update the Configure window.

10. User clicks the “Done” button.

11. Application processes the data in the Array text field and uses it to create an array.

12. Application closes the Configure window.

13. Application is updated with the new array.

Variation 1

1.1. In step 5, user chooses a file containing a comma separated list of numbers.

1.2. Continue with step 6.

Variation 2

2.1. In step 5, user chooses a file containing fewer than 10 numbers or more than 50 numbers.

2.2. User clicks the “Open” button.

2.3. Application closes the file selection dialog.

2.4. Application reads data from selected file.

2.5. Application opens an error dialog with the message “Array must contain between 10and 50 numbers”.

2.6. User clicks the “Ok” button in the error dialog.

2.7. Application closes the error dialog.

2.8. Continue with step 3.

29

Page 31: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

Variation 3

3.1. In step 5, user chooses a file containing erroneous JSON.

3.2. User clicks the “Open” button.

3.3. Application closes the file selection dialog.

3.4. Application reads data from selected file.

3.5. Application opens an error dialog with the message “Error in JSON formatting”.

3.6. User clicks the “Ok” button in the error dialog.

3.7. Application closes the error dialog.

3.8. Continue with step 3.

7.3.8 Configure Search Term

This use case assumes either the sorting algorithms window or searching algorithms is alreadyopen.

1. User clicks the “Configure” button.

2. Application opens the Configure window.

3. User enters a number in the Find text area.

4. User clicks the “Done” button.

5. Application is updated with new search term.

6. Application closes the Configure window.

Variation 1

1.1. In step 3, user enters something other than a number in the Find text area.

1.2. User clicks the “Done” button.

1.3. Application opens error dialog with the message “Search term must be a number”.

1.4. User clicks the “Ok” button in the error dialog.

1.5. Application closes error dialog.

1.6. Application replaces data in the Find text area with the original search term.

1.7. Continue with step 3.

30

Page 32: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

7.3.9 Start Algorithms

This use case assumes either the sorting algorithms window or searching algorithms is alreadyopen.

1. User clicks the “Start” button.

2. Application disables the “Configure” button.

3. Application changes the “Start” button to a “Pause” button.

4. Application resets the algorithm graphs in the GUI.

5. Application starts the algorithm suites.

6. Application starts the animation process.

7. When all animations finish, the application enables the “Configure” button, and changesthe “Pause” button back to the “Start” button.

7.3.10 Pause Algorithms

This use case assumes either the sorting algorithms window or searching algorithms is alreadyopen, and the algorithms are already running (Start Algorithms step 5).

1. User clicks the “Pause” button.

2. Application pauses the algorithm suites.

3. Application pauses the animation process.

4. Application changes the “Pause” button to a “Start” button.

5. User clicks the “Start” button.

6. Continue with Start Algorithms step 5.

7.3.11 Stop Algorithms

This use case assumes either the sorting algorithms window or searching algorithms is alreadyopen, and the algorithms are already running (Start Algorithms step 5).

1. User clicks the “Stop” button.

2. Application stops the algorithm suites.

3. Application stops the animation process.

4. Application resets the algorithm graphs in the GUI.

5. Application enables the“Configure” button.

6. Application changes the “Pause” button to a “Start” button.

31

Page 33: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

Chapter 8: Program Specification

Tout Suite is a sorting and searching testbed. The program allows users to see how varioussearching and sorting algorithms work by providing graphs which animate the process.

The initial window in the program will show graphs for the sorting algorithms. There willalso be a few usable buttons on the screen. A “Searching Algorithms” button, a “Configure”button, a “Start” button, and a “Stop” button.

Pressing the “Searching Algorithms” button will close the sorting algorithms window andopen a searching algorithms window. The searching algorithms window will have a similarsetup, but instead of a “Searching Algorithms” button, there will be a “Sorting Algorithms”button, which will take the user back to the sorting algorithms screen.

Pressing the “Configure” button will open a dialogue window allowing the user to enter anarray for the algorithms to work with, or load an array from a file. The program will be ableto read JSON files, and possibly CSV files. If configuring the sorting algorithms, the userwill also be able to enter an item to search for in the array.

Pressing the “Start” button will start the algorithms and their respective animations. The“Start” button will also change to a “Pause” button. The “Pause” button will allow the userto temporarily halt the execution and animation of the algorithms.

While the algorithms are running, pressing the “Stop” button will stop the algorithms andrespective animations completely.

Before starting the algorithms, either before any run or after the algorithms have stoppedcompletely, the data will be reset to its original state.

Users will be able to input arrays with a minimum size of 10 elements, and a maximum sizeof 100 elements. Though this may change, depending on how much screen space the graphstake up. By default, the program will start with a randomly ordered array of ten items forthe sorting algorithms, and an ordered array of 10 items for the searching algorithms.

Each algorithm has a graph associated with it in the GUI. These graphs are simple bar charts,with each bar representing an element in the array. Each graph will have a label indicatingwhich algorithm it represents.

The graphs will be capable of performing different animations. These animations will include:

• select - Change the colour of an item.

• move - Move an item from one position in the array to another.

• swap - Swap two items in the array.

• highlight - Highlight a section of the array by drawing a coloured bar underneath it.

• deselect - Reverse the colour change performed by a select animation.

• unhighlight - Remove any current highlights.

For each graph, there may be only one section highlighted at a time.

32

Page 34: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

Chapter 9: Program Design

This chapter is concerned with the design of the final program. Both the GUI design and thedesign for the source code. Here we will mostly be focusing on the systems responsible fortesting and animating the algorithms.

9.1 GUI Design

The GUI will have four main windows. A splash window, a sorting algorithms window, asearching algorithms window, and a configuration window.

9.1.1 Splash Window

The splash window is a small window that is only visible when the program is initiallyopened. The purpose of the splash window is to introduce the program. There should bea short message briefly describing the program, and two buttons. One button to open thesorting algorithms window, and another to open the searching algorithms window.

9.1.2 Algorithms Window

Figure 9.1: Example main window

33

Page 35: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

The sorting algorithms window and searching algorithms window are fairly similar in appear-ance and function. There must be a label indicating the type of algorithm the window willshow animations for. Next to this label there must be a button allowing the use to change thetype of algorithm window the application is currently using. This button closes the currentwindow and opens the alternative algorithms window.

The algorithms window features a “Configure” button, which opens the configuration window.There is also a “Start” button, and a “Stop” button, used to start and stop the algorithms,respectively.

9.1.3 Configuration Window

The configuration window allows the user to set searching and sorting data for the program.The same configuration window is used regardless of whether the sorting algorithms windowor searching algorithms window is open. The same configuration data is also used for sortingand searching. However, the array given is sorted before being used with searching algorithms.

9.2 GUI Programming

This program requires a GUI. As such, it is helpful to know how to work with the GUI toolkitavailable in Python. Here, we are mainly concerned with layout management and animations.

Different computers often have screens of varying sizes. Users may want to resize the applica-tion window, or simply use the application with a full-screen view. To allow GUI applicationsto be used in this manner, the application has to be made resizable in the program code.Python comes with tkinter, which provides an interface for the Tk GUI toolkit[6]. Tkinterallows developers to create GUIs that can be resized by the user.

9.2.1 Resizing

By default, tkinter GUI windows are resizable. The developer has to make sure that thewidgets within the window change size as the main window changes size. This can be donein different ways, depending on the layout manager in use. Below, is the source code for asimple GUI application which can be resized.

from Tkinter import ∗

class Application(Frame):def say hi(self):

print "hi there, everyone!"

def createWidgets(self):self.QUIT = Button(self)self.QUIT["text"] = "QUIT"self.QUIT["fg"] = "red"self.QUIT["command"] = self.quit

self.QUIT.pack({"side": "left"})

34

Page 36: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

self.hi there = Button(self)self.hi there["text"] = "Hello",self.hi there["command"] = self.say hi

self.hi there.pack({"side": "left"})

def init (self, master=None):Frame. init (self, master)self.pack()self.createWidgets()

root = Tk()app = Application(master=root)app.mainloop()root.destroy()

Running this application, you can resize the window. The buttons in the window are reposi-tioned when the window resizes so that the remain, horizontally, in the middle of the window.

As mentioned earlier, widgets inside the window do not necessarily automatically resize withthe window. Tkinter provides a pack and grid geometry manager for applications. Bothgeometry managers allow developers to create widgets that resize as the window resizes[16][9].

Pack Geometry Manager

Using the pack geometry manager provided by tkinter is quite simple. When creating widgetsfor the window, a reference to the parent window, or widget, must be given to the constructor.This determines which window, or widget, the widget is to be added to.

After creating a widget object, the method ‘pack‘ must be called, to add the widget tothe parent widget, using the pack layout manager. The ‘pack‘ accepts various optionalparameters. To ensure that the widget expands with the window, a parameter ‘expand‘ mustbe provided, with a non-zero value. Further, the widget being added to the window should fillavailable space on the X or Y axis, or both[9]. This can be achieved by providing the ‘pack‘method with a parameter ‘fill‘. A simple example implementing this can be seen below.

from Tkinter import ∗

class Application(Frame):

def init (self, master=None):Frame. init (self, master)self.pack(expand=1, fill=BOTH)self.createWidgets()

def createWidgets(self):self.blue = Label(self, text="Blue area",

bg="blue", fg="white")self.blue.pack(fill=BOTH, expand=1)self.red = Label(self, text="Red area",

bg="red", fg="white")self.red.pack(fill=BOTH, expand=1)

35

Page 37: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

root = Tk()app = Application(master=root)app.mainloop()

try:root.destroy()

except:pass

The following two figures show what this example looks like before and after resizing.

Figure 9.2: Initial view

Figure 9.3: Resized

Grid Geometry Manager

Using the grid geometry manager to resize widgets inside a window requires more code, andsome different methods. To use the grid manager, widgets must be added to their parentwidgets using the ‘grid‘ method instead of the ‘pack‘ method. The ‘grid‘ method should besupplied the parameters ‘column‘ and ‘row‘, telling the manager which row and column ofthe parent widget to add the widget to. In addition to this, the ‘grid‘ method accepts anoptional ‘sticky‘ parameter, telling the manager how the widget should be resized, if at all.Further, columns and rows in a widget must be configured using the methods ‘rowconfigure‘

36

Page 38: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

and ‘columnconfigure‘. Setting the ‘weight‘ property tells the widget to resize the widgetswithin the given row or column, and by what rate[16]. The following example shows how todo this properly.

from Tkinter import ∗

class Application(Frame):

def init (self, master=None):Frame. init (self, master)self.pack(expand=1, fill=BOTH)self.createWidgets()

def createWidgets(self):self.blue = Label(self, text="Blue area",

bg="blue", fg="white")self.blue.grid(row=0, column=0, sticky=W+E+N+S)self.red = Label(self, text="Red area",

bg="red", fg="white")self.red.grid(row=1, column=0, sticky=W+E+N+S)

self.columnconfigure(0, weight=1)self.rowconfigure(0, weight=1)self.rowconfigure(1, weight=1)

root = Tk()root.columnconfigure(0, weight=1)root.rowconfigure(0, weight=1)app = Application(master=root)app.mainloop()

try:root.destroy()

except:pass

When running this application, you get the same results shown in figures 9.2 and 9.3.

9.2.2 Animations

Animation can be achieved in Python using Tkinter. Tkinter provides a canvas object whichcan be used to draw shapes in a window[15]. Utilising the ‘after‘ method on a widget, shapesdrawn with the canvas can be moved after a number a given number of milliseconds. Toavoid freezing the GUI, ‘after‘ can be called repeatedly to animate shapes one step at a time.The application shown below does this to swap the positions of two rectangles after clickinga button.

from Tkinter import ∗

class Application(Frame):

def init (self, master=None):

37

Page 39: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

Frame. init (self, master)self.pack(expand=1, fill=BOTH)self.createWidgets()

def createWidgets(self):’ ’ ’C r e a t e our w i d g e t s t u f f .’ ’ ’

# B u t t o nself.start = Button(self, text="Start",

command=self.start animation)self.start.pack(side="top", fill=BOTH)# C r e a t e a c a n v a s .self.canvas = Canvas(self, width=150, height=220)self.canvas.pack(side="top", fill=BOTH)# R e c t a n g l e sself.red = self.canvas.create rectangle(

50, 10, 60, 200, fill="red", outline="red")self.blue = self.canvas.create rectangle(

100, 10, 110, 200, fill="blue", outline="blue")# Move d i s t a n c e sself.x = 1self.y = 0self.total = 50self.animating = Falseself.back = False

def animate(self):’ ’ ’Animate one f r a m e .Wooo .’ ’ ’if self.back:

self.canvas.move(self.red, −self.x, self.y)self.canvas.move(self.blue, self.x, self.y)

else:self.canvas.move(self.red, self.x, self.y)self.canvas.move(self.blue, −self.x, self.y)

self.canvas.update()self.total−= self.x

if self.total <= 0:self.animating = Falseself.back = not self.backreturn

self.after(10, self.animate)

def start animation(self, event=None):’ ’ ’S t a r t a n i m a t i o n .’ ’ ’if self.animating:

38

Page 40: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

return

self.total = 50self.animating = Trueself.animate()

root = Tk()app = Application(master=root)app.mainloop()

try:root.destroy()

except:pass

Figure 9.4: Before Animation

Figure 9.5: After Animation

39

Page 41: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

9.2.3 How This Will Be Used

This research has been helpful in figuring out how to build GUIs using Tkinter, make resizingwork well, and animating elements of the GUI. The final program must be able to animatea graph representing an array. Animations will involve moving bars in a graph from onelocation to another, and changing their appearance depending on what they are being usedfor in the algorithm.

9.3 Program Architecture

The program can be split up into multiple subsystems. Separating the functionality of theprogram into different systems should make it easier to implement these systems indepen-dently of each other. As the program is working with data, logic, and a GUI, it makessense to create subsystems that deal with these things directly, and have these subsystemscommunicate with each other when they need information from one another. Thus, it seemsappropriate to split the system up in a manner similar to MVC frameworks.

MVC, or Model, View, Controller frameworks are prevalent in website development. Suchframeworks provide the means to more easily modularise application code. Models are con-cerned with application data, views are concerned with input and output, and controllersare concerned with “business logic”. Business logic entails processing input data to produceoutput data, which is then passed to views. The views then produce and display the output.Typically, models and views do not communicate directly.

Figure 9.6: MVC Data Flow

Figure 9.7: System Architecture Overview

In the sorting and searching testbed, our models can be any code that stores or produces datato be used by the sorting and searching algorithms. The controllers are the objects whichhandle the execution of the sorting and searching algorithms. Our views are the programwindows, or the code which produces and handles the GUI.

40

Page 42: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

The final program will also be using threads to run the algorithms. To avoid problems withthreading, the algorithms should not communicate with the GUI directly. Instead, they canpush commands to an event queue, which the GUI code can read from as and when it hastime. Such an event queue needs to be thread safe, so will require the use of semaphores.Figure 9.7 shows a brief overview of the system architecture.

Figure 9.8: Algorithm Environment Overview

Figure 9.9: Algorithm Window Architecture Overview

9.3.1 Algorithm Environment Overview

The algorithm environment is concerned with loading algorithm suites, creating instances ofthem, and managing their threads. As there are multiple threads, a pool for the threads isrequired. A thread pool makes it easier for the application to tell all of the threads to startand stop running at any given time, as well as providing a simple interface for configuringthe algorithm suites. This architecture makes use of the composite design pattern, to someextent.

Individual algorithm suites are responsible for executing a specific sorting or searching al-gorithm in steps. The implementation must be able to send commands to the GUI, so theGUI knows what animations to perform and when. Hence, algorithm suites should hold areference to an event queue associated with a related graph in the GUI.

The suite loader is responsible for loading algorithm suite classes from files, and producesa suite pack containing references to these classes. The suite pack is then responsible forcreating instances of algorithm suite classes and associated suite thread objects, adding them

41

Page 43: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

to a suite pool, which is then returned to the main application.

Figure 9.8 shows a brief overview of the algorithm environment subsystem.

9.3.2 Algorithm Window Overview

The sorting and searching algorithm windows can use mostly the same code and architecture.The window itself displays a set of graphs representing algorithms. Graphs can performanimations. The animation factory is responsible for creating animations based on eventsreceived from the event queue. For ease of implementation, graphs are added as componentsof a graph collection. Figure 9.9 shows a brief overview of the algorithm window architecture.

9.3.3 Summary

As previously noted, the algorithm suites each have an associated graph in the GUI. Thegraph is responsible for managing animations. Animations are performed based on the dif-ferent operations undertaken by the algorithms implemented in the algorithm suites. So, thealgorithm suites need to be able to communicate with the graphs in the GUI.

To achieve this, an event queue is used. The event queues shown in figures 9.8 and 9.9 isthe same event queue. Algorithm suites push events to this queue, detailing operations thathave been performed. The graph then reads events from the queue, and produces animationsbased on the event data.

Using this paradigm keeps the GUI and algorithm suites separated. Because of this, algorithmsuites can be threaded without too much concern for unusual side-effects. Running thealgorithms in a thread also allows the GUI to remain responsive throughout operation.

9.4 Classes

Figure 9.10 shows the relationships between the classes active while a given algorithm windowis open. The GUI, algorithm suites, and data are all kept separate. Reducing the couplingpresent in classes should make maintenance easier, as changes can be made to differentsystems without the need to modify many other classes.

This section attempts to describe some of the classes, their responsibilities, and how theyrelate to other classes in the system. Here, we only examine classes of greater interest.

9.4.1 AlgorithmSuite class

This is a case class for algorithm suites. AlgorithmSuite provides a normalised API for othercomponents to make use of. Algorithm suites implement specific search or sort algorithms ina manner which makes it easier to animate the process.

Specific sorting algorithm suite classes are stored in modules in the toutsuite.algorithms.sortpackage. Similarily, searching algorithm suites appear in modules in the toutsuite.algorithms.searchpackage. Modules relating to a specific algorithm are named after the algorithm they imple-ment. Sorting algorithm suite classes are named SortSuite, and searching algorithm suite

42

Page 44: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

Figure 9.10: Class Diagram

classes are named SearchSuite. Hence the class bubble.SortSuite, which appears in thetoutsuite.algorithms.sort package, is a type of AlgorithmSuite which implements thebubble sort algorithm.

Algorithm suites perform an algorithm in steps which are evoked by a SuiteThread objectat regular intervals. During these steps, the algorithm suite is responsible for sending Event

objects to an EventQueue which a related Graph object also holds a reference to. TheseEvent objects are used to produce animations.

9.4.2 Application class

The Application is mostly responsible for loading the components of the system and runningthem. Generally, the main application does not need to know about most of the classes in thesystem. The Application class is mostly concerned with loading algorithm suites, managingsuite threads using a SuitePool object, managing configuration data using a Config ob-

43

Page 45: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

ject, and loading and running different GUI windows, including different AlgorithmWindow

windows.

9.4.3 AlgorithmWindow class

AlgorithmWindow objects are responsible for constructing and managing algorithm windows.The Application object does not use this class directly. Rather, the classes SortingWindowand SearchingWindow, which are child classes of AlgorithmWindow, are used.

9.4.4 Graph class

Each sorting or searching algorithm loaded in the program is represented by a graph inan algorithm window. A Graph object is used to construct and animate a graph. Whilean algorithm is working, the Graph object pulls Event objects from an EventQueue object,passes this to an AnimationFactory object, which then returns an Animation object. TheGraph object then evokes animation frames on the Animation object at regular intervals.

As there are likely to be multiple graphs in any given algorithm window, the Graph objectsare grouped in a GraphCollection object, which makes managing the graphs easier for thealgorithm window. This is a composition.

9.4.5 Animation class

The Animation class is a base class for graph animations. Classes which implement theAnimation class, animation classes, perform specific animations, working with rectanglesand the graph itself. Animation classes are run by the Graph class, calling the step methodat regular intervals to propagate the next frame of the animation. The different animationclasses are based on generic operations relating to the algorithms used in the application.

Select Animation

The Select animation class changes the colour of a specific rectangle to indicate it has beenselected by an algorithm. Elements are “selected” when the algorithm is using them forcomparisons or when moving the elements.

Deselect Animation

The Deselect animation class reverses the changes applied by a select animation. This isused when an algorithm has finished working with a given element.

Swap Animation

The Swap animation class swaps two adjacent rectangles on the graph. This is used when analgorithm swaps two elements in an array.

44

Page 46: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

Move Animation

The Move animation class moves a rectangle from one position in the graph to another. Thisis implemented by chaining as many swap animations as is required.

Highlight Animation

The Highlight animation class applies a highlight to a graph. A highlight is a blue barunder the bars in the graph, spanning across a defined number of the bars. This is used toindicate that an algorithm is working with a specific section, or partition, of the array. Theremay only be one highlight at a time for each graph.

Unhighlight Animation

The Unhighlight animation class reverses the changes applied by a highlight animation.

9.5 Summary

Object oriented programming provides opportunities to reuse code and more easily modu-larise a system. This chapter has identified some classes which are crucial to the implemen-tation, and discussed a structure for the source code which should make implementation andmaintenance easier.

The GUI, algorithm suites, and data handling sections of the system should be kept mostlyseparate. Loosely coupled objects are easier to maintain. The architecture proposed in thischapter has been formulated with this in mind.

Cluttered user interfaces can confuse users, and too much information in a small space can beoverwhelming. The suggested designs for the GUI itself have been kept as clean as possible.

45

Page 47: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

Chapter 10: Implementation

For the final program, I have used Python 2.7. Python is an interpret language, which allowsfor fast development. A GUI toolkit Tkinter is included in Python. This makes buildingGUIs fairly simple. Python also comes with unit test, a unit testing framework, which makesautomating tests simple.

Before working on the program itself, I wrote test cases for the classes in the algorithmenvironment defined in section 9.3.1 of this report, the EventQueue and Event classes, andthe Config class. Automating GUI code is more problematic, so I resolved to test the GUIas I made changes to the code driving it.

After writing the test cases, I implemented the algorithm environment and event systemclasses. Next came the main Application class, and the GUI. The Config class was the lastthing considered.

Implementing the algorithm environment was simple. GUI programming is difficult, andsomething I do not have a lot of experience. This is why I started with the algorithmenvironment and moved onto the GUI code afterwards.

The program mostly works as expected. There are small problems with the scaling of therectangles in the graphs as the size of the array gets larger, and animations run quite slowly.Animations running slowly is not too much of a problem as it allows users to really see howthe algorithms operate, without potentially missing anything.

10.1 References

To properly implement the program within the allotted time frame, I made use of some codefrom various sources.

10.1.1 SuiteLoader

For the SuiteLoader class, I adapted some code I had written for a previous project, Reflex.The code in question can be found at this address: https://github.com/photofroggy/

Reflex/blob/78db5fd197cad84652760185d2e1a0965e3fd10c/reflex/control.py#L256

10.1.2 AlgorithmSuite

Within the AlgorithmSuite class, I made use of small piece of code found on stackoverflow toaid in measuring the execution time of algorithms. The code is used in the measure methodand can be found at the following address: http://stackoverflow.com/a/14978116

10.1.3 VerticalScrolledFrame

For the VerticalScrolledFrame class, I adapted code written by someone else, found at thefollowing address: http://stackoverflow.com/a/16198198

46

Page 48: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

Chapter 11: Testing

Testing is an important part of development, as it provides proof that the program is workingcorrectly. This chapter documents some of the testing undertaken.

11.1 Unit Tests

For large portions of the program, I have written several test suites using Python’s unittestframework. Unfortunately, writing unit tests for GUI code is impractical. However, there areseveral parts of the program where unit tests can be used to prove their functionality. Belowis an example of a test suite failing.

respire-air:program henry$ python -m toutsuite.test.test_config

test_array (__main__.TestConfig) ... FAIL

test_array_error (__main__.TestConfig) ... ok

test_array_random (__main__.TestConfig) ... ERROR

test_array_random_error (__main__.TestConfig) ... ok

test_needle (__main__.TestConfig) ... FAIL

======================================================================

ERROR: test_array_random (__main__.TestConfig)

----------------------------------------------------------------------

Traceback (most recent call last):

File "/Users/henry/Documents/work/zuac072/project/program/toutsuite/test/test_config.py", line 42, in test_array_random

self.assertNotEqual( arr, sorted(arr), ’random_array produced a sorted array’ )

TypeError: ’NoneType’ object is not iterable

======================================================================

FAIL: test_array (__main__.TestConfig)

----------------------------------------------------------------------

Traceback (most recent call last):

File "/Users/henry/Documents/work/zuac072/project/program/toutsuite/test/test_config.py", line 27, in test_array

self.assertEqual( self.config.get_array(), [1,2,3,4,5,6,7,8,9,10], ’Wrong array returned’ )

AssertionError: Wrong array returned

======================================================================

FAIL: test_needle (__main__.TestConfig)

----------------------------------------------------------------------

Traceback (most recent call last):

File "/Users/henry/Documents/work/zuac072/project/program/toutsuite/test/test_config.py", line 59, in test_needle

self.assertEqual( self.config.get_needle(), 6, ’Wrong item returned’ )

AssertionError: Wrong item returned

----------------------------------------------------------------------

Ran 5 tests in 0.001s

FAILED (failures=2, errors=1)

47

Page 49: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

Below is the output of the full test suites after having implemented everything that test suitescan be written for.

respire-air:program henry$ python -m toutsuite.test.test_all

test_array (toutsuite.test.test_config.TestConfig) ... ok

test_array_error (toutsuite.test.test_config.TestConfig) ... ok

test_array_random (toutsuite.test.test_config.TestConfig) ... ok

test_array_random_error (toutsuite.test.test_config.TestConfig) ... ok

test_needle (toutsuite.test.test_config.TestConfig) ... ok

test_create (toutsuite.test.test_event.TestEvent) ... ok

test_each (toutsuite.test.test_event.TestEvent) ... ok

test_remove (toutsuite.test.test_event.TestEvent) ... ok

test_setget (toutsuite.test.test_event.TestEvent) ... ok

test_add_event (toutsuite.test.test_event_queue.TestEventQueue) ... ok

test_next_event (toutsuite.test.test_event_queue.TestEventQueue) ... ok

test_set_array (toutsuite.test.test_algorithm_suite.TestAlgorithmSuite) ... ok

test_set_needle (toutsuite.test.test_algorithm_suite.TestAlgorithmSuite) ... ok

test_start (toutsuite.test.test_algorithm_suite.TestAlgorithmSuite) ... ok

test_step (toutsuite.test.test_algorithm_suite.TestAlgorithmSuite) ... ok

test_search (toutsuite.test.test_suite_loader.TestSuiteLoader) ... ok

test_sort (toutsuite.test.test_suite_loader.TestSuiteLoader) ... ok

test_each (toutsuite.test.test_suite_pack.TestSuitePack) ... ok

test_get (toutsuite.test.test_suite_pack.TestSuitePack) ... ok

test_length (toutsuite.test.test_suite_pack.TestSuitePack) ... ok

test_pool (toutsuite.test.test_suite_pack.TestSuitePack) ... ok

test_queues (toutsuite.test.test_suite_pack.TestSuitePack) ... ok

test_pause (toutsuite.test.test_suite_thread.TestSuiteThread) ... ok

test_queue (toutsuite.test.test_suite_thread.TestSuiteThread) ... ok

test_run (toutsuite.test.test_suite_thread.TestSuiteThread) ... ok

test_add (toutsuite.test.test_suite_pool.TestSuitePool) ... ok

test_run (toutsuite.test.test_suite_pool.TestSuitePool) ... ok

test_self_stop (toutsuite.test.test_suite_pool.TestSuitePool) ... ok

test_sort (toutsuite.test.test_bubble_sort.TestBubbleSortSuite) ... ok

test_add (toutsuite.test.test_quick_partition.TestQuickPartition) ... ok

test_indices (toutsuite.test.test_quick_partition.TestQuickPartition) ... ok

test_pivot (toutsuite.test.test_quick_partition.TestQuickPartition) ... ok

test_sort (toutsuite.test.test_quick_sort.TestQuickSortSuite) ... ok

test_sort (toutsuite.test.test_parallel_sort.TestParallelSort) ... ok

test_sort (toutsuite.test.test_linear_search.TestLinearSearchSuite) ... ok

test_sort (toutsuite.test.test_binary_search.TestBinarySearchSuite) ... ok

test_sort (toutsuite.test.test_parallel_search.TestParallelSearch) ... ok

----------------------------------------------------------------------

Ran 37 tests in 2.375s

OK

The test suites have been named in such a way that the output of this testing should beenough to see what is being tested.

48

Page 50: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

Chapter 12: Professional Issues

Professional issues in computer science and programming is concerned with the impact ofcomputing technology on society. Computer scientists have an obligation to ensure thattechnology is created in the best interests of society and any other relevant parties, suchas clients. Techniques used and the end results must adhere to certain ethics by providingquality work, competence, integrity, and commitment to the work.

With a searching and sorting testbed, there are some things to consider in this regard. Thoughit may not seem too obvious. Searching and sorting testbeds are relatively simple programs,and I do not expect there to be wide usage of the program created for this project. Even so,there are some things that must be considered.

12.1 Usability and Safety

If anyone is going to use the program at all, then the program should be easy to use. Pro-viding a graphical user interface is a requirement, as there must be animations showing howthe various algorithms work. The program must be able to run without the user interfacebecoming unresponsive. If the user wants to stop the animations for any reason, they shouldbe able to do so.

Programs that are difficult to use are often useless, as users are less likely to want to usethem. Providing a simple interface that still allows enough control over the execution of theprogram is essential.

Further, safety could be a concern. The searching and sorting algorithms used in the programhave been adapted so that it is easier to create animations showing how they work. As such,they are not optimal implementations for practical programs. If someone tries to use thealgorithms from the program in their own code, for a mission-critical application, then itcould cause a lot of problems, mostly relating to speed.

Sorting algorithms are used to handle large data sets in many different applications andwebsites. As an unlikely example, if a large search engine (e.g. google) were to use thealgorithms created for my program, then it may take a lot longer than is acceptable for usersto get a response to searches. Using these algorithms in this manner could result in loss ofrevenue.

Because of this, it is advisable to include clear disclaimers and warnings when distributingthe application and its source code.

12.2 Licensing and Distribution

12.2.1 BSD License

The BSD license is an open source software license[10]. Creating and distributing open sourcesoftware allows other developers to make changes to the program as they see fit, and distributetheir own modified versions of the software. This can be beneficial as improvements can be

49

Page 51: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

made. In the case of the sorting and searching testbed, other developers could implementmore algorithms to show how they work in comparison to the algorithms already implemented.

Allowing others to distribute modified versions of the software can also lead to problems.A developer could add malicious code to the application, and users could unknowingly usethe modified program and be subject to attacks. The third clause of the BSD license statesthat others may not use the copyright holder’s name for the software to endorse their ownderivatives without express, written permission from the copyright holder. This clause shouldmake it more difficult to confuse potential end users, but malicious developers can alwaysignore license agreements. In the event that the licensing is ignored, legal action can be takenon the part of the copyright holder.

AS-IS Clause

The BSD license includes an “AS-IS” clause, shown below. This removes responsibility fromthe developer for applications that may contain severe bugs. As mentioned in the previoussection, if the algorithms are used inappropriately, then there could be severe consequences.In such cases, providing an “AS-IS” clause in the licensing should protect the developers fromany legal responsibilities.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ANDCONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES,INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OFMERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CON-TRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ORSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTER-RUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDINGNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USEOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCHDAMAGE.

12.2.2 Distribution

In order for people to use software, it has to be distributed. At the very least, it needs tobe easy to obtain. The internet provides a convenient means to distribute software, amongother things. There are several websites (e.g. sourceforge.com, github.com) which are builtfor, at least partially, distributing software.

As well as providing easy access to end users, in the case of open source software it is alsobeneficial to provide easy access to the source code for developers. This provides otherdevelopers with the opportunity to modify the source code and create derivative products.

While developers would need access to the source code, it is easier for users to simply providean executable file. Python applications are not compiled, so in the common case, onlythe source code is distributed, and any users are expected to manually invoke the Pythoninterpreter to run the application. However, there are some programs available which cancreate executable files out of Python programs. One such program is cx freeze.

50

Page 52: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

Git and Github

Git is a revision control software. The functionality of git is similar to SVN, but worksdifferently, and, in my view, provides an easier interface, and allows for a better work flow.Git uses “branches”, which easy to use. Branches should be used to develop different featuresseparately without interrupting the development of the whole application.

Github.com is a website which provides project hosting for software, and uses git. Developerscan conveniently download repositories, create forks, make their own modifications indepen-dent of the original repository, and send pull requests if they want to contribute to the originalproject. A pull request is a request to merge any given changes into another repository.

Git and github make collaboration and distribution easy. For this project, I have to use SVN,as that is what the college uses on its systems. For ease of distribution and collaboration, Iwould prefer to use git. An increasing number of open source developers are using git andgithub. Taking advantage of a thriving community is incredibly beneficial for open sourceprojects.

12.3 Coding Standards

Sticking to certain standards is encouraged, as it makes it far easier for other developers to getinvolved. Python programs should stick to the style guide defined in PEP 8[12]. This styleguide helps keep code readable. If code is easy to read, then it is easier for other developersto contribute to a project.

Using familiar design patterns also makes it easier for other developers to contribute. Pro-grams should be structured in a way that makes logical sense, and is recognisable. Establisheddesign patterns makes code easier to understand, as well as providing some intuition on howthe code should be structured.

12.4 Impact

A searching and sorting testbed is beneficial for programmers. Such programs provide someinsight into which algorithms are more efficient without having to analyse the algorithmsmanually. Providing animations of the algorithms also gives an idea of how the algorithmswork internally. This can help improve a programmer’s understanding of algorithms, as wellas providing some insight into how the algorithms are implemented.

51

Page 53: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

Chapter 13: Conclusion

Over the course of this project I have created a searching and sorting testbed which shows howthe bubble sort, quick sort, linear search, and binary search algorithms work. The programprovides a graph representing the arrays being used, and animates these graphs according tothe operation of the algorithms.

The first portion of the project involved researching algorithm complexity, software engineer-ing, and design patterns. I feel I have a far greater understanding of these areas having lookedinto them. Design patterns have been quite helpful in structuring the source code for theprogram.

I have used test driven development while developing the final program. This involves creatingtest suites for each component of the program before writing the components themselves.These test suites are run as programs in and of themselves. Python provides a unit testingframework, which came in useful when writing the test suites. Python is a “batteries included”language, in that it provides useful foundations for most non-trivial applications.

The final program works mostly as expected. The only real problem is in the scaling ofrectangles in the graphs. As the array increases in size, some of the elements become toolarge to fully fit inside the graph, and end up being clipped. It doesn’t seem to be a majorissue, as the rest of the program works well, and provides a decent visualisation of thealgorithms.

The program has been made in such a manner that more sorting and searching algorithmscould be implemented with relative ease. Unfortunately, I did not have the time to write anytutorials detailing how to do so. Further development for this project could include writingsuch tutorials, and finally fixing the problems with scaling rectangles in the graphs. Also, theapplication could be packaged into an executable file, which would make it easier to installand run the application.

Throughout the project, my time management has not been great, meaning at times I havebeen a little behind. Ultimately, I managed to get everything done in time for deadlines.Pushing on deadlines so closely is not ideal. This is definitely something I need to work on,at least a little bit.

52

Page 54: Final Year Project Report - frogpond.herokuapp.comfrogpond.herokuapp.com/uniproject/projectreport.pdfSorting and Searching Testbed Henry Rapley Chapter 1: Abstract Sorting and searching

Sorting and Searching Testbed Henry Rapley

Bibliography

[1] Eric Freeman; Elisabeth Freeman; Kathey Sierra; Bert Bates. Head first design patterns,isbn 978-0-596-00712-6. O’Reilly Media, 2004.

[2] Kent Beck. Test-driven development by example. Pearson Education Inc., 2003.

[3] Ken Schwaber; Mike Beedle. Agile software development with scrum. Prentice Hall,2002.

[4] Adam Drozdek. Data structures and algorithms in java, second edition. Thomson CourseTechnology, 2005.

[5] Simon Bennett; Steve McRobb; Ray Farmer. Object-oriented systems analysis anddesign. McGraw-Hill, 2006.

[6] Python Software Foundation. Tkinter - python interface to tcl/tk, November 2013.http://docs.python.org/2/library/tkinter.html.

[7] Oded Goldreich. Computational complexity: A conceptual perspective, isbn 978-0-521-88473-0. Cambridge University Press, 2008.

[8] Cay Horstmann. Object oriented design & patterns. John Wiley & Sons, 2006.

[9] Fredrik Lundh. The tkinter pack geometry manager, November 2013. http://effbot.org/tkinterbook/pack.htm.

[10] OpenSource.org. The bsd 3-clause license, February 2014. http://opensource.org/

licenses/BSD-3-Clause.

[11] Roger S. Pressman. Software engineering: A practitioner’s approach. McGraw-Hill,1994.

[12] python.org. Style guide for python code, November 2013. http://legacy.python.org/dev/peps/pep-0008/.

[13] Allen Tucker; Ralph Morelli; Chamindra De Silva. Software development: An opensource approach. CRC Press, Taylor & Francis Group, 2011.

[14] Ian Sommerville. Software engineering. Pearson Education Inc., 2011.

[15] TkDocs. Canvas, November 2013. http://www.tkdocs.com/tutorial/canvas.html.

[16] TkDocs. The grid geometry manager, November 2013. http://www.tkdocs.com/

tutorial/grid.html.

[17] E. Gamma; R. Helm; R. Johnson; J. Vlissides;. Design patterns: Elements of reusableobject oriented software. Addison-Wesley, 1995.

[18] Steven Rudich; Avi Wigderson. Computational complexity theory. American Mathe-matical Society, 2004.

53