Introduction to: Programming CS105 Lecture: Yang Mu.

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University
MATH 224 – Discrete Mathematics
Introduction to Computer Science Theory
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Analysis of Algorithms
Algorithm Analysis.
Lecture: Algorithmic complexity
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Analysys & Complexity of Algorithms Big Oh Notation.
the fourth iteration of this loop is shown here
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
Analysis of Algorithms. Time and space To analyze an algorithm means: –developing a formula for predicting how fast an algorithm is, based on the size.
Runtime Analysis CSC 172 SPRING 2002 LECTURE 9 RUNNING TIME A program or algorithm has running time T(n), where n is the measure of the size of the input.
Complexity Analysis (Part II)
Cmpt-225 Algorithm Efficiency.
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Elementary Data Structures and Algorithms
1 Section 2.3 Complexity of Algorithms. 2 Computational Complexity Measure of algorithm efficiency in terms of: –Time: how long it takes computer to solve.
CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a lgorithms.
Week 2 CS 361: Advanced Data Structures and Algorithms
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Analysis of Algorithms
Chapter 19: Searching and Sorting Algorithms
Algorithm Evaluation. What’s an algorithm? a clearly specified set of simple instructions to be followed to solve a problem a way of doing something What.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
CSC 205 Java Programming II Algorithm Efficiency.
Ch 18 – Big-O Notation: Sorting & Searching Efficiencies Our interest in the efficiency of an algorithm is based on solving problems of large size. If.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Asymptotic Analysis-Ch. 3
Asymptotic Notation (O, Ω, )
Sorting and Searching Pepper. Common Collection and Array Actions Sort in a certain order ◦ Max ◦ Min Shuffle Search ◦ Sequential (contains) ◦ Binary.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
MS 101: Algorithms Instructor Neelima Gupta
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Computer Science and Software Engineering University of Wisconsin - Platteville 8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this.
3.3 Complexity of Algorithms
1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation.
Algorithm Analysis (Big O)
Scalability for Search Scaling means how a system must grow if resources or work grows –Scalability is the ability of a system, network, or process, to.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
Searching Topics Sequential Search Binary Search.
CISC220 Spring 2010 James Atlas Lecture 07: Big O Notation.
DS.A.1 Algorithm Analysis Chapter 2 Overview Definitions of Big-Oh and Other Notations Common Functions and Growth Rates Simple Model of Computation Worst.
A Introduction to Computing II Lecture 5: Complexity of Algorithms Fall Session 2000.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
Section 1.7 Comparing Algorithms: Big-O Analysis.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Introduction to complexity
Introduction to Algorithms
Building Java Programs
CSE 143 Lecture 5 Binary search; complexity reading:
CSE 143 Lecture 5 More Stacks and Queues; Complexity (Big-Oh)
Midterm 2 review.
Building Java Programs
Searching, Sorting, and Asymptotic Complexity
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Estimating Algorithm Performance
Presentation transcript:

Introduction to: Programming CS105 Lecture: Yang Mu

Data types Three categories of primitive data types: numerical, string, and boolean. A numerical variable represents a number. A string variable represents text, for example "2" and "asdf". A boolean represents either true or false.

Expressions and Statements Expression: evaluates a value Statement: does not return a value

Expression Three general kinds of expressions are numerical expressions, boolean expressions, and string expressions. Expression contains: terms and operators. Function calls are always expressions.

Statement A statement does NOT return a value, but does something. Every statement is one or more lines of code. Every expression is always inside a statement.

Assignment statements Assignment statements look like variable_name = some_expression. some_expression is ALWAYS some kind of expression. After this statement is run, variable_name now stores the value of some_expression. For example, a_variable=4+6 assigns the value 10 to a_variable. a_variable + 5 now yields 15, and print(a_variable) will print out ?. A common type of assignment statement looks like var = var + 1. This takes the current value of var, adds one to it, then assigns that value back to the variable var. For example, if var was 5 before this line of code runs, then var will be ?

Flow control statements if statements, while loops, and for loops, because they make the program skip or repeat some lines of code.

if statements if (boolean_expression) { // some code } else { // other code } if (x>70) { print("you pass") } else { print("you fail") } if statements branch, depending on a boolean expression.

while loops while (boolean_expression) { // some code } i=3 while (i>0) { i=i-1 print(i) } while loops keep running some code while a boolean expression is true.

for loops for (assignment_statement; boolean_expression; statement) { // some code } for (i=3; i>0; i=i-1) { print(i) } for loops usually are a more concise form of a while loop.

Sorting Bubble sortSelection sortInsertion sort

Merge Sort

Compare different sorting algorithms Using Big O notation. Big-O notation is a way of ranking about how much time it takes for an algorithm to execute How many operations will be done when the program is executed?

Big-O Big-O of a function f(n) is O(f(n)). Using the Big-O on a function throws away everything but the largest power of n. The idea behind this is to make analysis simpler. For example, O(n 2 + n) = O(n 2 ). O(2n) = O(n). O(19) = O(1).

Big-O We use Big-O to classify the runtime growth rate of algorithms (how fast an algorithm is depending on the size of its input). In the best case, bubblesort visit each element 1 time. It is O(n) In the worst case, bubblesort visit each element n times. It is O(n 2 ). In the average case, bubblesort visit each element n/2 times. It is O(n 2 ). Hence, we can say bubblesort is O(n 2 ). Mergesort visits each element log 2 (n) times. Hence, mergesort is O(n*log 2 (n)).

Hierarchy of runtimes Big-ONameSpeedComputations O(1)Constant runtime Very fast, does not depend on size of input. Very few, does not depend on size of input O(log(n))Logarithmic runtimeVery fast. Few, depends only a little bit on size of input O(n)Linear runtimeFast. Some, depending linearly on size of input O(n*log(n))n*log(n) runtimeAcceptable. Depends more on size of input O(n 2 )Quadratic runtimeSlow. A lot, depending on size of input. If n doubles, runtime quadruples. O(n 3 ), O(n 4 ),...Polynomial runtimeVery slow. Way too many computations, not very scalable. O(2 n )Exponential runtimeIntractable (worst). Ridiculous amount of computations, cannot scale.

Hierarchy of runtimes Hierarchy of runtimes (lower is faster / more scalable and better). From

Practice What is the time complexity for searching an element from an array? O(n) What is the time complexity for searching an element from a sorted array? O(log 2 (n)) at best What is the time complexity to get the median of an array? O(nlog 2 (n)) What is the time complexity to get the median of a sorted array? O(1) What is the time complexity to get the median of two arrays? O((m+n)log 2 (m+n)) What is the time complexity to get the median of two sorted arrays? O((m+n)log 2 (m+n)) -> O(m+n) -> O(log 2 (m+n))