Lecture 5 of Computer Science II

Slides:



Advertisements
Similar presentations
Arrays part 2 Applications & such. Returning an array from a method A method can return an array, just like it can return any other kind of variable;
Advertisements

Chapter 6: Arrays Java Software Solutions for AP* Computer Science
HST 952 Computing for Biomedical Scientists Lecture 9.
Arrays, part 2. Array applications Arrays are useful whenever a relatively large amount of data must be kept available in memory for processing We will.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
Sorting1 Sorting Order in the court!. sorting2 Importance of sorting Sorting a list of values is a fundamental task of computers - this task is one of.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal.
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.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
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.
Lists in Python.
AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C arrays ❏ To be able to pass arrays and array elements to functions.
CSCI 51 Introduction to Programming March 12, 2009.
Arrays1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Data Structures Arrays and Lists Part 2 More List Operations.
CSCI 51 Introduction to Programming March 10, 2009.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
CS212: Data Structures and Algorithms
Implementing Singly Linked
Lecture 6 of Computer Science II
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Data Structures I (CPCS-204)
Two-Dimensional Arrays
EGR 115 Introduction to Computing for Engineers
Chapter 8 Arrays Objectives
Design and Analysis of Algorithms
Top Ten Words that Almost Rhyme with “Peas”
Siti Nurbaya Ismail Senior Lecturer
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Analysis of Algorithms CS 477/677
Arrays An Array is an ordered collection of variables
Sorts.
Bubble, Selection & Insertion sort
Data Structures and Algorithms
Chapter 8 Arrays Objectives
Array-Based Sequences
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Quadratic Sorting Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and.
Sorting … and Insertion Sort.
Announcements & review
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.
Chapter 8 Arrays Objectives
Simple Sorting Algorithms
Chapter 9: More About Data, Arrays, and Files
Data Structures & Algorithms
slides created by Marty Stepp
Analysis of Algorithms
Simple Sorting Algorithms
Sorting.
Arrays.
Presentation transcript:

Lecture 5 of Computer Science II Arrays

Storing Game Entries in an Array Using Arrays Storing Game Entries in an Array The first application we study is for storing entries in an array—in particular, high score entries for a video game. we have decided to store high score entries, which is a simple application that presents some important data structuring concepts that we will use for other implementations in this book. what we want to include in a high score entry? an integer representing the score itself, which we will call score. name of the person earning this score, which we will simply call name. Java code for a simple GameEntry class. Note that we include methods for returning the name and score for a game entry object, as well as a method for return a string representation of this entry.  Page 2

Storing Game Entries in an Array Using Arrays Storing Game Entries in an Array An illustration of an array of length ten storing references to six GameEntry objects in the cells from index 0 to 5, with the rest being null references.  Page 3

Insertion Using Arrays One of the most common updates we might want to make to the entries array of high scores is to add a new game entry. add(e): Insert game entry e into the collection of high scores. If the collection is full, then e is added only if its score is higher than the lowest score in the set, and in this case, e replaces the entry with the lowest score. Preparing to add a new GameEntry object to the entries array. In order to make room for the new reference, we have to shift the references to game entries with smaller scores than the new one to the right by one cell.  Page 4

Java code for inserting a GameEntry object. Using Arrays Insertion Once we have identified the place in the entries array where the new game entry, e, belongs, we add a reference to e at this position. Java code for inserting a GameEntry object. Adding a reference to a new GameEntry object to the entries array. The reference can now be inserted at index 2, since we have shifted all references to GameEntry objects with scores less than the new one to the right.  Page 5

Java code for performing the remove operation. Using Arrays Object Removal let us consider how we might remove a reference to a GameEntry object from the entries array. remove(i): Remove and return the game entry e at index i in the entries array. all objects previously stored at indices higher than i are "moved over" to fill in for the removed object Java code for performing the remove operation.  Page 6

Java code for performing the remove operation. Using Arrays Sorting an Array In this section, we study a way of starting with an array with objects that are out of order and putting them in order. A Simple Insertion-Sort Algorithm Insertion sort is the algorithm many people use when sorting a hand of cards. Java code for performing the remove operation.  Page 7

High-level description of the insertion-sort algorithm. Using Arrays A Simple Insertion-Sort Algorithm High-level description of the insertion-sort algorithm. This simple insertion–sort algorithm goes as follows. We start with the first character in the array. One character by itself is already sorted. Then we consider the next character in the array. If it is smaller than the first, we swap them. Next we consider the third character in the array. We swap it leftward until it is in its proper order with the first two characters. We continue in this manner with the fourth, fifth integer, the sixth, and so on, until the whole array is sorted.  Page 8

A Simple Insertion-Sort Algorithm Using Arrays A Simple Insertion-Sort Algorithm Intermediate-level description of the insertion-sort algorithm. Java code for performing insertion-sort on an array of characters.  Page 9

A Simple Insertion-Sort Algorithm Using Arrays A Simple Insertion-Sort Algorithm Execution of the insertion-sort algorithm on an array of eight characters. An interesting thing happens in the insertion-sort algorithm if the array is already sorted. In this case, the inner loop does only one comparison, determines that there is no swap needed, and returns back to the outer loop. That is, we perform only one iteration of the inner loop for each iteration of the outer loop. Thus, in this case, we perform a minimum number of comparisons. Of course, we might have to do a lot more work than this if the input array is extremely out of order. In fact, we will have to do the most work if the input array is in decreasing order.  Page 10

java.util Methods for Arrays and Random Numbers Using Arrays java.util Methods for Arrays and Random Numbers Java provides a number of built-in methods for performing common tasks on arrays, they are associated with the class, java.util.Arrays : equals(A, B): Returns true if and only if the array A and the array B are equal. fill(A,x): Stores element x into every cell of array A. sort(A): Sorts the array A using the natural ordering of its elements. toString(A): Returns a String representation of the array A. For example, the following string would be returned by the method toString called on an array of integers A = [4,5,2,3,5,7,10]: [4, 5, 2, 3, 5, 7, 10]  Page 11

Two-Dimensional Arrays and Positional Games Using Arrays Two-Dimensional Arrays and Positional Games Arrays in Java are one-dimensional. there is a way we can define two-dimensional arrays in Java—we can create a two-dimensional array as an array of arrays. Such a two—dimensional array is sometimes also called a matrix. In Java, we declare a two—dimensional array as follows: int[ ][ ] Y = new int[8][10]; Two-dimensional arrays have many applications to numerical analysis. we explore an application of two- dimensional arrays for implementing a simple positional game. Illustration of a two-dimensional integer array, Y, which has 8 rows and 10 columns. The value of Y[3][5] is 100 and the value of Y[6][2] is 632.  Page 12

Tic-Tac-Toe Game Using Arrays The basic idea is to use a two-dimensional array, board, to maintain the game board. Cells in this array store values that indicate if that cell is empty or stores an X or O. A simple, complete Java class for playing Tic-Tac-Toe between two players is provides at page No. 134 ( The text Book)  Page 13