Presentation is loading. Please wait.

Presentation is loading. Please wait.

3.1 Algorithms (and real programming examples).

Similar presentations


Presentation on theme: "3.1 Algorithms (and real programming examples)."— Presentation transcript:

1 3.1 Algorithms (and real programming examples)

2 List U={23, 7, 19, - 4, 3, 4, 12, 9, 8, 1, 2, 3 } //this list is: unsorted order List S = { 1, 2, 3, 3, 5, 7, 9, 10, 12, 15, 17, 20} //this list is: non-decreasing sorted order List D = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1} //this list is: decreasing sorted order List P = {7, 4, 3, 2, 1, 7, 1, 2, 4, 9} //this list is: unsorted order

3 The Easy Problems: For the problems listed below, use the following lists, and at this point only determine the answer: List U={23, 7, 19, - 4, 3, 4, 12, 9, 8, 1, 2, 3 } List S = { 1, 2, 3, 3, 5, 7, 9, 10, 12, 15, 17, 20} List D = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1} Problems to solve: Find the minimum (smallest) value in list S. 1 Find the minimum (smallest) value in list D. -1 Find the maximum (largest) value in list U. 23

4 The Easy Problems: For the problems listed below, use the following lists, and at this point only determine the answer: List U={23, 7, 19, - 4, 3, 4, 12, 9, 8, 1, 2, 3 } List S = { 1, 2, 3, 3, 5, 7, 9, 10, 12, 15, 17, 20} List D = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1} Problems to solve: Determine how many even numbered values are in list U. 5 Determine how many even numbered values are in list S. 4 Search for an integer value x within list U. (x = 8) found Search for an integer value x within list S. (x=8) not found

5 The Easy Problems: For the problems listed below, use the following lists, and at this point only determine the answer: List U={23, 7, 19, - 4, 3, 4, 12, 9, 8, 1, 2, 3 } List S = { 1, 2, 3, 3, 5, 7, 9, 10, 12, 15, 17, 20} List D = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1} Problems to solve: Search for an integer value x within list U. x=3 found (But, how many times was it found? And where?) found 2 times, in positions: 5, 12 Sum only the odd numbered values in list U = 65

6 The Easy Problems: For the problems listed below, use the following lists, and at this point only determine the answer: List U={23, 7, 19, - 4, 3, 4, 12, 9, 8, 1, 2, 3 } List S = { 1, 2, 3, 3, 5, 7, 9, 10, 12, 15, 17, 20} List D = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1} List P = {7, 4, 3, 2, 1, 7, 1, 2, 4, 9} Problems to solve: Determine if the pattern “1, 2, 3” is in List U.  true Determine if the pattern “1, 2, 3” and “3, 2, 1” is in List P . false

7 Documenting what your brain just did… so even a computer could solve these problems.

8 Algorithm: a finite set of precise instructions for solving a problem.
Terms to know… Algorithm: a finite set of precise instructions for solving a problem. Methods for representing algorithms: Pseudo-code: utilizing natural language to describe the steps of a process, leaving out coding requirements, such as variable declarations. Flowcharting: utilizing a standard set of symbols to describe the flow of a process.

9 Loops process body on TRUE
Elongated Circle: used to illustrate “start” and “stop” of algorithm Parallelogram: used to illustrate input to the process, or output from process Rectangle: used to illustrate a single step/direction/command in the process Diamond: used to illustrate a decision/branching point in the process Note: only 2 possible branches - true (T) or false (F) MOST IMPORTANT! Arrows: used to illustrate the order “flow” in which each of the steps are to be executed. Loops process body on TRUE What’s happening here?

10 Assign the first value of the list to be the minVal set i=1
Start Find the minimum value in a list: Assign the first value of the list to be the minVal set i=1 (to index list) Note: first val in list is at index 0 Input List increment i list i != end of list T F If (minVal > list i ) Display minVal F T Stop assign listi to minVal

11 loops run on true Start Find set i=1
the minimum value in a list: Assign the first value of the list to be the minVal set i=1 (to index list) Note: first val in list is at index 0 Input List increment i list i == end of list loops run on true F T If (minVal > list i ) Display minVal F T Stop assign listi to minVal

12 How can Raptor Help you? See link on course home page
(flowchart tester) How can Raptor Help you? See link on course home page

13 Is there only one correct way to solve a problem?
(sorting algorithms)

14 The Easy Problems: For the problems listed below, use the following lists, and at this point only determine the answer: List U={23, 7, 19, - 4, 3, 4, 12, 9, 8, 1, 2, 3 } Problem to solve: Sort list U in non-decreasing order.  { -4, 1, 2, 3, 3, 4, 7, 8, 9, 12, 19, 23} 23, 7, 19, - 4, 3, 4, 12, 9, 8, 1, 2, 3 Swap Process: temp = firstVal firstVal = secondVal secondVal = temp The Selection Sort 7 1 2 23 19 -4 Find the minimum value and swap it into 1st position, then move 1st position over by one, repeat with new (shorter) list.

15 The Easy Problems: For the problems listed below, use the following lists, and at this point only determine the answer: List U={23, 7, 19, - 4, 3, 4, 12, 9, 8, 1, 2, 3 } Problem to solve: Sort list U in non-decreasing order.  { -4, 1, 2, 3, 3, 4, 7, 8, 9, 12, 19, 23} 23, 7, 19, - 4, 3, 4, 12, 9, 8, 1, 2, 3 WHITE:SORTED BLACK: UNSORTED -4,1,2,3,23,4,12,9,8,7,19,3 -4,1,2,3,3,4,12,9,8,7,19,23 -4,1,2,3,3,4,7,9,8,12,19,23 -4,1,2,3,3,4,7,8,9,12,19,23 -4 7 19 1 2 23 3

16 Insertion Sort

17 (list inPlace < list inPlace-1)
Start Input List set i=1 (to index list) Insertion Sort non-decreasing order i++ i < lengthoflist T F inPlace = i If inPlace > 0 && (list inPlace < list inPlace-1) Display list F T Stop Swap list inPlace and list inPlace-1 inPlace - -

18 Bubble Sort ( musical demonstration <-click here) Note:
only pairs of values are compared with each pass through the entire list and the list must be passed through n-1 times, where n equals the number of elements in the list ( musical demonstration <-click here) (Other sorts are demonstrated as well, check out the other links.)

19 Let’s solve some problems…
using flowcharting, try solving the problems listed in the lecture guide.


Download ppt "3.1 Algorithms (and real programming examples)."

Similar presentations


Ads by Google