Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
Sorting Really Big Files Sorting Part 3. Using K Temporary Files Given  N records in file F  M records will fit into internal memory  Use K temp files,
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Algorithmic Thinking What’s the Plan? lawrence snyder c h a p t e r 10.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 9: Searching, Sorting, and Algorithm Analysis
Learning Objectives Explain similarities and differences among algorithms, programs, and heuristic solutions List the five essential properties of an algorithm.
Chapter 10 Algorithmic Thinking. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Learning Objectives List the five essential.
Chapter 10 What's The Plan?: Algorithmic Thinking.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Computers CS Dr. Zhizhang Shen Chapter 10: How to.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Searching and Sorting Topics  Sequential Search on an Unordered File  Sequential Search on an Ordered File  Binary Search  Bubble Sort  Insertion.
Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort.
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
CSE115/ENGR160 Discrete Mathematics 02/24/11 Ming-Hsuan Yang UC Merced 1.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
Sorting Chapter 10.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
COMP s1 Computing 2 Complexity
Algorithms Algorithms are a familiar idea. Our goal is to learn to specify them right so someone or something else does the work.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
Some quotes…. I really hate this damned machine I wish that they would sell it. It never does quite what I want But only what I tell it. ~Anon These machines.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
Simple Iterative Sorting Sorting as a means to study data structures and algorithms Historical notes Swapping records Swapping pointers to records Description,
Algorithms & Flowchart
Algorithms Algorithms are a familiar idea. Our goal is to learn to specify them right so someone or something else does the work.
Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
To know and use the Bubble Sort and Shuttle Sort Algorithms.
1 Algorithms CS 202 Epp section ??? Aaron Bloomfield.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Today’s Announcements Assignment 8 is due Project 2 is due 7/27/04 We will be skipping chapter 22 Test 3 (chapters 18-21) will be on 7/29/04 Tip of the.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Announcements: Project 5 Everything we have been learning thus far will enable us to solve interesting problems Project 5 will focus on applying the skills.
SNU OOPSLA Lab. 1 Great Ideas of CS with Java Part 1 WWW & Computer programming in the language Java Ch 1: The World Wide Web Ch 2: Watch out: Here comes.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
ALGORITHMS.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 10 Algorithmic Thinking. Learning Objectives Explain similarities and differences among algorithms, programs, and heuristic solutions List the.
Searching Topics Sequential Search Binary Search.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Can you put these children’s TV shows in date order - earliest to latest?
Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Experimental Study on the Five Sort Algorithms You Yang, Ping Yu, Yan Gan School of Computer and Information Science Chongqing Normal University Chongqing,
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Document that explains the chosen concept to the animator 1.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Based on Lawrence Snyder’s slides for UW-Seattle. This notice serves as permission for their use for teaching in public or private (not for profit) schools.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
Sorting With Priority Queue In-place Extra O(N) space
3.3 Fundamentals of data representation
Algorithm Analysis CSE 2011 Winter September 2018.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Analysis of Bubble Sort and Loop Invariant
CSCI 1250 Information Technologies
Chapter 10: Algorithm TECH Prof. Jeff Cheng.
What's The Plan?: Algorithmic Thinking
What's The Plan?: Algorithmic Thinking
What's The Plan?: Algorithmic Thinking
Presentation transcript:

Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004

 Def. An algorithm is a precise, systematic process directing an agent to produce a specified result  Five properties characterize algorithms ▪ Input specified – tell form and amount of input required ▪ Output specified – tell form and amount of output produced ▪ Definiteness – say explicitly what to do & in what order ▪ Effectiveness – operations within agent’s abilities ▪ Finiteness – will stop and give an answer or say “none”  Programs are algorithms 3/17/2016© Larry Snyder, CSE2

 There are always different algorithms to pick  Consider sorting …  Put items in order  Exchange Sort: visit all items, starting with first; for each item, compare it with all following, exchanging any out of order pairs … Steadily finds 1 st smallest, then 2 nd smallest, then … 3/17/2016© Larry Snyder, CSE

 Bubble Sort: go through list in pairs, correcting out of order pairs; can progressively stop earlier and earlier …  Pushes largest to the end, then pushes second largest to “next to end’ position, then pushes … 3/17/2016© Larry Snyder, CSE

 The two algorithms take the same amount of time, but they are different as we see from the patterns of their comparisons 3/17/2016© Larry Snyder, CSE5

 How do we know that the algorithms work?  Developing algorithms is not just thinking them up  It is also reasoning through why they work … you need to know why explicitly enough to tell someone else  Let’s see how to do that 3/17/2016© Larry Snyder, CSE6

 Say What You’re Claiming: “Exchange Sort Puts Numbers or Words in Ascending Order”  It’s not automatically obvious 3/17/2016© Larry Snyder, CSE7

 Formulate a way to see why it does work  Explain how “big picture-wise” … use analogy  Cookie “Spritzer” or Power Tool 3/17/2016© Larry Snyder, CSE8 Numbers Go In At Start Find Smallest _____________________ Smallest Item Comes Out Press For Next One The Minimizer

 If Minimizer Sorts, So Does Exchange Sort  The Operation of Exchange Sort Works Like The Minimizer … each pass “emits” the smallest item 3/17/2016© Larry Snyder, CSE

 Recall, Bubble Sort “pushes” the largest as far as possible to the right 3/17/2016© Larry Snyder, CSE

 Say What You’re Claiming: “Bubble Sort Puts Numbers or Words in Ascending Order”  Explain how to do it “big picture-wise” … use an analogy: 3/17/2016© Larry Snyder, CSE11 Numbers Go In At Start Find Largest ___________________ Largest Item Comes Out Press For Next One The Maximizer

 If The Maximizer Sorts then so does Bubble sort … the operation works like the maxi- mizer 3/17/2016© Larry Snyder, CSE

 What is the complexity of the sorting algorithms that we just looked at?  Often has two parts: get item in order; repeat  Exchange – interchange to put smallest earlier  Bubble – compare adjacent values, push largest further on  How many times do we do that  Be careful, not exactly how many times but what “order” … 3/17/2016© Larry Snyder, CSE13

 This exchange sort needs = 10 compares for 5 items  Generally for n items (n-1) + (n-2) + … = n(n-1)/2 = 1/2 (n 2 –n) 3/17/2016© Larry Snyder, CSE

 Consider another way to sort  Merging two sorted arrays into a single sorted array is straight forward 3/17/2016© Larry Snyder, CSE15

/17/2016© Larry Snyder, CSE16

/17/2016© Larry Snyder, CSE17

/17/2016© Larry Snyder, CSE18

/17/2016© Larry Snyder, CSE19

/17/2016© Larry Snyder, CSE20

/17/2016© Larry Snyder, CSE21

/17/2016© Larry Snyder, CSE22

/17/2016© Larry Snyder, CSE23

/17/2016© Larry Snyder, CSE24

/17/2016© Larry Snyder, CSE25

/17/2016© Larry Snyder, CSE26

/17/2016© Larry Snyder, CSE27

/17/2016© Larry Snyder, CSE28

/17/2016© Larry Snyder, CSE29

/17/2016© Larry Snyder, CSE30

/17/2016© Larry Snyder, CSE31

/17/2016© Larry Snyder, CSE32

/17/2016© Larry Snyder, CSE33

/17/2016© Larry Snyder, CSE34

/17/2016© Larry Snyder, CSE35

/17/2016© Larry Snyder, CSE36

n n/2 n/4 n/8 log 2 n n Complexity  nlog 2 n 3/17/2016© Larry Snyder, CSE37

 The binary code computers execute are algorithms  Software developers create algorithms all the time  Using languages like C, Processing, JavaScript, etc.  A compiler (it’s a translator) converts to binary code  These cases specify computation in complete detail because computers are clueless  But at other levels the agent is a person … 3/17/2016© 2011 Larry Snyder, CSE38

 In their paper Larry Page and Sergey Brin gave their algorithm for processing a Google query  Algorithms are “given” at many levels of detail – it depends on what agent needs 3/17/2016© 2011 Larry Snyder, CSE39

3/17/2016© 2011 Larry Snyder, CSE40 1.Place a marker at head of every list 2.If all markers point to same URL, record a hit 3.Advance marker on alphabetically earliest list(s) 4.If any list is finished stop; otherwise go to step 2

3/17/2016© 2011 Larry Snyder, CSE41

 Think about what makes the algorithm work?  A barrier is an imaginary ragged line across all lists marking the position of a URL that is a hit:  At Step 2 all markers are at a barrier 3/17/2016© 2011 Larry Snyder, CSE42 1.Place a marker at head of every list 2.If all markers point to same URL, record a hit 3.Advance marker on alphabetically earliest list(s) 4.If any list is finished stop; otherwise go to step 2

 Points to notice:  IAL starts at a barrier (1)  All markers ‘step across’ barrier together  No marker crosses w/o others 3/17/2016© 2011 Larry Snyder, CSE43

 It is not sufficient to think up a clever algorithm … you need to know why it works  It’s usually not tough, because the logic of your method typically translates into an explanation of why it works. But you must think about it!!  Once you know it works – CS people figure out how fast it is! 3/17/2016© Larry Snyder, CSE44