CS1020 Week 13: 16 th April 2015. Contents  Sit-in Lab #4 2.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
Longest Common Subsequence
HST 952 Computing for Biomedical Scientists Lecture 9.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Search and Recursion pt. 2 CS221 – 2/25/09. How to Implement Binary Search Take a sorted data-set to search and a key to search for Start at the mid-point.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Data Structures Review Session 1
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
1.7 Arrays academy.zariba.com 1. Lecture Content 1.Basic Operations with Arrays 2.Console Input & Output of Arrays 3.Iterating Over Arrays 4.List 5.Cloning.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Part 2. Searching Arrays Looking for a specific element in an array E.g., whether a certain score (85) is in a list of scores Linear search Binary search.
Recursion.
Vishnu Kotrajaras, PhD.1 Data Structures. Vishnu Kotrajaras, PhD.2 Introduction Why study data structure?  Can understand more code.  Can choose a correct.
COSC 2006 Data Structures I Recursion II
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
1 Searching and Sorting Linear Search Binary Search.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
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.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
Growth of Functions. Asymptotic Analysis for Algorithms T(n) = the maximum number of steps taken by an algorithm for any input of size n (worst-case runtime)
CS1101: Programming Methodology Aaron Tan.
CSC 211 Data Structures Lecture 13
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Binary search. What is binary search? We have  a sequence of data, sorted by a relation, and  an element to search for in the list We would like to.
Objectives At the end of the class, students are expected to be able to do the following: Understand the searching technique concept and the purpose of.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 2 Copyright © 2015, 2011, 2007 Pearson Education, Inc. Chapter Copyright © 2015, 2011, 2007 Pearson Education, Inc. Chapter 2-1 Solving Linear.
ALGORITHMS.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Vishnu Kotrajaras, PhD.1 Data Structures
Data Structures Arrays and Lists Part 2 More List Operations.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Searching Arrays Linear search Binary search small arrays
Using recursion for Searching and Sorting
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Chapter 5 - Control Structures: Part 2
Algorithms Chapter 3 With Question/Answer Animations
MSIS 655 Advanced Business Applications Programming
Searching: linear & binary
Search,Sort,Recursion.
Logical Operations In Matlab.
Given value and sorted array, find index.
Search,Sort,Recursion.
Presentation transcript:

CS1020 Week 13: 16 th April 2015

Contents  Sit-in Lab #4 2

Sit-in Lab #4 Set A – Word Scramble Set B – Numeric Sequence 3

Objective  Recursion Understanding the principles of recursion Understanding the basic requirements of a problem Formulating a recursive solution Figuring out the optimized way of solving a problem 4

Problem Statement  Word Scramble: Find a word in a character array  Input 1.A character array 2.A word  Objectives 1.Finding if the characters of the word are present in the character array as substring  All the letters of the word must be in consecutive positions in the array 2.Finding if the characters of the word are present in the character array as subsequence  All the letters of the word must be in same order in the array  Things to learn: 1.Using recursion 2.Using recursion in combination with loop Set A: Word Scramble 5

Search for the starting position of the word first Recursive Logic: Substring badtal k k re Character Array talk Word to search b b a a d d t t a a l l r r e e Recursively search for the next characters till end of array/word Set A: Word Scramble 6

Recursive Logic: Substring adtalre read Search for the starting position of the word first e e a a d d i i n n g g r r e e Recursively search for the next characters till end of array/word r r Search again for the starting position of the word in the rest of the array Set A: Word Scramble Character Array Word to search 7

Recursive Logic: Subsequence badtal k k re red Search for the starting position of the word first b b a a d d t t a a l l r r e e Recursively search for the next characters till end of array/word If a character mismatches, ignore it and continue to search in the remaining array Character Array Word to search Set A: Word Scramble 8

Program Flow findWordInArray 1.Find the position of first character of the word in array  i 2.Call a recursive method to find word in the array given the 1 st position  findWordInArrayRec(ar ray,word,i,1) 3.If returns True  Print i 4.Otherwise Continue from step 1 for remaining array findWordInArrayRec 1.Match array[i+1] and word[k] 2.If matches Call findWordInArrayRec( array,word,i+1,k+1) 3.Otherwise return false 4.Base Case: End of word has been reached  return True findWordInArrayRec 1.Match array[i+1] and word[k] 2.If matches Call findWordInArrayRec( array,word,i+1,k+1) 3.Otherwise Continue from step 1 for remaining array 4.Base Case: End of word has been reached  return True Substring search Subsequence search Set A: Word Scramble 9

Problem Statement  Numeric Sequence: Finding maximum value from a sequence  Input 1.An array integers  Objectives 1.Whether the array is sorted in increasing sequence or not  Determine whether an array is sorted, if yes, then print the maximum element 2.Find the maximum value from a sequence which is at first increasing then decreasing  Things to learn: 1.Using recursion 2.Using binary search appropriately Set B: Numeric Sequence 10

Recursive Logic: Check if sorted  To check whether a complete array is sorted or not you need to check whether every two consecutive elements are sorted or not  Note that for this problem it is necessary to do linear scan – we need to check every element Not Sorted!! 11 Set B: Numeric Sequence

Recursive Logic: Find maximum element  Property of the array: 1.The sequence is increasing at first and then decreasing 2.There are no duplicate elements Maximum element is the peak element  Last element of the increasing sequence/first element of decreasing element  It is greater than the element before it  It is also greater than the element after it  There is always a single element in the array satisfying both the above properties 12 Set B: Numeric Sequence

Recursive Logic: Find maximum element  Binary search: 1.If the element is peak element return the element // base case 2.If the current element belongs to increasing sequence (arr[i-1] < arr[i] < arr[i+1])  search on the right side of array 3.If the current element belongs to decreasing sequence (arr[i-1] > arr[i] > arr[i+1])  search on the left side of array Belongs to increasing sequence – search on right Belongs to decreasing sequence – search on left Peak element! 13 Set B: Numeric Sequence

Program Flow checkIfSorted 1.If array contains single element return true 2.If value at index < value at index+1, check the rest of the array (from index+1) 3.Otherwise return false findMax 1.If array contains single element return the element 2.If array contains 2 elements, return the greater element 3.Check mid-point of array i.If peak element, return the value ii.If belongs to increasing seq, findMax in right half iii.If belongs to decreasing seq, findMax in left half 14 Set B: Numeric Sequence

END OF FILE 15