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.

Slides:



Advertisements
Similar presentations
1. List Static List: no adding or deleting Dynamic List: can add or delete items from the list Both static and dynamic lists: linear search, update item.
Advertisements

Garfield AP Computer Science
CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014.
Searching and Sorting Linear Search Binary Search Selection Sort
CSE 373: Data Structures and Algorithms
Selection Sorting Lecture 21. Selection Sort Given an array of length n, –In first iteration: Search elements 0 through n-1 and select the smallest Swap.
Binary Search Visualization i j.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Selection Sort -Reading p Reading p
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
Problem Solving #6: Search & Sort ICS Outline Review of Key Topics Review of Key Topics Problem 1: Recursive Binary Search … Problem 1: Recursive.
Array Must declare a variable to reference the array double [] mylist; // cannot double list[20]; Or double mylist[]; The declaration doesn’t allocate.
Unit 271 Searching and Sorting Linear Search Binary Search Selection Sort Insertion Sort Bubble (or Exchange) Sort Exercises.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p
Sorting Algorithms Insertion and Radix Sort. Insertion Sort One by one, each as yet unsorted array element is inserted into its proper place with respect.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Searching Arrays Linear search Binary search small arrays
1 Two-Dimensional Arrays. 2 Can be visualized as consisting m rows, each of n columns Syntax: datatype arrayname [row] [ column] ; Example: int val[3]
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Chapter 8 ARRAYS Continued
Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS
Building Java Programs Chapter 13 Searching reading: 13.3.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Computer Science Searching & Sorting.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Chapter Searching and Sorting Arrays 8. Introduction to Search Algorithms 8.1.
Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
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.
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
CSE 373 Data Structures and Algorithms
1 Traversal algorithms. 2 Array traversal traversal: An examination of each element of an array. Traversal algorithms often takes the following form:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Searching and Sorting.
STARTING OUT WITH STARTING OUT WITH Class 3 Honors.
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.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
3 – SIMPLE SORTING 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.
Catie Welsh April 20,  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Recursion Method calls itself iteratively until a base case is met and usually containing the following: if-else for base case with return value increment/decrement.
Sorting and Searching. Searching  Problem definition: Given a value X, return the index of X in the array if such X exist. Otherwise, return NOT_FOUND(-1).
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
1 BUILDING JAVA PROGRAMS CHAPTER 7.2 ARRAY TRAVERSAL ALGORITHMS.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
Arrays and Sorting. Process Open a file that contains integers, one per line. Read each line, convert to short and store each into an array Sort the array.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
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.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Data Structures I (CPCS-204)
Recitation 13 Searching and Sorting.
Chapter 7 Single-Dimensional Arrays
RECITATION 1 ANALYSIS OF ALGORITHMS
Sorting Data are arranged according to their values.
Searching and Sorting Linear Search Binary Search ; Reading p
كلية المجتمع الخرج البرمجة - المستوى الثاني
Sorting Data are arranged according to their values.
Search,Sort,Recursion.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Linear Search (Area Code Example)
Module 8 – Searching & Sorting Algorithms
Exercise 5 1. We learned bubble sort during class. This problem requires you to modify the code for bubble sorting method to implement the selection sorting.
Module 8 – Searching & Sorting Algorithms
Sorting Algorithms.
Presentation transcript:

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

Linear Search Does someone in the class have a score of 85? How about 90? scores If found, return its index. If not found, return …

Binary Search The scores are in ascending order. Does someone in the class have a score of 91? scores If found, return its index. If not found, return …

Sorting Arrays Selection sort Insertion sort Bubble sort Quick sort …

Selection sort array (before the sorting): array (after the sorting): Selection sort: keeps finding the smallest number and places it first

public static void selectionSort(int[] array){ for (int i = 0; i < array.length - 1; i++){ int min = array[i]; int indexOfMin = i; for (int j = i + 1; j < array.length; j++){ if (array[j] < min){ min = array[j]; indexOfMin = j; } array[indexOfMin] = array[i]; array[i] = min; }

The Arrays Class The java.util.Arrays class contains various methods for sorting and searching arrays. int[] list = {9,3,7,5,8,2,6,1,10,4}; java.util.Arrays.sort(list);

Exercise 1: Strictly Identical Arrays Two arrays list1 and list2 are strictly identical if they have the same length and list1[i] is equal to list2[i] for each i. Write a method that returns true if list1 and list2 are strictly identical: public static boolean equal(int[] list1, int[] list2) Write a test program that prompts the user to enter two lists of integers. Note that the first number in the input is the number of the elements in the list.

Exercise 2: Identical Arrays Two arrays list1 and list2 are identical if they have the same contents. public static boolean equal(int[] list1, int[] list2)