0% found this document useful (0 votes)
2 views

ada labsheet3 (1)

The document presents a lab sheet for ENCS 256, focusing on the implementation and analysis of a recursive binary search algorithm. It includes code for the binary search function, test cases with expected outputs, and a table displaying the performance of the search across different input sizes. The results highlight the efficiency of the algorithm in finding elements in sorted arrays.

Uploaded by

abhiclips441
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

ada labsheet3 (1)

The document presents a lab sheet for ENCS 256, focusing on the implementation and analysis of a recursive binary search algorithm. It includes code for the binary search function, test cases with expected outputs, and a table displaying the performance of the search across different input sizes. The results highlight the efficiency of the algorithm in finding elements in sorted arrays.

Uploaded by

abhiclips441
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

LAB SHEET 3

Analysis and Design of Algorithms


(ENCS 256)

SUBMIITED BY:
ABHISHEK KUMAR

SUBMIITED TO:
DR. VANDNA BATRA

ROLL NUMBER: 2301010322


COURSE: BTECH CSE CORE
SECTION: E

School of Engineering & Technology


K. R. MANGALAM UNIVERSITY
Sohna, Haryana 122103, India
Recursive Binary Search:
Code:
public class BinarySearchTimeMeasurement {

// Recursive Binary Search Function


public static int binarySearch(int[] arr, int left, int right, int key) {
if (left <= right) {
int mid = left + (right - left) / 2; // Calculate middle index

// If element is found at mid, return index


if (arr[mid] == key)
return mid;

// If key is smaller than mid, search in left half


else if (arr[mid] > key)
return binarySearch(arr, left, mid - 1, key);

// If key is greater than mid, search in right half


else
return binarySearch(arr, mid + 1, right, key);
}
return -1; // If element not found, return -1
}

public static void main(String[] args) {


// Manually defined sorted lists for different input sizes
int[][] inputLists = {
{5, 10, 15, 20, 25}, // Size 5
{5, 10, 15, 20, 25, 30, 35, 40, 45, 50}, // Size 10
{5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80,
85, 90, 95, 100}, // Size 20
{5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80,
85, 90, 95, 100,
105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165,
170, 175, 180, 185, 190, 195, 200} // Size 40
};

// Corresponding search keys for each input size


int[] searchKeys = {15, 30, 55, 125}; // Manually defined keys

// Print table header


System.out.println("Input Size | Search Key | Index Found | Time Taken
(µs)");
System.out.println("--------------------------------------------------
-------");

// Loop through each manually defined input list


for (int i = 0; i < inputLists.length; i++) {
int[] sortedArray = inputLists[i]; // Take the corresponding
sorted list
int key = searchKeys[i]; // Take the corresponding search key

// Measure the start time before performing search


long startTime = System.nanoTime();

// Perform Binary Search


int index = binarySearch(sortedArray, 0, sortedArray.length - 1,
key);

// Measure the end time after search


long endTime = System.nanoTime();

// Calculate the time taken in microseconds


long timeTaken = (endTime - startTime) / 1000;

// Print the result in table format


System.out.printf("%10d | %10d | %11d | %10d µs%n",
sortedArray.length, key, index, timeTaken);
}
}
}

Test Case Results:

Test Case 1: Searching for an Element Present in the Middle

Input:
Sorted List: [5, 10, 15, 20, 25]
Search Element: 15

Expected Output:
Index: 2
Time Taken: Minimal
Test Case 2: Searching for an Element Not Present

Input:
Sorted List: [5, 10, 15, 20, 25]
Search Element: 30

Expected Output:
Index: -1
Time Taken: Higher due to full search

Test Case 3: Searching for the First Element

Input:
Sorted List: [2, 4, 6, 8, 10]
Search Element: 2

Expected Output:
Index: 0
Time Taken: Minimal

Graph: Input Size vs. Time Taken

You might also like