0% found this document useful (0 votes)
6 views4 pages

Java Lab Manual

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

Java Lab Manual

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

WEEK:2

[Link] JAVA PROGRAM TO SEARCH FOR AN ELEMENT IN A GIVEN LIST OF ELEMENTS USING BINARY
SEARCH MECHANISM.

SOURCE CODE:

import [Link];

class Main

int binarySearch(int array[], int element, int low, int high)

while (low <= high)

int mid = low + (high - low) / 2;

if (array[mid] == element)

return mid;

if (element > array[mid])

low = mid + 1;

else

high = mid - 1;

return -1;

public static void main(String args[])

1
Main obj = new Main();

int[] array = { 3, 4, 5, 6, 7, 8, 9 };

int n = [Link];

Scanner input = new Scanner([Link]);

[Link]("Enter element to be searched:");

int element = [Link]();

[Link]();

int result = [Link](array, element, 0, n - 1);

if (result == -1)

[Link]("Not found");

else

[Link]("Element found at index " + result);

OUTPUT:

Enter element to be searched:


4
Element found at index 1

2
[Link] A JAVA PROGRAM TO SORT FOR AN ELEMENTS USING BUBBLE SORT

SOURCE CODE:

class BubbleSort

void bubbleSort(int arr[])

int n = [Link];

for (int i = 0; i < n - 1; i++)

for (int j = 0; j < n - i - 1; j++)

if (arr[j] > arr[j + 1]) {

// swap temp and arr[i]

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

public static void main(String args[])

BubbleSort ob = new BubbleSort();

int a[] = { 64, 34, 25, 12 };

[Link](a);

int n = [Link];

3
for (int i = 0; i < n; ++i)

[Link](a[i] + " ");

[Link]();

OUTPUT:

12 25 34 64

You might also like