Professional Documents
Culture Documents
Task#01
Display the execution time of searching algo (linear and binary both) and examine which one is the
long before1=System.currentTimeMillis();
long after1=System.currentTimeMillis();
//than fine difference between after and before variable – it will give you the
Sample Output:
DSA Practical, Dept of SWE, Mehran UET
Code:
import java.util.*;
System.out.println("linear 2D");
int arr[][] = {{3, 6, 9},
{5, 2, 7},
else
System.out.println("Not found");
long before3 = System.nanoTime();
System.out.println(Arrays.toString(LAB3DSA.search(my_array, 11)));
long after3 = System.nanoTime();
System.out.println("time taken binary 1D " + (after3 - before3));
System.out.println("BINARY 2D");
int[][] arr2 = {
{10, 20, 30, 40},
{15, 25, 35, 45},
{28, 29, 37, 49},
DSA Practical, Dept of SWE, Mehran UET
}
public class LAB3DSA {
public static int linearSearch1D(int[] arr, int key){
for(int i=0;i<arr.length;i++){
if(arr[i] == key){
return i;
}
}
return -1;
}
if (arr[i][j] == target) {
if (midVal == searchValue){
// Return 1 if target found
return 1;
}
}
// Return 0 if not found
DSA Practical, Dept of SWE, Mehran UET
return 0;
}
Output: