810 likes | 904 Views
COP 3503 FALL 2012 Shayan Javed Lecture 15. Programming Fundamentals using Java. Algorithms Searching. So far. Focused on Object-Oriented concepts in Java Going to look at some basic computer science concepts: algorithms and more data structures. Algorithm.
E N D
COP 3503 FALL 2012ShayanJavedLecture 15 Programming Fundamentals using Java
Algorithms Searching
So far... • Focused on Object-Oriented concepts in Java • Going to look at some basic computer science concepts: algorithms and more data structures.
Algorithm An algorithm is an effective procedure for solving a problem expressed as a finite sequence of instructions. “effective procedure” = program
Pseudocode • A compact and informal description of algorithms using the structural conventions of a programming language and meant for human reading.
Pseudocode • A compact and informal description of algorithms using the structural conventions of a programming language and meant for human reading. • A text-based design tool that helps programmers to develop algorithms.
Pseudocode • A compact and informal description of algorithms using the structural conventions of a programming language and meant for human reading. • A text-based design tool that helps programmers to develop algorithms. • Basically, write out the algorithm in steps which humans can understand • and can be converted to a programming language
Searching • Ability to search data is extremely crucial.
Searching • Ability to search data is extremely crucial. • Searching the whole internet is always a problem. (Google/Bing/...Altavista).
Searching • Ability to search data is extremely crucial. • Searching the whole internet is always a problem. (Google/Bing/...Altavista). • Too complex of course.
Searching • The problem: • Search an array A.
Searching • The problem: • Search an array A. • N – Number of elements in the array
Searching • The problem: • Search an array A. • N – Number of elements in the array • k – the value to be searched
Searching • The problem: • Search an array A. • N – Number of elements in the array • k – the value to be searched • Does k appear in A?
Searching • The problem: • Search an array A. • N – Number of elements in the array • k – the value to be searched • Does k appear in A? • Output: Return index of k in A
Searching • The problem: • Search an array A. • N – Number of elements in the array • k – the value to be searched • Does k appear in A? • Output: Return index of k in A • Return -1 if k does not appear in A
Linear Search • How would you search this array of integers? • (For ex., we want to search for 87)
Linear Search • How would you search this array of integers? • (For ex., we want to search for 87) • Pseudocode: for i = 0 till N: if A[i] == k return i return -1
Linear Search intlinearSearch (int[] A, int key, int start, int end) { for (inti = start; i < end; i++) { if (key == A[i]) { returni; // found - return index } } // key not found return -1; }
Linear Search intlinearSearch (int[] A, int key, int start, int end) { for (inti = start; i < end; i++) { if (key == A[i]) { returni; // found - return index } } // key not found return -1; } int index = linearSearch(A, 87, 0, A.length);
Linear Search • Advantages: • Straightforward algorithm.
Linear Search • Advantages: • Straightforward algorithm. • Array can be in any order.
Linear Search • Advantages: • Straightforward algorithm. • Array can be in any order. • Disadvantages: • Slow and inefficient (if array is very large)
Linear Search • Advantages: • Straightforward algorithm. • Array can be in any order. • Disadvantages: • Slow and inefficient (if array is very large) • N/2 elements on average (4 comparisons for 45)
Linear Search • Advantages: • Straightforward algorithm. • Array can be in any order. • Disadvantages: • Slow and inefficient (if array is very large) • N/2 elements on average (4 comparisons for 45) • Have to go through N elements in worst-case
Algorithm Efficiency • How do we judge if an algorithm is good enough?
Algorithm Efficiency • How do we judge if an algorithm is good enough? • Need a way to describe algorithm efficiency.
Algorithm Efficiency • How do we judge if an algorithm is good enough? • Need a way to describe algorithm efficiency. • We use the Big O notation
Big O notation • Estimates the execution time in relation to the input size.
Big O notation • Estimates the execution time in relation to the input size. • Upper bound on the growth rate of the function. (In terms of the worst-case)
Big O notation • Estimates the execution time in relation to the input size. • Upper bound on the growth rate of the function. (In terms of the worst-case) • Written as O(x), where x = in terms of the input size.
Big O notation • If execution time is not related to input size, algorithm takes constant time: O(1).
Big O notation • If execution time is not related to input size, algorithm takes constant time: O(1). • Ex.: Looking up value in an array – A[3]
Big O notation • If execution time is not related to input size, algorithm takes constant time: O(1). • Ex.: Looking up value in an array – A[3] • Big O for Linear Search?
Big O notation • If execution time is not related to input size, algorithm takes constant time: O(1). • Ex.: Looking up value in an array – A[3] • Big O for Linear Search? • O(N) = have to search through at least N values of the Array A
Big O notation • If execution time is not related to input size, algorithm takes constant time: O(1). • Ex.: Looking up value in an array – A[3] • Big O for Linear Search? • O(N) = have to search through at least N values of the Array A • Execution time proportional to the size of the array.
Big O notation • Another example problem: • Find max value in an array A of size N.
Big O notation • Another example problem: • Find max value in an array A of size N. int max = A[0]; for i = 1 till i = N if (A[i] > max) max = A[i]
Big O notation • Another example problem: • Find max value in an array A of size N. int max = A[0]; for i = 1 till i = N if (A[i] > max) max = A[i] How many comparisons? N – 1 comparisons Efficiency:O(n-1) O(n) [Ignore non-dominating part]
Big O notation • Another example problem: • Find max value in an array A of size N. int max = A[0]; for i = 1 till i = N if (A[i] > max) max = A[i] How many comparisons? N – 1 comparisons Efficiency:O(n-1) O(n) [Ignore non-dominating part]
Big O notation • Another example problem: • Find max value in an array A of size N. int max = A[0]; for i = 1 till i = N if (A[i] > max) max = A[i] How many comparisons? N – 1 comparisons Efficiency:O(N-1) O(n) [Ignore non-dominating part]
Big O notation • Another example problem: • Find max value in an array A of size N. int max = A[0]; for i = 1 till i = N if (A[i] > max) max = A[i] How many comparisons? N – 1 comparisons Efficiency:O(N-1) O(N) [Ignore non-dominating part]
Back to Search... • Linear Search is the simplest way to search an array.
Back to Search... • Linear Search is the simplest way to search an array. • Other data structures will make it easier to search for data.
Back to Search... • Linear Search is the simplest way to search an array. • Other data structures will make it easier to search for data. • But what if data is sorted?
Search • Array A of size 6: (search for k = 87) • A sorted:
Search • A sorted: k = 87 • How can you search this more efficiently?
Search • A sorted: k = 87 • How can you search this more efficiently? • Let’s start by looking at the middle index: I = N/2 = 3
Search • A sorted: k = 87 • How can you search this more efficiently? • Let’s start by looking at the middle index: I = N/2 = 3
Search • A sorted: k = 87, I = 3 • is 87 == A[I] ? • No.