Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Basic Algorithms on Arrays

2 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……

3 We recall an Array 10206030679076658707 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

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

5 Arrays are Static 10206030679076658707 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.

6 So how do we change size? 10206030679076658707 double the size int[ ] B = new int[2*A.length]; for (int i=0; i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4035753/slides/slide_6.jpg", "name": "So how do we change size.", "description": "10206030679076658707 double the size int[ ] B = new int[2*A.length]; for (int i=0; i

7 Operations on Arrays

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

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

10 Reverse the Array 10206030679076658707 Think of an algorithm to do this 07876576906730602010

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

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

13 Now to class work

14 Next Linear and Binary Search on Arrays


Download ppt "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."

Similar presentations


Ads by Google