int getThird(int *arr){ return arr[3]; } In all these examples “n” is the size of the input e.g. length of arr O(1) And what two things are wrong with.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

For(int i = 1; i
What is shape function ? shape function is a function that will give the displacements inside an element if its displacement at all the node locations.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
1 Powers of Two: Trace Ex. Print powers of 2 that are  2 N. Increment i from 0 to N. Double v each time. int i = 0; int v = 1; while (i
Sorted Lists CS Data Structures. Sorted List Specification (partial) InsertItem (ItemType item) Function: Adds item to list Preconditions: (1) List.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Sort, Search, and Running Time.
Insertion Sort By Daniel Tea. What is Insertion Sort? Simple sorting algorithm Builds the final list (or array) one at a time – A type of incremental.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Chars and strings Jordi Cortadella Department of Computer Science.
Crypto Project Sample Encrypted Data: –Turing: CS Public\CryptoProjectData Crypto_Short_Keys_P_U.out Crypto_Long_Keys_O_R.out Bonus +10 points on.
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.
ADSA: IntroAlgs/ Advanced Data Structures and Algorithms Objective –introduce algorithm design using basic searching and sorting, and remind.
 O(1) – constant time  The time is independent of n  O(log n) – logarithmic time  Usually the log is to the base 2  O(n) – linear time  O(n*logn)
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
Sorting 2 Taking an arbitrary permutation of n items and rearranging them into total order Sorting is, without doubt, the most fundamental algorithmic.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
Introduction to Programming (in C++) Sorting Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Big-O and Sorting February 6, Administrative Stuff Readings for today: Ch Readings for tomorrow: Ch 8.
Exercise 3 Example> Write a C function that implements /* input : integer value n output : */ int f ( int n ) { int i, sum=0; for (i=1;i
 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.
Processing Sequences of Elements Technical Trainer Telerik Corporation Doncho Minkov.
UNIT 10 Multidimensional Arrays.
private void Application_Launching(object sender, LaunchingEventArgs e) { } private void Application_Activated(object.
A: A: double “4” A: “34” 4.
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
کلاس های متغيرها در C 1- کلاس متغيرهای اتوماتيک auto int x = 10; /* x is global auto int x = 10; /* x is global void main( ) { void main( ) { int x = 25;
CS32 Discussion Section 1B Week 5 TA: Hao Yu (Cody)
Processing Sequences of Elements
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
Selection Sorting Pseudocode (Forward) for i = 0 to size - 2 find the index of the required element between s[i] and s[size - 1] If i not the same as index.
Bohyung Han CSE, POSTECH
Insertion sort Loop invariants Dynamic memory
C++ Memory Management – Homework Exercises
Michele Weigle - COMP 14 - Spr 04 Catie Welsh March 30, 2011
Introduction to Search Algorithms
Bill Tucker Austin Community College COSC 1315
More Computational Theory
Alg2_1c Extra Material for Alg2_1
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
Big-Oh and Execution Time: A Review
Review Operation Bingo
תרגול Introduction to C - Fall Amir Menczel.
Chapter 4: Types.
מיונים וחיפושים קרן כליף.
מיונים וחיפושים קרן כליף.
Stack Memory 2 (also called Call Stack)
ניתוח מערכות מידע תכנות ב C#
מערך חד מימדי חלק א היכרות.
מיונים וחיפושים קרן כליף.
Propositional Equivalences Rosen 5th and 6th Editions section 1.2
Searching and Sorting Arrays
Standard Input/Output Stream
Topics discussed in this section:
If Statements.
Example. Sort {5, 1, 12, -5, 16, 2, 12, 14} using selection sort. Complexity analysis.
Nate Brunelle Today: Conditional Decision Statements
CS150 Introduction to Computer Science 1
Variables and Computer Memory
(Dreaded) Quiz 2 Next Monday.
Running Time Exercises
public class Dillo {     public int length;     public boolean isDead;         
Arrays Wellesley College CS230 Lecture 02 Thursday, February 1
Sorting Taking an arbitrary permutation of n items and rearranging them into total order Sorting is, without doubt, the most fundamental algorithmic.
Recursive example 1 double power(double x, int n) // post: returns x^n
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

int getThird(int *arr){ return arr[3]; } In all these examples “n” is the size of the input e.g. length of arr O(1) And what two things are wrong with the function?

int getMin(int *arr, int n){ smallest = arr[0]; for (int i=0; i < n; ++i) { if (arr[i] < smallest) { smallest = arr[i]; } return smallest ; } O(n)

int * f1(int *arr, int n): int *result = new int[n]; for (int i=0; i < n; ++i){ result[i] = arr[i] – getMin(arr); } return result; } O(n 2 )

int f2(int *arr, int n){ if (n > 0){ return arr[n - 1]; } else{ return -1; } O(1)

double f3(double *arr, int n): double diff = 0; int ln = n; for (int i = 0; i < ln; ++i){ double total = 0.0; for (int j = 0; j < ln; ++j){ total += arr[j]; } diff += arr[i] – total / ln; } return diff; } O(n 2 )

double f3(double *arr, int n): double diff = 0; double avg = 0; for (int i=0; i < n; ++i){ avg += arr[i]; } avg = avg / n; for (int j=0; j < n; ++j){ diff += arr[j] - avg; } return diff; } O(n)

int f4(int *arr, int n, int m){ if (m > n – 1){ return 0; } else if (m == n – 1){ return arr[m]; } else{ return arr[m] + f4(arr, n, m + 1); } O(n)

int f5(int *arr, int n){ int result = 0; int i = 0; ln = n; while (i < ln / 2){ result += arr[i]; i += 1; while (i >= ln / 2 && i < ln){ result += arr[i]; i += 1; } return result; } O(n)

bool alpha(string s){ int ln = s.size(); for (int i = 0; i < ln - 1; ++i){ if (s[i] > s[i + 1]){ return false; } return true; } Here n is the length of the String s worst case: ? best case: ? average case: ?