CS0007: Introduction to Computer Programming Array Algorithms.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

CS0007: Introduction to Computer Programming Arrays: Higher Dimensional Arrays.
CSE Lecture 3 – Algorithms I
Efficiency of Algorithms Csci 107 Lecture 6-7. Topics –Data cleanup algorithms Copy-over, shuffle-left, converging pointers –Efficiency of data cleanup.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CS4413 Divide-and-Conquer
HST 952 Computing for Biomedical Scientists Lecture 9.
Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort.
Efficiency of Algorithms
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Topic 9B – Array Sorting and Searching
Searching and Sorting Arrays
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
CS0007: Introduction to Computer Programming Introduction to Arrays.
CSE 1301 J Lecture 13 Sorting Richard Gesick. CSE 1301 J 2 of 30 Sorting an Array When an array's elements are in random order, our Sequential Search.
Chapter 8 ARRAYS Continued
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.
Chapter 16: Searching, Sorting, and the vector Type.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Chapter 19 Searching, Sorting and Big O
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
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.
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
CSC 211 Data Structures Lecture 13
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 2.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
Chapter 3 Searching and Selection Algorithms. 2 Chapter Outline Sequential search Binary search List element selection.
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.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Sorting Algorithms Written by J.J. Shepherd. Sorting Review For each one of these sorting problems we are assuming ascending order so smallest to largest.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
Chapter 16: Searching, Sorting, and the vector Type.
1 Algorithms Searching and Sorting Algorithm Efficiency.
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 9: Sorting and Searching Arrays
16 Searching and Sorting.
Searching and Sorting Algorithms
Searching Given a collection and an element (key) to find… Output
Siti Nurbaya Ismail Senior Lecturer
Chapter 8 Search and Sort
MSIS 655 Advanced Business Applications Programming
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
24 Searching and Sorting.
Divide and Conquer Algorithms Part I
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Presentation transcript:

CS0007: Introduction to Computer Programming Array Algorithms

Review Arrays are… complex variables that can hold multiple values of the same data type. The size declarator indicates… the number of elements the array can hold. Individual values inside of an array are called… elements. An element’s index (or subscript) is… a uniquely identifying number that is used to pinpoint its position in the array. The enhanced for loop is… a special loop that iterates through the elements in a collection (in this case array) and allows access to each element.

Comparing Arrays Sometimes you want to know if two arrays contain all of the same elements. What would be the output? int[] firstArray = {1, 2, 3, 4, 5}; int[] secondArray = {1, 2, 3, 4, 5}; if(firstArray == secondArray) System.out.print("The arrays are the same!"); else System.out.print("The arrays are NOT the same!"); Answer: “The arrays are NOT the same” Why? Because the == operator compares the references in the array variables and they point to two different objects How do we fix it? We iterate through each the arrays and compare them element by element Example: ArrayComparison.java

Sum, Average, and Minimum and Maximum Four common algorithms used with arrays: Sum of all the elements in the array (ArraySum.java) Average of all the elements in the array (ArrayAverage.java) Minimum element in the array (ArrayMin.java) Max element in the array (ArrayMax.java)

Partially Filled Arrays Sometimes you need to stores a series of items in an array, but you don’t know the number of items there are. Solutions? One solution is to create and array with the size being equal to the maximum number of possible items. Problem? oIf you do not fill the entire array, the array will only be partially filled. oIf you go to process the items in the array, you need to only process valid items. Solution? You must keep track of the number of items you have currently in the array, as well. Example: PartialArray.java

The Sequential Search Algorithm Arrays are known as a data structures. A Data Structure is a particular way of storing and organizing data in a computer so that it can be used efficiently. There are many, many data structures and many are provided by the Java API. The array is one of the more simple ones. Although arrays are very useful, they may not be the most efficient structure depending on the situation you want to use them. For instance, if you need to find an element with a specific value in an array, what can you do? Simplest answer: look at every element in the array starting from the begining and see if it is the one we are looking for. oThis is called the Sequential Search Algorithm (SequentialSearch.java).

The Sequential Search Algorithm If we are talking about speed in terms of number of comparisons, what is the worst case scenario for the Sequential Search Algorithm? The element we are looking for is the last element of the array. If there are n elements, that means we have to check n elements in the worst case. If there are n elements in the array what is the average case? n+1/2 comparisons. There are data structures that can theoretically perform better in the worst case There are search algorithms that can perform better in the average case We will revisit this in a bit.

The Selection Sort Algorithm Another thing we often want do to with data, besides search it, is to sort it. Names, grades, scores, etc. Probably the most simple sorting algorithm is the Selection Sort Algorithm. The Selection Sort Algorithm works as follows: 1. Search the array for the smallest element 2. Swap the first element with the smallest element 3. The first element is holding the correct value, so no longer consider it in the sort 4. Repeat previous steps for rest of array until the array is completely sorted. Example: SelectionSort.java There are many, many more sorting algorithms, most of which are more efficient than Selection Sort.

Binary Search Sequential Search does not scale well to arrays with a large number of elements. As stated before, there are many, many searching algorithms. One of which is the Binary Search Algorithm. The general idea of Binary Search assumes that the array is sorted in ascending order, and it uses this knowledge to incrementally eliminate half of the elements from the array. The algorithm is as follows: 1. Compare the target value to the element in the middle of the sorted array If they are the same, the search ends If the target value is larger than the element, then you know every element to the left of the middle is also not the element. oEliminate all values to the left of the middle and repeat step 1 with the elements left in the array. If the target value is smaller that the element, then you know every element to the right of the middle is also not the element. oEliminate all values to the right of the middle and repeat step 1 with the elements left in the array. If there is no more elements to eliminate in the array, the target value is not in the array.