0% found this document useful (0 votes)
7 views7 pages

Java Search and Sort Algorithms

The document contains multiple experiments demonstrating various sorting and searching algorithms implemented in Java, including recursive linear and binary search, heap sort, merge sort, selection sort, and insertion sort. Each experiment provides the program code and outputs the sorted results or search results. The objective of each experiment is to implement the respective algorithm and showcase its functionality.

Uploaded by

sahumanoj8090
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)
7 views7 pages

Java Search and Sort Algorithms

The document contains multiple experiments demonstrating various sorting and searching algorithms implemented in Java, including recursive linear and binary search, heap sort, merge sort, selection sort, and insertion sort. Each experiment provides the program code and outputs the sorted results or search results. The objective of each experiment is to implement the respective algorithm and showcase its functionality.

Uploaded by

sahumanoj8090
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

EXPERIMENT NO.

1
Title: Recursive Linear and Binary Search
Objective: To implement recursive linear and binary search algorithms in Java.

Program Code:
import [Link].*;

public class RecursiveSearch {


// Recursive Linear Search
static int linearSearch(int arr[], int n, int x) {
if (n == 0)
return -1;
if (arr[n - 1] == x)
return n - 1;
return linearSearch(arr, n - 1, x);
}

// Recursive Binary Search


static int binarySearch(int arr[], int low, int high, int x) {
if (high >= low) {
int mid = low + (high - low) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, low, mid - 1, x);
return binarySearch(arr, mid + 1, high, x);
}
return -1;
}

public static void main(String args[]) {


int arr[] = {2, 3, 4, 10, 40};
int n = [Link];
int x = 10;

int result1 = linearSearch(arr, n, x);


[Link]("Linear Search Result: " + (result1 == -1 ? "Not found" :

int result2 = binarySearch(arr, 0, n - 1, x);


[Link]("Binary Search Result: " + (result2 == -1 ? "Not found" :
}
}
Output:

Linear Search Result: Found at index 3


Binary Search Result: Found at index 3
EXPERIMENT NO. 2
Title: Heap Sort
Objective: To implement heap sort algorithm in Java.

Program Code:
public class HeapSort {
public void sort(int arr[]) {
int n = [Link];
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (int i = n - 1; i > 0; i--) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}

void heapify(int arr[], int n, int i) {


int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;

if (l < n && arr[l] > arr[largest])


largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;

if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
heapify(arr, n, largest);
}
}

public static void main(String args[]) {


int arr[] = {12, 11, 13, 5, 6, 7};
HeapSort ob = new HeapSort();
[Link](arr);
[Link]("Sorted array:");
for (int i : arr)
[Link](i + " ");
}
}
Output:
Sorted array:
5 6 7 11 12 13
EXPERIMENT NO. 3
Title: Merge Sort
Objective: To implement merge sort algorithm in Java.

Program Code:
public class MergeSort {
void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int L[] = new int[n1];
int R[] = new int[n2];
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];

int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}

void sort(int arr[], int l, int r) {


if (l < r) {
int m = (l + r) / 2;
sort(arr, l, m);
sort(arr, m + 1, r);
merge(arr, l, m, r);
}
}

public static void main(String args[]) {


int arr[] = {12, 11, 13, 5, 6, 7};
MergeSort ob = new MergeSort();
[Link](arr, 0, [Link] - 1);
[Link]("Sorted array:");
for (int i : arr)
[Link](i + " ");
}
}
Output:

Sorted array:
5 6 7 11 12 13
EXPERIMENT NO. 4
Title: Selection Sort
Objective: To implement selection sort algorithm in Java.

Program Code:
public class SelectionSort {
void sort(int arr[]) {
int n = [Link];
for (int i = 0; i < n - 1; i++) {
int min_idx = i;
for (int j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}

public static void main(String args[]) {


int arr[] = {64, 25, 12, 22, 11};
SelectionSort ob = new SelectionSort();
[Link](arr);
[Link]("Sorted array:");
for (int i : arr)
[Link](i + " ");
}
}
Output:

Sorted array:
11 12 22 25 64
EXPERIMENT NO. 5
Title: Insertion Sort
Objective: To implement insertion sort algorithm in Java.

Program Code:
public class InsertionSort {
void sort(int arr[]) {
int n = [Link];
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

public static void main(String args[]) {


int arr[] = {12, 11, 13, 5, 6};
InsertionSort ob = new InsertionSort();
[Link](arr);
[Link]("Sorted array:");
for (int i : arr)
[Link](i + " ");
}
}
Output:

Sorted array:
5 6 11 12 13

You might also like