Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2 Array Data Structure Winter 2004. Array The Array is the most commonly used Data Storage Structure. It’s built into most Programming languages.

Similar presentations


Presentation on theme: "Chapter 2 Array Data Structure Winter 2004. Array The Array is the most commonly used Data Storage Structure. It’s built into most Programming languages."— Presentation transcript:

1 Chapter 2 Array Data Structure Winter 2004

2 Array The Array is the most commonly used Data Storage Structure. It’s built into most Programming languages.

3 Creating an Array An array is a sequential data abstrction, its name is a reference to an array. int[ ] intArray; //defines a reference to an array intArray = new int[100];//creates the array

4 Show sample program array.java lowArray.java highArray.java

5 INSERTION Example of insert operation and behavior –New Item always inserted in the first vacant cell in the array. Observations: –Most algorithm knows how many items are already in the array. –Searching and deleting are however not fast.

6 DELETING Example of delete operation and behavior –To delete an element first, you must find it. –How long does it take for the worst case? –A deletion requires searching through an average of N/2 elements and moving the rest of the elements.

7 INITIALIZATION In Java, an array of integers is automatically initialized to 0. Unless you specify otherwise, You can initialize an array to something beside 0 using this syntax: int[] intArray ={0,1,2,3,4,5,6,7,8,9};

8 Accessing Array Elements Array elements are accessed using an index number. temp = intArray[3]; //get 4th element content intArray[7] = 66; //insert 66 in eighth cell

9 Linear Search What is Linear search? Simple-minded way= a sequential search N elements array –What is the worst case? –Run time is O (N)

10 Binary Search What is Binary search? –A sorted array is required New insert() Searching a sorted array by repeatedly dividing the search interval in half. Run time is O (log N)

11 Sample code for ordered array..\ReaderPrograms\ReaderFiles\Chap02\O rderedArray\orderedArray.java..\ReaderPrograms\ReaderFiles\Chap02\O rderedArray\orderedArray.java

12 logarithms RangeComparisons in B-search 104 (2 3 = 8) 100010 10,00014 : : 1,000,000,00030 Power of Two: 2 s -> s + 1 steps

13 Complexity of algorithm Time complexity Space complexity Time complexity: in big O notation. How much time it takes to process N data elements? Ignore constant details

14 Complexity of algorithm for i = 1 to n If a[i] == x then return a[i] Else i++; endfor What is the complexity? for i = 1 to n for j = 1 to m If a[i, j] == x then return a[i] else i++; endfor

15 Complexity of algorithm Don’t need constant since N can be very large –O(N) - worst case for a linear search in an array –O(N 2 ) –O(log N)

16 Complexity of algorithm Example of algorithmBig O Linear search Binary Search Insertion for unordered array Insertion for ordered array Deletion for unordered array Deletion for ordered array

17 BIG O : O( )

18 More complex object Array What if a complex data structure instead of int as an array element (e.g. person database)..\ReaderPrograms\ReaderFiles\Chap02\ClassD ata\classDataArray.java..\ReaderPrograms\ReaderFiles\Chap02\ClassD ata\classDataArray.java Person() getLastname() displayPerson() person Firstname Lastname age


Download ppt "Chapter 2 Array Data Structure Winter 2004. Array The Array is the most commonly used Data Storage Structure. It’s built into most Programming languages."

Similar presentations


Ads by Google