Day 2 – Logic and Algorithms REACHING WIDER SUMMER SCHOOL.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

1 CS101 Introduction to Computing Lecture 17 Algorithms II.
Recursion vs. Iteration The original Lisp language was truly a functional language: –Everything was expressed as functions –No local variables –No iteration.
Recursion. Recursion is a powerful technique for thinking about a process It can be used to simulate a loop, or for many other kinds of applications In.
HST 952 Computing for Biomedical Scientists Lecture 9.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Recursion COMP T1.
1 Sorting. 2 Fundamental problem in computing science  putting a collection of items in order Often used as part of another algorithm  e.g. sort a list,
Searching and Sorting SLA Computer Science 4/16/08 Allison Mishkin.
Cmpt-225 Sorting. Fundamental problem in computing science  putting a collection of items in order Often used as part of another algorithm  e.g. sort.
Data Structures Introduction. What is data? (Latin) Plural of datum = something given.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Lecture 14: A Programming Example: Sorting Yoni Fridman 7/24/01 7/24/01.
Algorithm Efficiency and Sorting
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Sort an array - the selection sort algorithm. Selection Sort Selection sort a classic computer algorithm that is used to sort an array The selection sort.
5.1 and 5.4 through 5.6 Various Things. Terminology Identifiers: a name representing a variable, class name, method name, etc. Operand: a named memory.
CS1101: Programming Methodology Aaron Tan.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Problem Solving and Algorithms
Invitation to Computer Science, Java Version, Second Edition.
Recursion, Complexity, and Sorting By Andrew Zeng.
Chapter 12 Recursion, Complexity, and Searching and Sorting
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Objectives - 11  We will work with processing Arrays.  Objectives:  Describe the concept of an array and its benefits.  Define the terms index, traverse,
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
Informal Analysis of Merge Sort  suppose the running time (the number of operations) of merge sort is a function of the number of elements to sort  let.
How to Read Code Benfeard Williams 6/11/2015 Susie’s lecture notes are in the presenter’s notes, below the slides Disclaimer: Susie may have made errors.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
Introduction to: Programming CS105 Lecture: Yang Mu.
Some comments on lab4. Hi Philippe! Can you tell me if my code works? Thanks! I’ll show you what works…
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Sorting.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
QUICKSORT 2015-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
 MergeSort is a sorting algorithm which is more on the advanced end.  It is very fast, but unfortunately uses up a lot of memory due to the recursions.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
1 Lecture 18: Selection Sort, Quicksort & Merge Sort Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
Written by: Dr. JJ Shepherd
Simple algorithms on an array - compute sum and min.
COP 3540 Data Structures with OOP
Today’s Material Sorting: Definitions Basic Sorting Algorithms
Data Structures Arrays and Lists Part 2 More List Operations.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Algorithms and Flowcharts
char first[10]="monkey"; char second[10]="fish"; char* keep;
GC211Data Structure Lecture2 Sara Alhajjam.
Simple Sorting Algorithms
Alg2_1c Extra Material for Alg2_1
Sorting by Tammy Bailey
Algorithm Analysis CSE 2011 Winter September 2018.
Design and Analysis of Algorithms
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
Algorithm design and Analysis
Describing algorithms in pseudo code
And now for something completely different . . .
Search,Sort,Recursion.
Searching and Sorting Topics Sequential Search on an Unordered File
Algorithms Key Revision Points.
Presentation transcript:

Day 2 – Logic and Algorithms REACHING WIDER SUMMER SCHOOL

ALGORITHMS What are algorithms A set of step by step instructions To solve a problem Written in easy to understand language – not strictly controlled Like a recipe in cooking Algorithm efficiency can be measured…improved? Algorithms are not programs

LETS GIVE THIS A GO How do you make a cup of tea?

HOW DID WE DO Did we miss anything? Is anything unclear? How efficient are we? Can we do better?

PSEUDO CODE More refined use of natural language Still user defined Often includes programming language like statements If this and this then do that End; Less ambiguous that natural language “Time flies like an arrow, fruit flies like a banana”

BACK TO MY CUP OF TEA Lets try and refine this a little more. Still a large step away from a program

WHY WRITE ALGORITHMS Aren’t they time consuming? Could we just use this time programming Programming prone to errors Algorithms help us design our program Avoid mistakes in our program – BUGS! Can be analysed, compared, and improved without costly implementation

POSSIBLY MORE USEFUL Lets try something a little more programming orientated Reading a text file containing a list of text

DID WE GET IT? Get file name Open file while(reading a line != end of file) Store string from line to memory Close the file – Important! Alternative Get file name Open file until(reading a line = end of file) do Store string from line to memory Close the file – Important!

CODE EXAMPLES Java: BufferedReader in = new BufferedReader(new FileReader( "infilename" )); String str; while ((str = in.readLine()) != null) { process (str); } in.close(); “process(str)” is a mathod that takes a String input parameter

CODE EXAMPLES C – FILE *fp; char str[128]; if ((fp = fopen(argv[ 1 ], "r"))==NULL) { printf("Cannot open file.\n"); exit(1); } while (!feof(fp)) { if (fgets(str, 126, fp)) printf("%s", str); } fclose(fp); return 0;

ALGORITHMS Not restricted to any one language Can (should) be readable by anyone with experience in any programming langauge. In fact readable by anyone at all Crucial for the design of good quality code

SORTING! Sorting another classical problem in computer science Many, many different algorithms developed Lists a lot of examples. Today we’re going to look at “Bubble sorting”

WHY SORT? Already seen that a sorted list can be searched must faster than an unsorted list Sort once. Improves search always. Adding an element to a sorted list is possible Insertion short

BUBBLE SORT  Pretty strange name  So called because the smaller values “bubble” up to the top of this list.  Slow – only practically used for very small sets of data

LETS TAKE A LOOK AT THE ALGORITHM Sorted = false; While sorted = false; Sorted = true; For all elements n in a list If n > n +1 Swap Sorted = false; N = n +1;

DEMONSTRATION

WHY IS IT SLOW

PRACTICALITY How do we do the swap? Array[i] = Array[i+1]; Array[i+1] = Array[i]; That wont work, we’re overwriting Array[i+1] before assigning its value to Array[i]. So what do we do.

PRACTICALITY Simples We can use a local variable int tmp; tmp = Array[i]; Array[i] = Array[i+1]; Array[i+1] = tmp; Now value of Array[i] is stored before being overwritten.

HOW CAN WE DO BETTER?  Example  Merge sort  Example of a recursive algorithm An algorithm that calls itself  Let me explain

ALGORITHM mergeSort( input:Array) If (Array.length > 1) arrayFirst = Array[0..n/2] arraySecond = Array[n/2+1..n] mergeSort(arrayFirst); mergeSort(arraySecond); Combine arrayFirst with arraySecond With elements in order

ALGORITHM mergeSort( input:Array) If (Array.length > 1) arrayFirst = Array[0..n/2] arraySecond = Array[n/2+1..n] mergeSort(arrayFirst); mergeSort(arraySecond); Combine arrayFirst with arraySecond With elements in order Recursive Calls

DEMONSTRATION

Now merge each list in the right order 16

DEMONSTRATION Now merge each list in the right order 16 3

DEMONSTRATION Now merge each list in the right order 16 35

DEMONSTRATION Now merge each list in the right order

DEMONSTRATION Now merge each list in the right order

DEMONSTRATION Now merge each list in the right order

DEMONSTRATION Now merge each list in the right order

DEMONSTRATION Now merge each list in the right order

DEMONSTRATION Now merge each list in the right order

DEMONSTRATION Now merge each list in the right order

DEMONSTRATION Now merge each list in the right order

DEMONSTRATION Now merge each list in the right order

DEMONSTRATION Now merge each list in the right order

DEMONSTRATION Now merge each list in the right order

DEMONSTRATION Now merge each list in the right order

DEMONSTRATION Now merge each list in the right order

DEMONSTRATION Now merge each list in the right order Sorted!

FASTER THAN BUBBLE SORT? Yes Sorting each recurrence takes 2*n/2+n time of the level above. Worst case O(n log n) Requires more memory than bubble sort