0% found this document useful (0 votes)
6 views6 pages

sakkk

j

Uploaded by

malikumair6748
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)
6 views6 pages

sakkk

j

Uploaded by

malikumair6748
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

import java.util.

Scanner;

public class InterpolationSearch {

// Interpolation Search method

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

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

while (low <= high && target >= arr[low] && target <= arr[high]) {

// Formula for Interpolation Search

int pos = low + ((target - arr[low]) * (high - low)) / (arr[high] - arr[low]);

// Check if the target is at the position

if (arr[pos] == target) {

return pos;

// If the target is larger, ignore the left half

else if (arr[pos] < target) {

low = pos + 1;

// If the target is smaller, ignore the right half

else {

high = pos - 1;

return -1; // Return -1 if the target is not found

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

// Input array size

System.out.println("Enter the size of the array:");

int n = scanner.nextInt();

int[] arr = new int[n];

// Input array elements

System.out.println("Enter " + n + " sorted integers:");

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

arr[i] = scanner.nextInt();

// Input target value

System.out.println("Enter the target value to search:");

int target = scanner.nextInt();

// Search for the target using interpolation search

int result = interpolationSearch(arr, target);

if (result == -1) {

System.out.println("Target value not found in the array.");

} else {

System.out.println("Target value found at index: " + result);

scanner.close();

}
import java.util.Scanner;

public class RadixSort {

// Method to perform counting sort on the array based on a particular digit (place value)

private static void countingSort(int[] arr, int exp) {

int n = arr.length;

int[] output = new int[n];

int[] count = new int[10]; // For digits 0-9

// Count occurrences of digits

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

count[(arr[i] / exp) % 10]++;

// Calculate cumulative count

for (int i = 1; i < 10; i++) {


count[i] += count[i - 1];

// Place the elements in output array

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

int digit = (arr[i] / exp) % 10;

output[count[digit] - 1] = arr[i];

count[digit]--;

// Copy the sorted output array back to the original array

System.arraycopy(output, 0, arr, 0, n);

// Method to implement Radix Sort

public static void radixSort(int[] arr) {

int max = arr[0];

// Find the maximum number to know the maximum number of digits

for (int num : arr) {

if (num > max) {

max = num;

// Apply counting sort for every digit

for (int exp = 1; max / exp > 0; exp *= 10) {

countingSort(arr, exp);

}
}

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Input array size

System.out.println("Enter the size of the array:");

int n = scanner.nextInt();

int[] arr = new int[n];

// Input array elements

System.out.println("Enter " + n + " non-negative integers:");

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

arr[i] = scanner.nextInt();

if (arr[i] < 0) {

System.out.println("Please enter non-negative integers only.");

return;

// Apply Radix Sort

radixSort(arr);

// Output sorted array

System.out.println("Sorted array:");

for (int num : arr) {

System.out.print(num + " ");

System.out.println();
scanner.close();

}ajjadddddd

You might also like