Catie Welsh April 20, 2011.  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

College of Information Technology & Design
CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
CSE Lecture 3 – Algorithms I
Session 3 Algorithm. Algorithm Algorithm is a set of steps that are performed to solve a problem. The example below describes an algorithm Example Check.
CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Arrays Arrays are data structures consisting of data items of the same type. Arrays are ‘static’ entities, in that they remain the same size once they.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
1 Searching Algorithms Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p
Searching Arrays Linear search Binary search small arrays
Analysis of Algorithm.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
Chapter 8 ARRAYS Continued
Introduction to Arrays. Useful Array Operations  Finding the Highest Value int [] numbers = new int[50]; int highest = numbers[0]; for (int i = 1; i.
One Dimensional Array. Introduction to Arrays Primitive variables are designed to hold only one value at a time. Arrays allow us to create a collection.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Building Java Programs Chapter 13 Searching reading: 13.3.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)
CS 106 Introduction to Computer Science I 03 / 19 / 2007 Instructor: Michael Eckmann.
Applications of Arrays (Searching and Sorting) and Strings
1 Searching. 2 Searching Searching refers to the operation of finding an item from a list of items based on some key value. Two Searching Methods (1)
Lecture 12. Searching Algorithms and its analysis 1.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
1 Linear and Binary Search Instructor: Mainak Chaudhuri
1 Searching and Sorting Linear Search Binary Search.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
SEARCHING.  This is a technique for searching a particular element in sequential manner until the desired element is found.  If an element is found.
COMP Recursion, Searching, and Selection Yi Hong June 12, 2015.
Catie Welsh March 28,  Lab 7 due Friday, April 1st, 1pm 2.
1 Traversal algorithms. 2 Array traversal traversal: An examination of each element of an array. Traversal algorithms often takes the following form:
Searching Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
COMP 110 Arrays Luv Kohli November 5, 2008 MWF 2-2:50 pm Sitterson 014.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
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.
Searching and Sorting Arrays
Michele Weigle - COMP 14 - Spr 04 Catie Welsh March 30, 2011
Chapter 7 Single-Dimensional Arrays
Lecture 14: binary search and complexity reading:
Lecture 15: binary search reading:
Searching and Sorting Arrays
UNIT – V PART - I Searching By B VENKATESWARLU, CSE Dept.
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
Given value and sorted array, find index.
Announcements Lab 6 was due today Lab 7 assigned this Friday
Searching and Sorting Arrays
Lecture 4: Introduction to Code Analysis
Module 8 – Searching & Sorting Algorithms
Sorting Algorithms.
Presentation transcript:

Catie Welsh April 20, 2011

 Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday 2

3

 Search Algorithms 4

 The array itself is referred to by the name scores (in this particular case) Indices the array scores scores[3]

System.out.println("Enter 5 basketball scores:"); int[] scores = new int[5]; int scoreSum = 0; for (int i = 0; i < scores.length; i++) { scores[i] = keyboard.nextInt(); scoreSum += scores[i]; } double average = (double) scoreSum / scores.length; System.out.println("Average score: " + average); for (int i = 0; i < scores.length; i++) { if (scores[i] > average) System.out.println(scores[i] + ": above average"); else if (scores[i] < average) System.out.println(scores[i] + ": below average"); else System.out.println(scores[i] + ": equal to the average"); } 6

 Given an array of numbers, sort the numbers into ascending order  Input array:  Sorted array:

public static void printArray(int[] array) { for(int i = 0; i < array.length; i++) System.out.print(array[i] + " "); System.out.println( ); } 8

 Get in groups of 2-3 people  Write an algorithm to search for an item in an unsorted list  Start with find the index of 5 in the list ◦  Then turn that into a Java method with the following header: public int findIndex(int x, int[] array)  Finally, think about a more efficient way to do that with a sorted array – write pseudocode  Hand in with names on it 9

public int findIndex(int x, int[] array) { for(int i =0; i< array.length; i++) { if(array[i] == x) return i; } return -1; } 10

 Do we need to start at the beginning and look all the way through?  What if we start in the middle of the array? ◦ int midPoint = (array.length / 2);  Since the array is sorted: ◦ If array[midPoint] > x, we know x must be in first half of array ◦ Else if array[midPoint] > x, x must be in 2 nd half of array ◦ Else if array[midPoint] == x, return midPoint  This is called a binary search 11

public int binarySearch(int x, int[] array) { int min= 0; int max= array.length-1; while (min <= max) { int mid = (min+ max) / 2; if(array[mid] > x) max = mid-1; else if(array[mid] < x) min = mid + 1; else//must be equal return mid; } return -1; } 12

 Review of Arrays, Inheritance and Sorting/Search Algorithms 13