Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tirgul no. 6 Topics covered : b Constructors – types of constructors b Two-dimensional and Multi-dimensional arrays in java. b Sorting an array of numbers.

Similar presentations


Presentation on theme: "Tirgul no. 6 Topics covered : b Constructors – types of constructors b Two-dimensional and Multi-dimensional arrays in java. b Sorting an array of numbers."— Presentation transcript:

1 Tirgul no. 6 Topics covered : b Constructors – types of constructors b Two-dimensional and Multi-dimensional arrays in java. b Sorting an array of numbers using the BubbleSort algorithm, pseudo-code and java implementation. b Complexity analysis (running time) of the BubbleSort algorithm.

2 Constructors revisited: b There are 3 basic types of constructors that can be defined for a class: 1)Default constructor – a constructor that receives no parameters. If no constructors are defined – the compiler automatically creates an empty default constructor. 2)Insertion constructor – any constructor that receives some parameters which are used to initialize the data members of the object created. 3)Copy constructor – a constructor which receives a ref to an object of the same class – and uses it to copy the ‘internal state’ (all data members) of that object to the newly created object. This constructor creates a new copy of the object it receives as a parameter.

3 Constructor example: public class NumberedRectangle { private int serialNumber; private Point p1; //lower left corner. private Point p2 // upper right corner. //default constructor public NumberedRectangle(){ serialNumber= 0; p1= new Point(0,0); p2= new Point(1,1); } //insertion constructor public NumberedRectangle(int serialNumber, Point p1, Point p2){ this.serialNumber= serialNumber; this.p1= new Point(p1); this.p2= new Point(p2); }

4 Defining constructors (cont.) //class NumberedRectangle continued. public NumberedRectangle(NumberedRectangle other){ this.serialNumber= other.serialNumber; this.p1= new Point(other.p1); this.p2= new Point(other.p2); } //other methods… } //end of class b b Note that since the ref passed to the copy constructor is a ref to an object of the same type which is created – the new object has direct access to the other objects’ data members!

5 Constructor overloading b In java a class can have more than one constructor defintion. This is called Constructor Overloading and is a special case of Method overloading (will be addressed in future lessons) b The compiler knows which constructor to call by looking at the parameters that are passed to the constructor, when it is called. Therefore – the constructors must differ in their parameter lists: they can have a different number of parameters, or different kinds of parameters.

6 Two-Dimensional Arrays: declaration and initialization b A two dimensional array – is actually an array of arrays. b Declaration: boolean[ ][ ] table; Table is a reference to a 2-dimensional array of booleans which is uninitialized. b Initialization: option I: table= new boolean[10][5]; table now refers to an array of 10 boolean arrays, each with 5 boolean values. This can be thought of as a table with 10 rows and 5 columns.

7 Two-Dimensional Arrays: declaration and initialization b Option II: table= new boolean[3][ ]; table[0] = new boolean[3]; table[1]= new boolean[10]; table[2]= new boolean[2]; b This way table now refers to an array of 3 boolean arrays, each with a different size. Note – each cell in the array must be initialized separately – if they are not all initialized using option I.

8 2-dim arrays with different lengths: b Example: int intArray2D[ ][ ] = new int[5][ ]; for(int i=0; i<intArray2D.length; i++) intArray2D[i] = new int[i+1]; b What we have now is: intArray2D[0] intArray2D[1] intArray2D[2] intArray2D[3] intArray2D[4]

9 Accessing a 2-dim array: b Accesing the arrays is done using indices: table[0][1]= true; Or: if( table[i][j] ) //some code

10 int[][] table = new table [10][10]; for (int i=0; i<10; i++) for (int j=0; j<10; j++) table[i][j] = i * j; System.out.println(table[4][3]); System.out.println(table[2][1]); Multiplication Table Example

11 Multidimensional Arrays b In java we can define arrays of any dimension. This can be thought of as an array of arrays of arrays… b Each array can have a different size which will be denoted by its length field. b Example: int[][][] counts= new int[2][2][ ]; counts[0][1]= new int[10]; counts[1][1]= new int[20]; … counts[0][0][0]= 100;

12 Multi Dimensional Arrays Two equivalent ways to create a 2x3 array of int: Separate creation and initialization: int intArray2D[][] = new int[2][3]; intArray2D[0][0]=1; intArray2D[0][1]=2; intArray2D[0][2]=3; intArray2D[1][0]=4; intArray2D[1][1]=5; intArray2D[1][2]=6; Declaration and initialization (size is implied by initialization): int intArray2D[][] = {{1,2,3}, {4,5,6}};

13 Arrays of objects Create a 2x2 array of strings, initially all entries are “null”: String stringArray2D[][] = new String[2][2]; Create a 2x2 array of strings which are initialized: String stringArray2D[][] = {{new String("a"),new String("aa")}, {new String("b"), new String("bb")}};

14 Permutation Matrices Definition: An NxN matrix is a permutation matrix if in each row and column of the matrix there is only one entry with the value ‘1’ and all other values are ‘0’. Examples: 1 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 1 0 0 0 0 1 0

15 Permutation Matrix Class public class PermMat { private int data[][]; public PermMat(int data[][]) { this.data = new int[data.length][data[0].length]; if(isPerm(data)) { for(int i=0; i<data.length; i++) System.arraycopy(data[i],0,this.data[i],0,data[i].length); } else setIdentity(); }

16 Permutation Matrix Class (cont.) private boolean isPerm(int data[][]) { int i,j,numOnes; //check the rows for(i=0; i<data.length; i++) { numOnes = 0; for(j=0; j<data[0].length; j++) { if(data[i][j] != 0) { if(((data[i][j] == 1) && (numOnes==1)) || (data[i][j] != 1)) return false; else numOnes++; } if(numOnes == 0) return false; } cont on next slide...

17 Permutation Matrix Class (cont.) //check the columns for(j=0; j<data[0].length; j++) { numOnes = 0; for(i=0; i<data.length; i++) { if(data[i][j] != 0) { if(((data[i][j] == 1) && (numOnes==1)) || (data[i][j] != 1)) return false; else numOnes++; } if(numOnes == 0) return false; } return true; }

18 Sorting an array of numbers- Buble Sort b Sorting is a basic task used in many different contexts. b There are many different algorithms to sort a list of numbers, (or any other objects which can be ordered). b We present a simple naïve sorting algorithm called BubbleSort. b Basic idea: each time “bubble” largest element left in the array to its place at the end of the array, until all values are positioned in their “correct” place.

19 BubbleSort – PseudoCode: Notice different notation: this is just intended to be readable for any programmer in ANY language! Notice different notation: this is just intended to be readable for any programmer in ANY language! //assume A is an array of numbers of size n. BubbleSort(A){ (1) for i=1 to n do (1.1) for j=1 to n-1 do (1.1) for j=1 to n-1 do (1.1.1) if (A[j] > A[j+1] ) then (1.1.1) if (A[j] > A[j+1] ) then (1.1.1.1) swap(A,j,j+1); (1.1.1.1) swap(A,j,j+1);} //swap two values in the array A in indices i, and j. swap(A,i,j){ (1) temp  A[i]; (2) A[i]  A[j]; (3) A[j]  temp; }

20 BubbleSort: java code: public void bubbleSort(int[] array){ for(int i=0; i<array.length; i++) for(int j=0; j<array.length-1; j++) for(int j=0; j<array.length-1; j++) if(array[j] > array[j+1]){ if(array[j] > array[j+1]){ int temp= array[j]; int temp= array[j]; array[j]= array[j+1]; array[j+1]= temp; }}

21 BubbleSort – complexity analysis b We want to measure how efficient is the algorithm. b You have seen in class that the standard way of doing this is by analyzing the number of operations that the algorithm performs for a given input of size n. b we measure the running time of the algorithms as a function of the input size n. b In our case – the input size is : the Array’s size! b BubbleSort running time: for an array of size n how many times will we evaluate the if statement (and decide if to swap or not to swap 2 values)?

22 BubbleSort – complexity analysis b Anlysis: outer loop: n times, each time runs inner loop n-1 iterations: total is: n(n-1) if evaluations. b We write: T(n)= n(n-1)= O(n 2 ) using the “Big O” notation shown in class. b Question: can we improve the BubbleSort algorithm shown here?

23 BubbleSort – improved version: b Answer: notice that after the first iteration of the outer loop the largest element in the array is “bubbled” to the last place in the array – and we don’t need to compare any of the other numbers to it again – it will never be moved from that location! public void bubbleSort(int[] array){ for(int i=0; i<array.length; i++) for(int j=0; j<array.length-(i+1); j++) for(int j=0; j<array.length-(i+1); j++) if(array[j] > array[j+1]){ if(array[j] > array[j+1]){ int temp= array[j]; int temp= array[j]; array[j]= array[j+1]; array[j+1]= temp; }}

24 Improved Version Complexity Analysis: b In the improved version for a given input array of size n we now have: T(n)= (n-1) + (n-2) + (n-3) + … + (n-(n-1))= n*(n-1)/2 b This is an improvement over n(n-1) ! BUT: T(n)= n*(n-1)/2 = O(n 2 ) So in terms of complexity the algorithm performs the same order of operations! BubbleSort – performs a constant number of operations – for all inputs: best case (array is ordered), average case and worst case. Questions: - what is the “worst” case? - can you think of an improvement for “best” and “worst” case?


Download ppt "Tirgul no. 6 Topics covered : b Constructors – types of constructors b Two-dimensional and Multi-dimensional arrays in java. b Sorting an array of numbers."

Similar presentations


Ads by Google