Searching and Sorting In Java
Java provides built-in methods and algorithms for efficiently searching and sorting data structures. In this article, we'll explore common searching
Searching Algorithms
Linear Search
Linear search is the simplest searching algorithm that sequentially checks each element in a list until a match is found or the entire list has been searched.
public class LinearSearch {
public static int linearSearch(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
return i; // Element found at index i
}
}
return -1; // Element not found
}
public static void main(String[] args) {
int[] array = {4, 2, 7, 1, 9, 5};
int targetElement = 7;
int result = linearSearch(array, targetElement);
if (result != -1) {
System.out.println("Element found at index: " + result);
} else {
System.out.println("Element not found in the array.");
}
}
}
Time Complexity :
best case: 0(1) -> found at index[0]
worst case: O(n) -> n is size of an array.
Binary Search
Binary search is a more efficient algorithm for sorted arrays. It works by repeatedly dividing the search interval in half.
import java.util.Arrays;
public class BinarySearch {
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;
if (arr[mid] == target) {
return mid; // Element found at index mid
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1; // Element not found
}
public static void main(String[] args) {
int[] array = {1, 2, 4, 5, 7, 9};
int targetElement = 5;
int result = binarySearch(array, targetElement);
if (result != -1) {
System.out.println("Element found at index: " + result);
} else {
System.out.println("Element not found in the array.");
}
}
}
Sorting Algorithms
Bubble Sort
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.
public class BubbleSort {
public static 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]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] array = {4, 2, 7, 1, 9, 5};
bubbleSort(array);
System.out.println("Sorted array: " + Arrays.toString(array));
}
}
Quick Sort
Quick Sort is a divide-and-conquer algorithm that works by selecting a 'pivot' element and partitioning the other elements into two sub-arrays.
import java.util.Arrays;
public class QuickSort {
public static 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);
}
}
public static 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++;
// Swap arr[i] and arr[j]
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// Swap arr[i+1] and arr[high] (pivot)
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}
public static void main(String[] args) {
int[] array = {4, 2, 7, 1, 9, 5};
quickSort(array, 0, array.length - 1);
System.out.println("Sorted array: " + Arrays.toString(array));
}
}
ย