[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14

Slides:



Advertisements
Similar presentations
The Important Thing About By. The Important Thing About ******** The important thing about ***** is *****. It is true s/he can *****, *****, and *****.
Advertisements

Java Control Statements
Recursion.
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Control Flow Statements: Repetition/Looping
Logical Operators and While Loops CS303E: Elements of Computers and Programming.
The "if structure" is used to execute statement(s) only if the given condition is satisfied.
The lambda calculus David Walker CS 441. the (untyped) lambda calculus a language of functions e ::= x | \x.e | e1 e2 v ::= \x.e there are several different.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Searching and Sorting Linear Search Binary Search Selection Sort
True/False. False True Subject May Go Here True / False ? Type correct answer here. Type incorrect answer here.
1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such.
 x (x 2  0) 1. True2. False.  x (3x + 2 = 12) 1. True2. False.
Searching and Sorting Arrays
Determine whether each curve below is the graph of a function of x. Select all answers that are graphs of functions of x:
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
Geography 465 Assignments, Conditionals, and Loops.
Chapter 8 ARRAYS Continued
8/9: Multiple-Subscripted Arrays About BinarySearch.java Multiple-Subscripted Arrays Program of the Day.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Searching Given a collection and an element (key) to find… Output –Print a message (“Found”, “Not Found) –Return a value (position of key ) Don’t modify.
Standard Algorithms –search for an item in an array –count items in an array –find the largest (or smallest) item in an array.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
4/17: Multiple-Subscripted Arrays About BinarySearch.java Multiple-Subscripted Arrays Program of the Day.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
int num = 22; if (num > 0) if (num % 5 == 0) System.out.println(num); else System.out.println(num + “ is negative”);
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
Lecture #3 Analysis of Recursive Algorithms
Title Category #1 Category #2 Category #3Category #
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
for( 起始條件 ; 判斷式 ; 條件運算 ){ // 迴圈內容 } while( 判斷式 ){ // 迴圈內容 } do{ // 迴圈內容 } while( 判斷式 ) ;
Searching and Sorting Arrays
Unit-1 Introduction to Java
Recitation 13 Searching and Sorting.
Control Statements Lecture 6 from Chapter 5.
Factors, multiple, primes: Factors from prime factors
Algorithm design and Analysis
البرمجة بلغة الفيجول بيسك ستوديو
Introduction to Programming
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
التسعير الفصل الرابع عشر.
searching Concept: Linear search Binary search
Chapter 9 One-Dimensional Arrays
Searching and Sorting Arrays
Search,Sort,Recursion.
IF Statements.
Three Special Structures – Case, Do While, and Do Until
True or False: {image} is one-to-one function.
Search,Sort,Recursion.
Program Flow.
Searching and Sorting Arrays
Module 8 – Searching & Sorting Algorithms
Compiler Construction
Notorious Bugs – BYTE, September byte
Composition & Inverses Review
Module 8 – Searching & Sorting Algorithms
Computer Science 101 A Survey of Computer Science
Factors, multiple, primes: Multiples
Shadows CSE 681.
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
SEQUENCE Start Initial situation Step 1 Step 2 Step 3 End
Standard Form: Multiplying powers of 10
If there is any case in which true premises lead to a false conclusion, the argument is invalid. Therefore this argument is INVALID.
Standard form: In standard form?
True or False True or False
If there is any case in which true premises lead to a false conclusion, the argument is invalid. Therefore this argument is INVALID.
Lesson 3. Controlling program flow. Loops. Methods. Arrays.

Presentation transcript:

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99

binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99

binSearch(52) 2 6 9 14 18 23 28 36 47 52 68 81 93 99 TARGET 52 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 TARGET 52

binSearch(52) 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > FIRST = 0 TARGET 52 FIRST

binSearch(52) 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > LAST > FIRST = 0 LAST = ARRAY.length – 1 TARGET 52 FIRST LAST 13

MIDDLE = (FIRST + LAST) / 2 TARGET 52 binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > MIDDLE > LAST > FIRST = 0 LAST = ARRAY.length – 1 MIDDLE = (FIRST + LAST) / 2 TARGET 52 FIRST LAST 13 MIDDLE 6

MIDDLE = (FIRST + LAST) / 2 FOUND = FALSE TARGET 52 binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > MIDDLE > LAST > FIRST = 0 LAST = ARRAY.length – 1 MIDDLE = (FIRST + LAST) / 2 FOUND = FALSE TARGET 52 FIRST LAST 13 MIDDLE 6 FOUND FALSE

loop while FIRST <= LAST AND FOUND = FALSE binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST LAST 13 MIDDLE 6 FOUND FALSE

loop while FIRST <= LAST AND FOUND = FALSE binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 81 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST LAST 13 MIDDLE 6 FOUND FALSE

loop while FIRST <= LAST AND FOUND = FALSE binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 81 MIDDLE > FIRST > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 7 LAST 13 MIDDLE 6 FOUND FALSE

loop while FIRST <= LAST AND FOUND = FALSE binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 81 MIDDLE > FIRST > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 7 LAST 13 MIDDLE 6 FOUND FALSE

loop while FIRST <= LAST AND FOUND = FALSE binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 7 LAST 13 MIDDLE 10 FOUND FALSE

loop while FIRST <= LAST AND FOUND = FALSE binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 7 LAST 13 MIDDLE 10 FOUND FALSE

loop while FIRST <= LAST AND FOUND = FALSE binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 7 LAST 13 MIDDLE 10 FOUND FALSE

loop while FIRST <= LAST AND FOUND = FALSE binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > LAST > MIDDLE > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 7 LAST 9 MIDDLE 10 FOUND FALSE

loop while FIRST <= LAST AND FOUND = FALSE binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > LAST > MIDDLE > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 7 LAST 9 MIDDLE 10 FOUND FALSE

loop while FIRST <= LAST AND FOUND = FALSE binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 7 LAST 9 MIDDLE 8 FOUND FALSE

loop while FIRST <= LAST AND FOUND = FALSE binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 7 LAST 9 MIDDLE 8 FOUND FALSE

loop while FIRST <= LAST AND FOUND = FALSE binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 7 LAST 9 MIDDLE 8 FOUND FALSE

loop while FIRST <= LAST AND FOUND = FALSE binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 68 81 MIDDLE > FIRST > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 9 LAST MIDDLE 8 FOUND FALSE

loop while FIRST <= LAST AND FOUND = FALSE binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 68 81 MIDDLE > FIRST > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 9 LAST MIDDLE 8 FOUND FALSE

loop while FIRST <= LAST AND FOUND = FALSE binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 9 LAST MIDDLE FOUND FALSE

loop while FIRST <= LAST AND FOUND = FALSE binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 9 LAST MIDDLE FOUND FALSE

loop while FIRST <= LAST AND FOUND = FALSE binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 9 LAST MIDDLE FOUND FALSE

loop while FIRST <= LAST AND FOUND = FALSE binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 9 LAST MIDDLE FOUND TRUE

binSearch(52) 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST > [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST > MIDDLE > LAST > if FOUND = TRUE return MIDDLE else return -1 end if TARGET 52 FIRST 9 LAST MIDDLE FOUND TRUE

binSearch(52) 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST > [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST > MIDDLE > LAST > if FOUND = TRUE return MIDDLE else return -1 end if TARGET 52 FIRST 9 LAST MIDDLE FOUND TRUE

binSearch(52) 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST > [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST > MIDDLE > LAST > binSearch(52) = 9 TARGET 52 FIRST 9 LAST MIDDLE FOUND TRUE