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

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

Analysis of Algorithms
11 November 2014Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
CS4413 Divide-and-Conquer
CSCE 3110 Data Structures & Algorithm Analysis
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
CMPS1371 Introduction to Computing for Engineers SORTING.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Faster Sorting Methods Chapter 9. 2 Chapter Contents Merge Sort Merging Arrays Recursive Merge Sort The Efficiency of Merge Sort Merge Sort in the Java.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
Algorithms. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L18 (Chapter 23) Algorithm.
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
Algorithmics - Lecture 21 LECTURE 2: Algorithms description - examples -
Introduction to Computer Systems
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
25 November 2014Birkbeck College1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 26 Sorting.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 2.
29 September 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
17 November 2015Birkbeck College1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
ALGORITHMS.
13 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
3 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
17 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.
Divide and Conquer Strategy
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Elementary Sorting 30 January Simple Sort // List is an array of size == n for (i = 1; i < n; i++) for (j = i+1; j List[j])
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Introduction to Computer Systems
Today’s Material Sorting: Definitions Basic Sorting Algorithms
12 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Linear Sorting. Comparison based sorting Any sorting algorithm which is based on comparing the input elements has a lower bound of Proof, since there.
Quick Sort Modifications By Mr. Dave Clausen Updated for Python.
4 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲.
11 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 23 Sorting.
CSE15 Discrete Mathematics 03/06/17
Introduction to Algorithms
Introduction to Computer Systems
Introduction to Computer Systems
C++ Basic Syntax – Homework Exercises
Teach A level Computing: Algorithms and Data Structures
CSc 110, Spring 2017 Lecture 39: searching.
Chapter 8 Arrays Objectives
Data Structures Review Session
Algorithm Discovery and Design
Introduction to Programming
Introduction to Algorithms
Chapter 8 Arrays Objectives
CSE 373 Data Structures and Algorithms
Presentation transcript:

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

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.

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 November 2015Brookshear Ch 5 review problems 113

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

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

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

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

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 would produce

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 structures 24 November 2015Brookshear Section 5.49

24 November 2015Brookshear Section 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

24 November 2015Brookshear, Section 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

24 November 2015Brookshear, Section 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

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

24 November 2015Brookshear, Section 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

24 November 2015Brookshear, Section 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

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

24 November 2015Brookshear, Section 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

24 November 2015Brookshear, Section 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

24 November 2015compare Brookshear, Section 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.

24 November 2015Brookshear, Section Search Example: solving the eight-puzzle An unsolved eight puzzle A solved eight puzzle Number of configurations = 9x8x…x1 = 362,880

24 November 2015Brookshear, Section Possible Moves

24 November 2015Brookshear, Section 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?)