DSA_Lab assignment
DSA_Lab assignment
Abbottabad Campus
Department of Computer Science
Lab Assignment-02 – Fall-24
Class: BCS-4A Date: 1/1/2025
Subject: Data Structure and Algorithms Total Time Allowed: 60 minutes
Name: Tayyab Shahid Registration #SP23-BCS-052
(CLO-3)
Mar
ks (10)
Question-1: Write a Java program to implement the Interpolation Search algorithm. The program should
allow the user to input a sorted array of integers and a target value to search. Use the Interpolation Search
technique to find the position of the target value in the array. If the target value is found, display its index;
otherwise, indicate that the value is not present in the array.
Requirements:
• Input:
• Output:
• A message indicating the target is not found if it does not exist in the array.
• Implementation:
• Optimize the search by narrowing the search range based on key distribution.
• Validation:
• Handle cases where the array is empty or contains only one element.
Program:
import java.util.Scanner;
while (low <= high && target >= arr[low] && target <= arr[high]) {
if (low == high) {
return -1;
if (arr[pos] == target)
return pos;
low = pos + 1;
else
high = pos - 1;
return -1;
}
public static void main(String[] args) {
int n = scanner.nextInt();
if (n <= 0) {
arr[i] = scanner.nextInt();
if (result != -1)
System.out.println("Target found at index: " + result);
else
Output:
Question-2: Write a Java program to implement the Radix Sort algorithm. The program should allow the
user to input an array of non-negative integers and sort the array in ascending order using the Radix Sort
technique. Display the sorted array as output.
Requirements:
• Input:
• Output:
• Implementation:
• Use the Radix Sort algorithm, which sorts numbers digit by digit starting from the least
significant digit (LSD).
• Validation:
• Handle cases where the array is empty.
Program:
import java.util.*;
int n = scanner.nextInt();
if (n <= 0) {
return;
arr[i] = scanner.nextInt();
if (arr[i] < 0) {
return;
}
}
RadixSort.radixSort(arr);
System.out.println("Sorted array:");
System.out.println(Arrays.toString(arr));
scanner.close();
}
class RadixSort {
countSort(arr, exp);
return max;
}
int n = arr.length;
Output: