210 likes | 219 Views
Introduction to Computer Systems. Department of Computer Science and Information Systems Lecturer: Steve Maybank sjmaybank@dcs.bbk.ac.uk Spring 2019 Week 9a: Revision Exercises and Sorting. Example 1.
E N D
Introduction to Computer Systems Department of Computer Science and Information Systems Lecturer: Steve Maybank sjmaybank@dcs.bbk.ac.uk Spring 2019 Week 9a: Revision Exercises and Sorting Birkbeck College, U. London
Example 1 • From a given list of 1000 integers from 1 to 1000, extract pairs of integers whose product is 2424 Brookshear Ch 5 review problems 51
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. Brookshear Ch 5 review problems 11
Example 3 Design an algorithm that lists all possible rearrangements of the symbols in a string of three distinct characters. Birkbeck College
Example 4 • Design a function f with parameters n dimensional arrays A and B, n > 0. Each entry of A is an integer and each entry of B is 0 or 1. Let sum0 be the sum of the entries A[i] for which B[i] is 0. Let sum1 be the sum of the entries A[i] for which B[i] is 1. The function f returns sum1-sum0. Birkbeck College
Example 5 • Let M be an m×n array such that each entry is 0 or 1. Let A be an n dimensional array. Design a function g with parameters M, A that returns the index of the row R of M for which f(A, R) has the smallest absolute value. Brookshear Ch 5 review problems 50
Example 6 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. Birkbeck College
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 Brookshear Section 5.4
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 Brookshear Section 5.4
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 Brookshear, Section 5.4
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 Brookshear, Section 5.4
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} Birkbeck College, U. London
Insertion Sort function insertionSort(L) N = 1 while (N < Length[L]) e = L[N]; hole = N while (there is an entry immediately before hole and > e) copy the entry to L[hole] hole = hole-1 endWhile L[hole] = e; N = N+1 endWhile return L endFunction Brookshear, Section 5.4
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 Brookshear, Section 5.5
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. Birkbeck College, U. London
Bubble Sort 1 function bubbleSort(L) N = 0 while (N < Length(L)-1) bubble(section of L from index N to index Length(L)-1) N = N+1 endWhile return L endFunction Brookshear, Section 5.5
Bubble Sort 2 function bubble(L) i = Length(L)-2 while(i >= 0) if(L[i] > L[i+1]): interchange L[i] and L[i+1] endIf i = i-1 endWhile return L endFunction Brookshear, Section 5.5
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 Log2(n). • If n is large then Log2(n) is much less than n/2. E.g. Log2(1,030,576)=20. compare Brookshear, Section 5.6
Search Example: solving the eight-puzzle An unsolved eight puzzle A solved eight puzzle Number of configurations = 9x8x…x1 = 362,880 Brookshear, Section 11.3
Possible Moves Brookshear, Section 11.3
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?) Brookshear, Section 11.3