lecture4

lecture4 - Measuring Functions Recurrences and Recursion...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Measuring Functions Recurrences and Recursion IE170: Algorithms in Systems Engineering: Lecture 4 Jeff Linderoth Department of Industrial and Systems Engineering Lehigh University January 22, 2007 Jeff Linderoth IE170:Lecture 4 Measuring Functions Recurrences and Recursion ,O, Some Functions Youll See Everyone Gets an A! Go Bears! Jeff Linderoth IE170:Lecture 4 Measuring Functions Recurrences and Recursion ,O, Some Functions Youll See Taking Stock Last Time , O and Recursion. See recursion. Analyzing Recurrences This Time Analyzing a simple algorithm The impact of data structures Jeff Linderoth IE170:Lecture 4 Measuring Functions Recurrences and Recursion ,O, Some Functions Youll See A Canonical Problem Example: The Sorting Problem Input: A sequence of numbers a 1 , a 2 , . . . , a n Output: A reordering a 1 , a 2 , . . . , a n such that a 1 a 2 a n . Sorting in place : No new memory is allocated (or at least a constant amount of memory is allocated). (The input is usually overwritten by the output as the algorithm executes.) Sorting out of place : New memory must be allocated Jeff Linderoth IE170:Lecture 4 Measuring Functions Recurrences and Recursion ,O, Some Functions Youll See Sample. Reverse-Out-Of-Place In this case, we allocate a new array B public static void reverseOP(int A) { int n = A.length; int B = new int[n]; for (int j = 0; j < n; j++) { B[n-1-j] = A[j]; } System.arraycopy(B,0,A,0,n); } Jeff Linderoth IE170:Lecture 4 Measuring Functions Recurrences and Recursion ,O, Some Functions Youll See Sample. Reverse-In-Place Here everything is done directly on A public static void reverseIP(int A) { int n = A.length; for(int j = 0; j < n/2; j++){ // Swap A[j] and A[n-j-1] int t = A[j]; A[j] = A[n-j-1]; A[n-j-1] = t; } } Jeff Linderoth IE170:Lecture 4 Measuring Functions Recurrences and Recursion ,O, Some Functions Youll See Sorting: Some Java Code public static void iSortMe(int A) { for(int j = 1; j < A.length; j++) { int key = A[j]; int i = j-1; while(i >= 0 && A[i] > key) { A[i+1] = A[i]; i = i-1;...
View Full Document

Page1 / 6

lecture4 - Measuring Functions Recurrences and Recursion...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online