3.1 Algorithms (and real programming examples).

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
MATH 224 – Discrete Mathematics
One Dimensional Arrays
1 CS101 Introduction to Computing Lecture 17 Algorithms II.
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Chapter 2- Visual Basic Schneider
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
CSE115/ENGR160 Discrete Mathematics 02/24/11 Ming-Hsuan Yang UC Merced 1.
Simple Sorting Algorithms
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
Searching and Sorting SLA Computer Science 4/16/08 Allison Mishkin.
Algorithm Efficiency and Sorting
Searching and Sorting Arrays
The Program Design Phases
ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.
Algorithm & Flowchart.
Algorithms in Computer Science Topics in CS. Terms & Concepts in this Unit  algorithms  Big O Notation  flow-charts  pseudo-code  Search Algorithms.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
1. Reference  2  Algorithm :- Outline the essence of a computational procedure, step by step instructions.  Program :- an.
PROGRAMMING LANGUAGES Prof. Lani Cantonjos. PROGRAM - set of step-by-step instructions that tells or directs the computer what to do. PROGRAMMING LANGUAGE.
ดร.สุรศักดิ์ มังสิงห์ SPU, Computer Science Dept.
Software Life Cycle What Requirements Gathering, Problem definition
ALGORITHM List of instructions for carrying out some process step by step. A sequence of instructions which has a clear meaning and can performed with.
Algorithms & Flowchart
1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.
Chapter Algorithms 3.2 The Growth of Functions 3.3 Complexity of Algorithms 3.4 The Integers and Division 3.5 Primes and Greatest Common Divisors.
Algorithms 1.Notion of an algorithm 2.Properties of an algorithm 3.The GCD algorithm 4.Correctness of the GCD algorithm 5.Termination of the GCD algorithm.
Fundamentals of Algorithms MCS - 2 Lecture # 5. Representation of Algorithms (continued) Flowcharts.
ALGORITHMS.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Flowchart. a diagram of the sequence of movements or actions of people or things involved in a complex system or activity. a graphical representation.
Program Design & Development EE 201 C7-1 Spring
Algorithms and Flowcharts
Copyright Prentice Hall Modified by Sana odeh, NYU
The Bubble Sort Mr. Dave Clausen La Cañada High School
CSE15 Discrete Mathematics 03/06/17
CS212: Data Structures and Algorithms
Program design Program Design Process has 2 phases:
Tips and tools for creating and presenting wide format slides
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
ALGORITHMS part-1.
Data Structures and Algorithms
GC211Data Structure Lecture2 Sara Alhajjam.
Computer Programming Flowchart.
CS1371 Introduction to Computing for Engineers
Algorithm Analysis CSE 2011 Winter September 2018.
Design and Analysis of Algorithms
Unit# 9: Computer Program Development
Algorithms Chapter 3 With Question/Answer Animations
Algorithm design and Analysis
Describing algorithms in pseudo code
Lecture 11 Searching and Sorting Richard Gesick.
Algorithm Discovery and Design
Introduction to Algorithms and Programming
Search,Sort,Recursion.
Faculty of Computer Science & Information System
Chapter 2- Visual Basic Schneider
ME 142 Engineering Computation I
Problem Solving Designing Algorithms.
Principles of Computing – UFCFA3-30-1
Search,Sort,Recursion.
Revision of C++.
Developing a Program.
Basic Concepts of Algorithm
Principles of Computing – UFCFA3-30-1
WJEC GCSE Computer Science
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

3.1 Algorithms (and real programming examples)

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

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

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

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. 23 + 7 + 19 + 3 + 9 + 1 + 3 = 65

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

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

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.

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?

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

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

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

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

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.

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

Insertion Sort

(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 - -

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.)

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