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

Sorting and Searching

Uploaded by

freefireth123900
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)
11 views

Sorting and Searching

Uploaded by

freefireth123900
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/ 21

Implement in java

public class BinarySearch {


public static int binarySearch(int[] arr, int target) {
int left = 0, right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int target = 3;
int result = binarySearch(arr, target);
if (result != -1) {
System.out.println("Element found at index " + result);
} else {
System.out.println("Element not found");
}
}
}

Implement in C

#include <stdio.h>
int binary_search(int arr[], int size, int target) {
int left = 0, right = size - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int target = 3;
int size = sizeof(arr) / sizeof(arr[0]);
int result = binary_search(arr, size, target);
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found\n");
}
return 0;
}
Implement in java

import java.util.Arrays;

public class JumpSearch {


public static int jumpSearch(int[] arr, int target) {
int n = arr.length;
int step = (int) Math.floor(Math.sqrt(n));
int prev = 0;
while (arr[Math.min(step, n) - 1] < target) {
prev = step;
step += (int) Math.floor(Math.sqrt(n));
if (prev >= n) {
return -1;
}
}
for (int i = prev; i < Math.min(step, n); i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int target = 3;
int result = jumpSearch(arr, target);
if (result != -1) {
System.out.println("Element found at index " + result);
} else {
System.out.println("Element not found");
}
}
}

Implement in C

#include <stdio.h>
#include <math.h>
int jump_search(int arr[], int size, int target) {
int step = sqrt(size);
int prev = 0;
while (arr[(step < size ? step : size) - 1] < target) {
prev = step;
step += sqrt(size);
if (prev >= size) {
return -1;
}
}
for (int i = prev; i < (step < size ? step : size); i++)
{
if (arr[i] == target) {
return i;
}
}
return -1;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int target = 3;
int size = sizeof(arr) / sizeof(arr[0]);
int result = jump_search(arr, size, target);
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found\n");
}
return 0;
}
public class InterpolationSearch {
public static int interpolationSearch(int[] arr, int target) {
int low = 0, high = arr.length - 1;
while (low <= high && target >= arr[low] && target <= arr[high]) {
if (low == high) {
if (arr[low] == target) {
return low;
}
return -1;
}
int pos = low + ((target - arr[low]) * (high - low) / (arr[high] - arr[low]));
if (arr[pos] == target) {
return pos;
} else if (arr[pos] < target) {
low = pos + 1;
} else {
high = pos - 1;
}
}
return -1;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int target = 3;
int result = interpolationSearch(arr, target);
if (result != -1) {
System.out.println("Element found at index " + result);
} else {
System.out.println("Element not found");
}
}
}

Implement in C

#include <stdio.h>
int interpolation_search(int arr[], int size, int target) {
int low = 0, high = size - 1;
while (low <= high && target >= arr[low] && target <= arr[high]) {
int pos = low + ((double)(high - low) / (arr[high] - arr[low])) * (target - arr[low]);
if (arr[pos] == target) {
return pos;
} else if (arr[pos] < target) {
low = pos + 1;
} else {
high = pos - 1;
}
}
return -1;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int target = 3;
int size = sizeof(arr) / sizeof(arr[0]);
int result = interpolation_search(arr, size, target);
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found\n");
}
return 0;
}
Implement in java

public class ExponentialSearch {


public static int binarySearch(int[] arr, int left, int right, int target) {
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}

public static int exponentialSearch(int[] arr, int target) {


if (arr[0] == target) {
return 0;
}
int i = 1;
while (i < arr.length && arr[i] <= target) {
i *= 2;
}
return binarySearch(arr, i / 2, Math.min(i, arr.length - 1), target);
}

public static void main(String[] args) {


int[] arr = {1, 2, 3, 4, 5};
int target = 3;
int result = exponentialSearch(arr, target);
if (result != -1) {
System.out.println("Element found at index " + result);
} else {
System.out.println("Element not found");
}
}
}

Implement in C

#include <stdio.h>

int binary_search(int arr[], int left, int right, int target) {


while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
int exponential_search(int arr[], int size, int target) {
if (arr[0] == target) {
return 0;
}
int i = 1;
while (i < size && arr[i] <= target) {
i *= 2;
}
return binary_search(arr, i / 2, (i < size ? i : size - 1), target);
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int target = 3;
int size = sizeof(arr) / sizeof(arr[0]);
int result = exponential_search(arr, size, target);
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found\n");
}
return 0;
}
Implement in java

public class FibonacciSearch {


public static int min(int x, int y) {
return (x <= y) ? x : y;
}

public static int fibonacciSearch(int[] arr, int target) {


int fibMMm2 = 0;
int fibMMm1 = 1;
int fibM = fibMMm2 + fibMMm1;

while (fibM < arr.length) {


fibMMm2 = fibMMm1;
fibMMm1 = fibM;
fibM = fibMMm2 + fibMMm1;
}

int offset = -1;

while (fibM > 1) {


int i = min(offset + fibMMm2, arr.length - 1);

if (arr[i] < target) {


fibM = fibMMm1;
fibMMm1 = fibMMm2;
fibMMm2 = fibM - fibMMm1;
offset = i;
} else if (arr[i] > target) {
fibM = fibMMm2;
fibMMm1 = fibMMm1 - fibMMm2;
fibMMm2 = fibM - fibMMm1;
} else {
return i;
}
}

if (fibMMm1 == 1 && arr[offset + 1] == target) {


return offset + 1;
}

return -1;
}

public static void main(String[] args) {


int[] arr = {1, 2, 3, 4, 5};
int target = 3;
int result = fibonacciSearch(arr, target);
if (result != -1) {
System.out.println("Element found at index " + result);
} else {
System.out.println("Element not found");
}
}
}

Implement in C
#include <stdio.h>

int min(int x, int y) {


return (x <= y) ? x : y;
}

int fibonacci_search(int arr[], int size, int target) {


int fibMMm2 = 0;
int fibMMm1 = 1;
int fibM = fibMMm2 + fibMMm1;

while (fibM < size) {


fibMMm2 = fibMMm1;
fibMMm1 = fibM;
fibM = fibMMm2 + fibMMm1;
}

int offset = -1;

while (fibM > 1) {


int i = min(offset + fibMMm2, size - 1);

if (arr[i] < target) {


fibM = fibMMm1;
fibMMm1 = fibMMm2;
fibMMm2 = fibM - fibMMm1;
offset = i;
} else if (arr[i] > target) {
fibM = fibMMm2;
fibMMm1 = fibMMm1 - fibMMm2;
fibMMm2 = fibM - fibMMm1;
} else {
return i;
}
}

if (fibMMm1 && arr[offset + 1] == target) {


return offset + 1;
}

return -1;
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int target = 3;
int size = sizeof(arr) / sizeof(arr[0]);
int result = fibonacci_search(arr, size, target);
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found\n");
}
return 0;
}
Implement in java

public class TernarySearch {


public static int ternarySearch(int[] arr, int left, int right, int target) {
if (right >= left) {
int mid1 = left + (right - left) / 3;
int mid2 = right - (right - left) / 3;

if (arr[mid1] == target) {
return mid1;
}
if (arr[mid2] == target) {
return mid2;
}

if (target < arr[mid1]) {


return ternarySearch(arr, left, mid1 - 1, target);
} else if (target > arr[mid2]) {
return ternarySearch(arr, mid2 + 1, right, target);
} else {
return ternarySearch(arr, mid1 + 1, mid2 - 1, target);
}
}
return -1;
}

public static void main(String[] args) {


int[] arr = {1, 2, 3, 4, 5};
int target = 3;
int result = ternarySearch(arr, 0, arr.length - 1, target);
if (result != -1) {
System.out.println("Element found at index " + result);
} else {
System.out.println("Element not found");
}
}
}

Implement in C

#include <stdio.h>

int ternary_search(int arr[], int left, int right, int target) {


if (right >= left) {
int mid1 = left + (right - left) / 3;
int mid2 = right - (right - left) / 3;

if (arr[mid1] == target) {
return mid1;
}
if (arr[mid2] == target) {
return mid2;
}

if (target < arr[mid1]) {


return ternary_search(arr, left, mid1 - 1, target);
} else if (target > arr[mid2]) {
return ternary_search(arr, mid2 + 1, right, target);
} else {
return ternary_search(arr, mid1 + 1, mid2 - 1, target);
}
}
return -1;
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int target = 3;
int size = sizeof(arr) / sizeof(arr[0]);
int result = ternary_search(arr, 0, size - 1, target);
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found\n");
}
return 0;
}
Implement in java

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]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

public static void main(String[] args) {


int[] arr = {5, 2, 6, 1, 3, 4};
bubbleSort(arr);
for (int num : arr) {
System.out.print(num + " ");
}
}
}

Implement in C

#include <stdio.h>

void bubbleSort(int arr[], int n) {


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;
}
}
}
}

int main() {
int arr[] = {5, 2, 6, 1, 3, 4};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Implement in java

public class SelectionSort {


public static void selectionSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}

public static void main(String[] args) {


int[] arr = {5, 2, 6, 1, 3, 4};
selectionSort(arr);
for (int num : arr) {
System.out.print(num + " ");
}
}
}

Implement in C

#include <stdio.h>

void selectionSort(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}

int main() {
int arr[] = {5, 2, 6, 1, 3, 4};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Implement in java

public class InsertionSort {


public static void insertionSort(int[] arr) {
int n = arr.length;
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--;
}
arr[j + 1] = key;
}
}

public static void main(String[] args) {


int[] arr = {5, 2, 6, 1, 3, 4};
insertionSort(arr);
for (int num : arr) {
System.out.print(num + " ");
}
}
}

Implement in C

#include <stdio.h>

void insertionSort(int arr[], int n) {


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--;
}
arr[j + 1] = key;
}
}

int main() {
int arr[] = {5, 2, 6, 1, 3, 4};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Implement in java

public class MergeSort {


public static 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, 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++;
}
}

public static void mergeSort(int[] arr, int l, int r) {


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

public static void main(String[] args) {


int[] arr = {5, 2, 6, 1, 3, 4};
mergeSort(arr, 0, arr.length - 1);
for (int num : arr) {
System.out.print(num + " ");
}
}
}

Implement in C

#include <stdio.h>
void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[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, 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 mergeSort(int arr[], int l, int r) {


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

int main() {
int arr[] = {5, 2, 6, 1, 3, 4};
int n = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, n - 1);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Implement in java
public class QuickSort {
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++;
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;
}

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 void main(String[] args) {


int[] arr = {5, 2, 6, 1, 3, 4};
quickSort(arr, 0, arr.length - 1);
for (int num : arr) {
System.out.print(num + " ");
}
}
}

Implement in C

#include <stdio.h>

void swap(int* a, int* b) {


int temp = *a;
*a = *b;
*b = temp;
}

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], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return i + 1;
}
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 main() {
int arr[] = {5, 2, 6, 1, 3, 4};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Implement in java

public class HeapSort {


public static void heapify(int[] arr, int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
if (largest != i) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}

public static void heapSort(int[] arr) {


int n = arr.length;
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);
}
}

public static void main(String[] args) {


int[] arr = {5, 2, 6, 1, 3, 4};
heapSort(arr);
for (int num : arr) {
System.out.print(num + " ");
}
}
}
Implement in C

#include <stdio.h>

void swap(int* a, int* b) {


int temp = *a;
*a = *b;
*b = temp;
}

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


int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}

void heapSort(int arr[], int n) {


for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
for (int i = n - 1; i > 0; i--) {
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
}
}

int main() {
int arr[] = {5, 2, 6, 1, 3, 4};
int n = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, n);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Implement in java

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;
}
}
return -1;
}

public static void main(String[] args) {


int[] arr = {1, 2, 3, 4, 5};
int target = 3;
int result = linearSearch(arr, target);
if (result != -1) {
System.out.println("Element found at index " + result);
} else {
System.out.println("Element not found");
}
}
}

Implement in C

#include <stdio.h>

int linear_search(int arr[], int size, int target) {


for (int i = 0; i < size; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int target = 3;
int size = sizeof(arr) / sizeof(arr[0]);
int result = linear_search(arr, size, target);
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found\n");
}
return 0;
}

You might also like