Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal.

Slides:



Advertisements
Similar presentations
Hash Tables and Sets Lecture 3. Sets A set is simply a collection of elements Unlike lists, elements are not ordered Very abstract, general concept with.
Advertisements

1 Various Methods of Populating Arrays Randomly generated integers.
Appendix I Hashing. Chapter Scope Hashing, conceptually Using hashes to solve problems Hash implementations Java Foundations, 3rd Edition, Lewis/DePasquale/Chase21.
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
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;
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.
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.
CS 206 Introduction to Computer Science II 10 / 31 / 2008 Happy Halloween!!! Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
AP Computer Science.  Not necessary but good programming practice in Java  When you override a super class method notation.
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.
Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1.
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Text Book: M. T. Goodrich and R. Tamassia, "Data Structures and Algorithms in Java," 4th edition, 2006, Wiley & Sons, Inc., ISBN Text Book:
Dr. Engr. Sami ur Rahman Assistant Professor Department of Computer Science University of Malakand Data Structure: List.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Lists Based on content from: Java Foundations, 3rd Edition.
Lists in Python.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
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.
Chapter 6: Arrays: Lists and Tables
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
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.
Chapter 2 Collections. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Define the concept and terminology related.
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.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Array Lists Array Lists Dale.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
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.
Chapter 9: Data types and data structures OCR Computing for A Level © Hodder Education 2009.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 23 Sorting.
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
Lecture 6 of Computer Science II
Lecture 5 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)
Siti Nurbaya Ismail Senior Lecturer
Quadratic Sorting Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and.
Data Structures & Algorithms
slides created by Marty Stepp
Sorting.
Arrays.
Presentation transcript:

Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal

 Page 2 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? 1.an integer representing the score itself, which we will call score. 2.name of the person earning this score, which we will simply call name.

 Page 3 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 4 Using Arrays Insertion 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 5 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. 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 6 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

 Page 7 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.

 Page 8 Using Arrays A Simple Insertion-Sort Algorithm High-level description of the insertion-sort algorithm.

 This simple insertion–sort algorithm goes as follows. 1.We start with the first character in the array. 2.One character by itself is already sorted. 3.Then we consider the next character in the array. If it is smaller than the first, we swap them. 4.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. 5.We continue in this manner with the fourth, fifth integer, the sixth, and so on, until the whole array is sorted.

 Page 10 Using Arrays A Simple Insertion-Sort Algorithm Intermediate-level description of the insertion-sort algorithm.

 Page 11 Using Arrays A Simple Insertion-Sort Algorithm Execution of the insertion- sort algorithm on an array of eight characters.

 Page 12 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 13 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.

 Page 14 Using Arrays Tic-Tac-Toe Game 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)