You are on page 1of 4

DSA Practical, Dept of SWE, Mehran UET

Student Name Muhammad Saad


Roll Number 21SW112
Section # II
Lab # 3

Task#01 & 02 & 03

Task#01

Implement linear search on 1D and 2D array.

Note: create methods for both 1D and 2D linear search

//LinearSearch1D(int[] A, int key)


//LinearSearch2D(int[][] A, int key)
//call methods and show the search result
Task#02

Implement binary search on 1D and 2D array.

Note: create methods for both 1D and 2D binary search

//BinarySearch1D(int[] A, int key, int first, int end)


//BinarySearch2D(int[][] A, int key, int first, int end)
//call methods and show the search result
Task#03

Display the execution time of searching algo (linear and binary both) and examine which one is the

faster and explain why?

long before1=System.currentTimeMillis();

//call method here

long after1=System.currentTimeMillis();

//than fine difference between after and before variable – it will give you the

execution time of method

Sample Output:
DSA Practical, Dept of SWE, Mehran UET

Time took by Linear Search : ____ (time in milliseconds or nanoseconds)

Time took by Binary Search : ____ (time in milliseconds or nanoseconds)

Code:
import java.util.*;

public class Main {


public static void main(String[] args) {
LAB3DSA ls = new LAB3DSA();
int[] a1 = {10, 20, 30, 50, 70, 90};
int index = ls.linearSearch1D(a1, 50);
if (index == -1)
System.out.println("key not found");
else {
System.out.println("key found " + index);
}
long before1 = System.nanoTime();
System.out.println(Arrays.toString(LAB3DSA.search(new int[][]{a1},
25)));
long after1 = System.nanoTime();
System.out.println("time taken binary 2D " + (after1 - before1));

System.out.println("linear 2D");
int arr[][] = {{3, 6, 9},

{5, 2, 7},

{90, 45, 82}};


int target = 45;
int ans[] = LAB3DSA.linearSearch2D(arr, target);
System.out.println("Element found at index: "
+ Arrays.toString(ans));
long before2 = System.nanoTime();
System.out.println(Arrays.toString(LAB3DSA.search(arr,25)));
long after2 = System.nanoTime();
System.out.println("time taken binary 2D "+ (after2-before2) );
System.out.println("binary search 1D");
int[][] my_array = {{1, 6, 7},
{8, 11, 13},
{18, 25, 31}};
int answer = 11;

if (LAB3DSA.binarySearch1D(my_array, answer) > 0)


System.out.println("Found");

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

{33, 34, 38, 50}


};
long before4 = System.nanoTime();
System.out.println(Arrays.toString(LAB3DSA.search(arr2, 25)));
long after4 = System.nanoTime();
System.out.println("time taken binary 2D " + (after4 - before4));
}

}
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;
}

static int[] linearSearch2D(int[][] arr, int target)

for (int i = 0; i < arr.length; i++) {

for (int j = 0; j < arr[i].length; j++) {

if (arr[i][j] == target) {

return new int[] { i, j };


}
}
}
return new int[] { -1, -1 };
}
public static int binarySearch1D(int a[][], int searchValue){
int rows = a.length;
int cols = a[0].length;
int start = 0;
int end = rows * cols - 1;
int mid, midRow, midCol, midVal;

while (start <= end) {


mid = (start + end) / 2;
midRow = mid / cols;
midCol = mid % cols;
midVal = a[midRow][midCol];

if (midVal == searchValue){
// Return 1 if target found
return 1;

} else if (midVal > searchValue) {


end = mid - 1;

} else if (midVal < searchValue){


start = mid + 1;

}
}
// Return 0 if not found
DSA Practical, Dept of SWE, Mehran UET

return 0;
}

static int[] search(int[][] a, int target) {


int r = 0;
int c = a.length - 1;
while (r < a.length && c >= 0) {
if (a[r][c] == target) {
return new int[]{r, c};
}
if (a[r][c] < target) {
r++;
} else
c--;
}
return new int[]{-1, -1};
}

Output:

TIME TAKEN IN BINARY SEARCH IS LESS THAN BINARY LINEAR.

You might also like