0% found this document useful (0 votes)
1 views5 pages

Searching Sorting Java Guide

The document outlines various searching and sorting algorithms implemented in Java. It includes linear search, binary search (both iterative and recursive), and sorting techniques such as bubble sort, selection sort, insertion sort, merge sort, and quick sort. Each algorithm is presented with its respective code implementation.

Uploaded by

ayush dutta
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)
1 views5 pages

Searching Sorting Java Guide

The document outlines various searching and sorting algorithms implemented in Java. It includes linear search, binary search (both iterative and recursive), and sorting techniques such as bubble sort, selection sort, insertion sort, merge sort, and quick sort. Each algorithm is presented with its respective code implementation.

Uploaded by

ayush dutta
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

Important Searching and Sorting Algorithms - Java Edition

SEARCHING TECHNIQUES

1. Linear Search

------------------------------

int linearSearch(int[] arr, int target) {

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

if (arr[i] == target) return i;

return -1;

2. Binary Search (Iterative)

------------------------------

int binarySearch(int[] arr, int target) {

int low = 0, high = arr.length - 1;

while (low <= high) {

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

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

else if (arr[mid] < target) low = mid + 1;

else high = mid - 1;

return -1;

}
3. Binary Search (Recursive)

------------------------------

int binarySearchRecursive(int[] arr, int low, int high, int target) {

if (low > high) return -1;

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

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

else if (arr[mid] < target)

return binarySearchRecursive(arr, mid + 1, high, target);

else

return binarySearchRecursive(arr, low, mid - 1, target);

SORTING TECHNIQUES

1. Bubble Sort

------------------------------

void bubbleSort(int[] arr) {

int n = arr.length;

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

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

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

int temp = arr[j];

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

arr[j + 1] = temp;

}
}

2. Selection Sort

------------------------------

void selectionSort(int[] arr) {

int n = arr.length;

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

int minIdx = i;

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

if (arr[j] < arr[minIdx]) minIdx = j;

int temp = arr[minIdx];

arr[minIdx] = arr[i];

arr[i] = temp;

3. Insertion Sort

------------------------------

void insertionSort(int[] arr) {

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

int key = arr[i];

int j = i - 1;

while (j >= 0 && arr[j] > key) {

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

j--;
}

arr[j + 1] = key;

4. Merge Sort

------------------------------

void mergeSort(int[] arr, int left, int right) {

if (left < right) {

int mid = (left + right) / 2;

mergeSort(arr, left, mid);

mergeSort(arr, mid + 1, right);

merge(arr, left, mid, right);

void merge(int[] arr, int left, int mid, int right) {

int n1 = mid - left + 1;

int n2 = right - mid;

int[] L = new int[n1];

int[] R = new int[n2];

for (int i = 0; i < n1; i++) L[i] = arr[left + i];

for (int j = 0; j < n2; j++) R[j] = arr[mid + 1 + j];

int i = 0, j = 0, k = left;

while (i < n1 && j < n2) arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];

while (i < n1) arr[k++] = L[i++];

while (j < n2) arr[k++] = R[j++];


}

5. Quick Sort

------------------------------

void quickSort(int[] arr, int low, int high) {

if (low < high) {

int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

int partition(int[] arr, int low, int high) {

int pivot = arr[high];

int i = low - 1;

for (int j = low; j < high; j++) {

if (arr[j] < pivot) {

i++;

int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;

int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp;

return i + 1;

You might also like