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

JAVAARRAY

The document contains Java code examples for linear search, binary search, array sorting, and finding the largest element in an array. It demonstrates the implementation of each algorithm with sample arrays and outputs the results. Each section includes a main method to test the respective functionality.

Uploaded by

r7596813
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)
2 views

JAVAARRAY

The document contains Java code examples for linear search, binary search, array sorting, and finding the largest element in an array. It demonstrates the implementation of each algorithm with sample arrays and outputs the results. Each section includes a main method to test the respective functionality.

Uploaded by

r7596813
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/ 6

// LINEAR SEARCH

public class LinearSearchExample{


public static int linearSearch(int[] arr, int key){
for(int i=0;i<arr.length;i++){
if(arr[i] == key){
return i;
}
}
return -1;
}
public static void main(String a[]){
int[] a1= {10,20,30,50,70,90};
int key = 50;
System.out.println(key+" is found at index: "+linearSearch(a1, ke
y));
}
}
public class BinarySearchIterative {

// Method to perform binary search on a sorted array


public static int binarySearch(int[] arr, int target) {
int left = 0;
int right = arr.length - 1;

while (left <= right) {


int mid = left + (right - left) / 2;

// Check if target is present at mid


if (arr[mid] == target) {
return mid; // Target found
}

// If target greater than mid, ignore left half


if (arr[mid] < target) {
left = mid + 1;
}
// If target is smaller than mid, ignore right half
else {
right = mid - 1;
}
}

// Target not found


return -1;
}

// Main method to test the binarySearch method


public static void main(String[] args) {
int[] arr = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};
int target = 23;
int result = binarySearch(arr, target);

if (result == -1) {
System.out.println("Element not present in the array.");
} else {
System.out.println("Element found at index: " + result);
}
}
}
// ARRAY SORTING
public class SortArrayExample2
{
public static void main(String[] args)
{
//creating an instance of an array
int[] arr = new int[] {78, 34, 1, 3, 90, 34, -1, -4, 6, 55, 20, -65};
System.out.println("Array elements after sorting:");
//sorting logic
for (int i = 0; i < arr.length; i++)
{
for (int j = i + 1; j < arr.length; j++)
{
int tmp = 0;
if (arr[i] > arr[j])
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
//prints the sorted element of the array
System.out.println(arr[i]);
}
}
}
// LARGEST ELEMENT IN AN ARRAY

public class LargestElement_array {


public static void main(String[] args) {

//Initialize array
int [] arr = new int [] {25, 11, 7, 75, 56};
//Initialize max with first element of array.
int max = arr[0];
//Loop through the array
for (int i = 0; i < arr.length; i++) {
//Compare elements of array with max
if(arr[i] > max)
max = arr[i];
}
System.out.println("Largest element present in given array: " + max);
}
}

You might also like