Basic Algorithms on Arrays. Learning Objectives Arrays are useful for storing data in a linear structure We learn how to process data stored in an array.

Slides:



Advertisements
Similar presentations
ArrayList Difference of Array and ArrayList [Sample code] TestArrayList.java.
Advertisements

UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.
Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
CS102 A Wide Array of Possibilities CS 102 Java’s Central Casting.
Sorting Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Lecture 05 - Arrays. Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values.
Binary Search Visualization i j.
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS1110: Computer Science I Chapter 5 Arrays. One-dimensional Arrays int [] data = new int [4]; data[0] = 1; data[1] = 3; data[2] = 5; data[3] = 7; Arrays.
Array Must declare a variable to reference the array double [] mylist; // cannot double list[20]; Or double mylist[]; The declaration doesn’t allocate.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Lecture No.01 Data Structures Dr. Sohail Aslam
Data Structures Using C++ 2E
A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab.
1 Chapter 4: Arrays and strings Taufik Djatna
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Searching CS 105 See Section 14.6 of Horstmann text.
Arrays.
CSC 205 Java Programming II Algorithm Efficiency.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Java Arrays [Lists] in 10 Minutes. Declaring an array (and its types) int[] myIntArray; double[] myDoubleArray; String[] myStrings; //What’s the pattern?
© 2007 Lawrenceville Press Slide 1 Chapter 10 Arrays  Can store many of the same kind of data together  Allows a collection of related values to be stored.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
Working with arrays (we will use an array of double as example)
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
1 5. Abstract Data Structures & Algorithms 5.2 Static Data Structures.
Enhanced For Loops (For Each Loops) Less Code and Less Overhead for Writing For Loops.
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.
Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be.
Pointers *, &, array similarities, functions, sizeof.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Array.  ARRAYS ALLOW US TO STORE ELEMENTS OF SINGLE DATA TYPE CONTAGUISLY IN MEMORY  EACH ELEMENT IS ASSOCIATED WITH AN INDEX OR LOCATION  WE CAN ACCESS.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Array Lists Array Lists Dale.
1 BUILDING JAVA PROGRAMS CHAPTER 7.2 ARRAY TRAVERSAL ALGORITHMS.
Simple algorithms on an array - compute sum and min.
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
 Array is a data structure were elements are stored in consecutive memory location.in the array once the memory is allocated.it cannot be extend any more.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
1 Data Structures and Algorithms Outline This topic will describe: –The concrete data structures that can be used to store information –The basic forms.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Searching CS 110: Data Structures and Algorithms First Semester,
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
3/21/2016IT 2751 Tow kinds of Lists Array What can be done? What can be easily done? student 1 student 2 student 3 student 4 Linked List student 2 student.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
UNIT-II Topics to be covered Singly linked list Circular linked list
Array in C# Array in C# RIHS Arshad Khan
Arrays (review) CSE 2011 Winter May 2018.
AKA the birth, life, and death of variables.
Arrays Arrays are data structures that allow us to store data of the same type in contiguous memory locations. That was a pretty dense statement!
Arrays An Array is an ordered collection of variables
Lecture 18 Arrays and Pointer Arithmetic
Can store many of the same kind of data together
CS2011 Introduction to Programming I Arrays (I)
Why did the programmer quit his job?
Suggested self-checks: Section 7.11 #1-11
Building Java Programs
Presentation transcript:

Basic Algorithms on Arrays

Learning Objectives Arrays are useful for storing data in a linear structure We learn how to process data stored in an array We learn how to Read a file and store a set of Strings in an array We learn how to find a specific String in an array We learn how to find ALL Strings that starts with a prefix String And more……

We recall an Array Characteristics - A contiguous block of memory spaces - Each one is referred to by an index - indices start from 0 and go to n-1 - Array is “homogeneous”, that is, it can only keep one type of data

Define and initialize an array int[ ] A = new int[10]; A[0] = 10; A[1] = 20; …..

Arrays are Static Once defined Size cannot be changed A.length = 20; /* illegal */ A Why? Because a Static Memory Block is already allocated and it cannot be changed.

So how do we change size? double the size int[ ] B = new int[2*A.length]; for (int i=0; i<A.length; i++){ B[i] = A[i]; } A = B; Define a new array B of double the size Iterate through the array A Copy old elements of A to B Discard old A and assign new B to A

Operations on Arrays

Iterate through all elements in the array for (int i =0; i < A.length; i = i + 1) { /* write your code here */ }

Iterate backwards for (int i = A.length-1; i >= 0; i = i - 1) { /* write your code here */ }

Reverse the Array Think of an algorithm to do this

Find the max/min in an Array for (int i = 0; i < A.length ; i = i + 1) { }

Array of Strings aaaade6asdbbtroriohelpmeyesno for (int i = 0; i < A.length ; i = i + 1) { } Print all Strings starting with prefix

Now to class work

Next Linear and Binary Search on Arrays