Merging two sorted arrays public static void Merge
Merging two sorted arrays public static void Merge (double array[], int start, int m, int n) { // start would be 0 in this case double temp[] = new double[n-start]; int index = 0, index 1, index 2, i; for (index 1=start, index 2=m; (index 1 < m) && (index 2 < n); ) { if (array[index 1] < array[index 2]) { temp[index] = array[index 1]; index++; index 1++; } else { temp[index] = array[index 2]; index++; index 2++; 1 }
Merging two sorted arrays } for(; index 1<m; index 1++, index++) { temp[index] = array[index 1]; } for(; index 2<n; index 2++, index++) { temp[index] = array[index 2]; } for (i=start; i<n; i++) { array[i] = temp[i-start]; } 2
Merge sort • Recursively sort half of the array separately and merge them class Merge. Sort { public static void main (String arg[]) { int n = 20; double array[] = new double[n]; Initialize (array, n); Sort (array, 0, n-1); } // continued in next slide // not shown 3
Merge sort public static void Sort (double array[], int start, int end) { if (start < end) { Sort (array, start+(end+1 start)/2 -1); Sort (array, start+(end+1 -start)/2, end); Merge (array, start+(end+1 start)/2, end+1); } } 4 } // end class
Merge sort • Running time? Let it be T(n) for an array of size n T(n) = 2 T(n/2) + cn for some constant c • Solution to this functional equation (or recurrence) is the running time of merge sort • T(n) = c’nlog 2(n) for some constant c’ and large n • Note that this is the worst case running time of merge sort – Much better than bubble sort and insertion sort which had worst case running time quadratic in n 5
Use a static variable class Static. Demo { int x; // a normal instance variable static int y; // a static variable } class SDemo { public static void main(String args[]) { Static. Demo ob 1 = new Static. Demo(); Static. Demo ob 2 = new Static. Demo(); /* Each object has its own copy of an instance variable. */ ob 1. x = 10; ob 2. x = 20; System. out. println("Of course, ob 1. x and ob 2. x " + "are independent. "); System. out. println("ob 1. x: " + ob 1. x + "nob 2. x: " + ob 2. x); System. out. println(); 6
Use a static variable /* Each object shares one copy of a static variable. */ System. out. println("The static variable y is shared. "); ob 1. y = 19; System. out. println("ob 1. y: " + ob 1. y + "nob 2. y: " + ob 2. y); System. out. println(); } } System. out. println("The static variable y can be" + " accessed through its class. "); Static. Demo. y = 11; // Can refer to y through class name System. out. println("Static. Demo. y: " + Static. Demo. y + "nob 1. y: " + ob 1. y + "nob 2. y: " + ob 2. y); 7
Use a static method class Static. Meth { static int val = 1024; // a static variable } // a static method static int val. Div 2() { return val/2; } class SDemo 2 { public static void main(String args[]) { System. out. println("val is " + Static. Meth. val); System. out. println("Static. Meth. val. Div 2(): " + Static. Meth. val. Div 2()); Static. Meth. val = 4; System. out. println("val is " + Static. Meth. val); System. out. println("Static. Meth. val. Div 2(): " + Static. Meth. val. Div 2()); } } 8
Static method • Can call only other static methods. • Must access only static data. • Do not have ‘this’ reference. 9
Use a static block class Static. Block { static double root. Of 2; static double root. Of 3; static { System. out. println("Inside static block. "); root. Of 2 = Math. sqrt(2. 0); root. Of 3 = Math. sqrt(3. 0); } } Static. Block(String msg) { System. out. println(msg); } class SDemo 3 { public static void main(String args[]) { //Static. Block ob = new Static. Block("Inside Constructor"); System. out. println("Square root of 2 is " + Static. Block. root. Of 2); System. out. println("Square root of 3 is " + Static. Block. root. Of 3); } } 10
- Slides: 10