Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


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

1 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 4 student 1 student 3

2 3/21/2016IT 2752 Array What can be done? What can be easily done? student 1 student 2 student 3 student 4 Linked List student 2 student 4 student 1 student 3 Some typical operations on Lists 1.Random access 2.Add/remove from the head 3.Add/remove from the tail 4.Add/remove from the middle 5.Resize

3 3/21/2016IT 2753 Two types of arrays Static array –size determined at compile time –can’t change afterwards Dynamic array (JAVA’s type, objects) –Size determine at run time –Can be resized (larger or smaller)

4 3/21/2016IT 2754 Array Characteristics Array student 1 student 2 student 3 student 4 A A[0] A[1] A[2] A[3] Linked List student 2 student 4 student 1 student 3 The elements of an array are in adjacent memory locations An array is a random (direct) access data structure.

5 3/21/2016IT 2755 Example of Array Traversal: Linear Search – O(n) StatementsCost 1int linearSearch( int [] a, int target ) { 2 int n = a.length; 1 3 int i = 0; 1 4 while ( i < n ) { n + 1 5 if ( target == a[i] ) n 6 return i; 1 7 i++; n 8 } 9 return –1; 1 } TOTAL 3n + 4

6 3/21/2016 IT 275 6 Creating Arrays public static void main(String[] args) { int size=4; int[] A=null; A = new int[size]; Double[] B = new Double[100]; String[] S = new String[size/2];..... } size A B S 4 Created during the run time Created during the compile time

7 3/21/2016 IT 275 7 Creating Arrays public static void main(String[] args) { int size=4; int[] A=null; A = new int[size]; Double[] B = new Double[100]; String[] S = new String[size/2];..... } size A B S 4 Created during the run time Created during the compile time

8 3/21/2016IT 2758 Using Arrays public static void main(String[] args) { int size=4; int[] A=null; A = new int[size]; Double[] B = new Double[100]; String[] S = new String[size/2];..... A[2]=2; B[0]= new Double(3.14); S[1] = new String(“test”); // or S[1] = “test”;..... } size A B S 4 Double: 3.14 String: “test” 2

9 3/21/2016IT 2759 Arrays passed as References void public static void fun(int a[]) { int i; I=2; a[i] += 5; } aiai public static void main(String[] args) { int a[4]={10,200,300,400}; for (int i=0; i < a.length; i++) System.out.print(" "+a[i]); fun(a); for (int i=0; i < a.length; i++) System.out.print(" "+a[i]); } 10 200 300 400 a a[2] 10 200 300 400 10 200 305 400 305 Still, we use the call-by- value mechanism!! 2

10 3/21/2016IT 27510 Passing Arrays (an object) public static void main(String[] args) {..... int[] A=null; A = new int[30]; double[] B = new double[100]; String[] S = new String[2];..... } public static void getArray(int[] a) { for (int i=0; i<a.length; i++) a[i] = (int)(Math.random()*1000); }

11 3/21/2016IT 27511 Passing Arrays (exercise) // select the minimum between a[s] to the end // and return the index of the minimum // private int min(int a[], int s){ int m = s; for (int i=s; i < a.length;i++) { if (a[i] < a[m]) m=i; } return m; } // exchange the contents of a[i] and a[j] // private void exchange(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } O(n-s) where n is the size of the array O(3) = O(1)

12 3/21/2016IT 27512 Selection Sort (a quadratic sort) public void sort(int a[]) { // select right element for a[i] for (int i = 0; i<a.length-1; i++) { int j = min(a,i); exchange(a,i,j); } 9 6 3 4 2 8 5 i j 2 6 3 4 9 8 5 i j 2 3 6 4 9 8 5 i j 2 3 4 6 9 8 5 i j 2 3 4 5 9 8 6 i j 2 3 4 5 6 8 9 i j

13 3/21/2016IT 27513 Why sorting? For searching! Sequential search  Binary search O(n)  O (log n) 1,000,000  20 More sorting algorithms will be seen in this class?sorting algorithms Try here

14 3/21/2016 IT 275 14 Two dimensional Arrays public static void main(String[] args) { int size=3; int[][] A = new int[size][4]; A[2][3] = 7; A[0][2] = 9; String[][] B;..... } 7 size A B 4 9 A[0] A[1] A[2] A[0][0] A[0][1] A[0][2] A[0][3] A[2][0] A[2][1] A[2][2] A[2][3]

15 3/21/2016 IT 275 15 Two dimensional Arrays public static void main(String[] args) { String[][] B; B = new String[3][]; B[1] = new String[4]; B[1][2] = “Robinson”; B[0] = new String[5]; ……… } B B[0] B[1] B[2]B[1][0] B[1][1] B[1][2] B[1][3] Robinson

16 3/21/2016 IT 275 16 Handling two dimensional arrays public static void main(String[] args) { String[][] B; B = new String[3][]; B[0] = new String[4]; B[1] = new String[5]; B[2] = new String[7]; ……… clear(B); ……… } public static void clear(String[][] X) { for (int i=0; i< X.length; i++) for (int j=o; j<X[i].length; j++) X[i][j] = null; }

17 3/21/2016IT 27517 Operations on Array Traversing – can be bidirectional O(n) Resizing – θ(size of new array) operation Replacing an element – an O(1) operation Inserting an element – an O(n) operation due to data movement Deleting an element – O(1) or O(n) depending on implementation

18 3/21/2016IT 27518 Some other operations on arrays are expensive Resizing – θ(size of new array) operation Inserting an element – an O(n) operation due to data movement Deleting an element – O(1) or O(n) depending on implementation (way more than you thought)


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

Similar presentations


Ads by Google