Presentation is loading. Please wait.

Presentation is loading. Please wait.

24 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

Similar presentations


Presentation on theme: "24 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems."— Presentation transcript:

1 24 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Autumn 2015 Week 9a: Revision Exercises and Sorting

2 17 November 2015Birkbeck College2 Example 1: Qu. 11 from the 2003 Exam Write an algorithm for calculation of tax for a yearly salary. The tax is zero when the salary is £3000 or less. It is 10% for the next salary band from £3001 to £8000. It is 20% for the part from £8001 to £20000, and it is 40% for any part of the salary over £20000.

3 Example 2 Design an algorithm for finding all the factors of a positive integer. For example, in the case of the integer 12, your algorithm should report the values 1, 2, 3, 4, 6 and 12. 17 November 2015Brookshear Ch 5 review problems 113

4 Example 3 The Euclidean algorithm finds the greatest common divisor of two positive integers x and y by the following process: As long as x and y are both not zero divide the larger number by the smaller. Replace the larger value with the remainder. When x or y has the value 0 the value of the other variable is the GCD. Produce a pseudo code version of the algorithm 17 November 2015Brookshear Section 5.24

5 Example 4 Write a pseudo code algorithm to carry out the following task: input: a 1-dimensional array A of integers output: the integer -1 if A[i] ≥ A[i+1] for 0 ≤ i ≤ Length[A]-2, otherwise the least integer j such that A[j] < A[j+1]. 17 November 2015Birkbeck College5

6 Example 5 Design an algorithm that tests two one dimensional arrays of the same length to see if the two arrays contain the same entries in the same order. 17 November 2017Birkbeck College6

7 17 November 2015Birkbeck College7 Example 6 Design an algorithm that lists all possible rearrangements of the symbols in a string of three distinct characters.

8 17 November 2015Birkbeck College8 Example 7 Design an algorithm that when given an arrangement of the digits 0,1,2,3,4,5,6,7,8,9 rearranges the digits so that the new arrangement represents the next larger value that can be represented by these digits (or reports that no such arrangement exists if no rearrangement produces a larger value). Thus 5647382901 would produce 5647382910.

9 Sorted and Unsorted Lists In a sorted list it is known that the elements are in a predefined order, eg. {-1, 4, 26, 30, 31} In an unsorted list it is not known if the elements are in a predefined order, eg. {3, 0, -6, 2, 1} and {-1, 4, 26, 30, 31} Sorting is an important task. See http://en.wikipedia.org/wiki/Sorting_algorithm http://www.studytonight.com/data-structures/introduction-to-data- structures 24 November 2015Brookshear Section 5.49

10 24 November 2015Brookshear Section 5.410 Sketch 1 of Insertion Sort Task: sort a list L. Suppose the section L[0], …, L[N] is sorted Sort the section L[0],…L[N], L[N+1] by inserting L[N+1] into the correct place in L[0], … L[N] Set N = N+1 and keep going until the whole list is sorted

11 24 November 2015Brookshear, Section 5.411 Sketch 2 of Insertion Sort  The symbol || means list concatenation.  Assume L=L1 || {e} || L2 with L1 sorted (e is the pivot entry)  Sort L1 || {e} to give the list L3  Form the new list L3 || L2  Notice L3 is i) sorted, and ii) strictly larger than L1

12 24 November 2015Brookshear, Section 5.412 Example of Insertion Sort  At an intermediate stage, L = {2, 6, 8, 4, 5, 7} L1 = {2, 6, 8} e = 4 L2 = {5, 7}  The position L[3] is the pivot, 4 is the pivot entry.  The pivot entry is stored in the variable e, thus the pivot L[3] can be overwritten without losing information

13 Example Continued L = {2, 6, 8,, 5, 7} The position is called the hole. 4 < 8 therefore move 8 one place right to obtain L = {2, 6,, 8, 5, 7} 4 < 6 therefore move 6 one place right to obtain L = {2,, 6, 8, 5, 7} 4 > 2 therefore copy 4 into the hole to obtain L = {2, 4, 6, 8, 5, 7} 24 November 2015Birkbeck College, U. London13

14 24 November 2015Brookshear, Section 5.414 Insertion Sort def insertionSort(L): N = 1 while (N < Length[L]): e = L[N] hole = N while (there is an entry immediately preceding hole and that entry is greater than e): copy the entry to L[hole] hole = hole-1 L[hole] = e N = N+1 return L

15 24 November 2015Brookshear, Section 5.515 Sketch of Bubble Sort Task: sort a list L If a pair L[N], L[N+1] are in the wrong order then interchange them Problem: how to choose the pairs such that the algorithm terminates with a sorted list? Answer: make sure low entries move quickly towards the beginning of L

16 Example of Bubble Sort L = {6, 2, 8, 4, 5, 7} L[4] < L[5]: no change L[3] < L[4]: no change L[2] > L[3]: interchange L[2] and L[3] to obtain L = {6, 2, 4, 8, 5, 7} L[1] < L[2]: no change L[0] > L[1]: interchange L[0] and L[1] to obtain L = {2, 6, 4, 8, 5, 7} Note: the least entry, 2, is in the correct place. 24 November 2015Birkbeck College, U. London16

17 24 November 2015Brookshear, Section 5.517 Bubble Sort 1 def bubbleSort(L): N = 0 while (N < Length(L)-1): bubble(section of L from index N to index Length(L)-1) N = N+1 return L

18 24 November 2015Brookshear, Section 5.518 Bubble Sort 2 def bubble(L): i = Length(L)-2 while(i >= 0): if(L[i] > L[i+1]): interchange L[i] and L[i+1] i = i-1 return L

19 24 November 2015compare Brookshear, Section 5.619 Algorithm Efficiency for Searching  Searching a list of length n Unsorted list: average number of comparisons=n/2. Sorted list: average number of comparisons is of order Log 2 (n).  If n is large then Log 2 (n) is much less than n/2. E.g. Log 2 (1,030,576)=20.

20 24 November 2015Brookshear, Section 11.320 Search Example: solving the eight-puzzle 135 42 786 An unsolved eight puzzle 123 456 78 A solved eight puzzle Number of configurations = 9x8x…x1 = 362,880

21 24 November 2015Brookshear, Section 11.321 Possible Moves 135 42 786 135 42 786 135 426 78 13 425 786

22 24 November 2015Brookshear, Section 11.322 Search Strategy Build the tree of all possible moves, breadth first. Stop when the solved eight puzzle is found. Assumption: a solution exists. Problem: low efficiency (why?)


Download ppt "24 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems."

Similar presentations


Ads by Google