0% found this document useful (0 votes)
12 views13 pages

ADS P1 Corrected

The document contains Java programs implementing various search and sorting algorithms, including linear search, binary search, bubble sort, insertion sort, selection sort, and shell sort. Each program includes code snippets, user input prompts, and example outputs demonstrating the functionality of the algorithms. The programs are designed to handle arrays of integers and provide sorted results or search results based on user input.

Uploaded by

jiteshkadam2003
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)
12 views13 pages

ADS P1 Corrected

The document contains Java programs implementing various search and sorting algorithms, including linear search, binary search, bubble sort, insertion sort, selection sort, and shell sort. Each program includes code snippets, user input prompts, and example outputs demonstrating the functionality of the algorithms. The programs are designed to handle arrays of integers and provide sorted results or search results based on user input.

Uploaded by

jiteshkadam2003
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/ 13

1.

Write a java program to implement a sequential search algorithm for searching for a
key
Code:
import java.util.Scanner; public class LinearSearch {
public static int linearSearch(int[] num, int size, int key) { for (int i = 0; i < size; i++) {
if (num[i] == key) { return i;
}
}
return -1;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter array size: "); int size = scanner.nextInt();
int[] num = new int[size];
System.out.println("Enter array elements:"); for (int i = 0; i < size; i++) {
System.out.print("Enter integer: "); num[i] = scanner.nextInt();
}
System.out.print("Enter element to search: "); int key = scanner.nextInt();

int result = linearSearch(num, size, key); if (result != -1) {


System.out.println("Element found at index: " + result);
} else {
System.out.println("Element not found");
}
scanner.close();
}
}
OUTPUT:
Enter array
size: 5 Enter
array
elements:
Enter integer: 10
Enter integer: 25
Enter integer: 30
Enter integer: 45
Enter integer: 50
Enter element to search: 30 Element found at index: 2
2. Write a java program to implement a recursive binary search algorithm for searching
for a key.
Code:
import java.util.Scanner; class BinarySearchRecursive {
public static int binarySearch(int[] list, int top, int bottom, int key) { if (top <= bottom) {
int mid = (top + bottom) / 2; if (list[mid] == key) {
return mid;
}
if (key > list[mid]) {
return binarySearch(list, mid + 1, bottom, key);
}
return binarySearch(list, top, mid - 1, key);
}
return -1;
}
}

public class BinarySearchDem {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements in the array: "); int size = sc.nextInt();
int[] list = new int[size];
System.out.println("Enter " + size + " sorted elements:")
for (int i = 0; i < size; i++) {
list[i] = sc.nextInt();
}

System.out.print("Enter the key to search


for: "); int key = sc.nextInt();

int result = BinarySearchRecursive.binarySearch(list, 0, size -


1, key); if (result != -1) {
System.out.println("Key found at position " + (result + 1) + ".");
} else {
System.out.println("Element not found.");
}
sc.close();
}
}

OUTPUT:
Enter the number of elements in the array: 5 Enter 5 sorted elements:
10 20 30 40 50
Enter the key to search for: 30 Key found at position 3.

Enter the key to search for: 60 Element not found.


3. Write a java program to implement a Bubble sort algorithm to sort 15 numbers.
Code:
import java.util.Scanner; class BubbleSort {
public static void bubbleSort(long[] list, long size) { boolean flag;

for (long hold = 0; hold < size - 1; hold++) { flag = false;


for (long walker = 0; walker < size - hold - 1; walker++) {
if (list[(int) walker] > list[(int) (walker + 1)]) { long temp = list[(int) walker];
list[(int) walker] = list[(int) (walker + 1)]; list[(int) (walker + 1)] = temp;
flag = true;
}
}
if (!flag) { break;
}
System.out.print("\nPass " + (hold + 1) + ": "); displayArray(list);
}
}
public static void displayArray(long[] array) { for (long value : array) {
System.out.print(value + " ");
}
System.out.println();
}
}
public class BubbleSortDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); final int SIZE = 15;

long[] num = new long[SIZE];


System.out.println("Enter " + SIZE + " elements:"); for (int i = 0; i < SIZE; i++) {num[i] =
sc.nextLong();
}

System.out.print("\nUnsorted array: "); BubbleSort.displayArray(num);


BubbleSort.bubbleSort(num, SIZE); System.out.print("\nSorted array: ");
BubbleSort.displayArray(num);
sc.close();
}
}

OUTPUT:
Enter 15 elements:
34 7 23 32 5 62 32 2 25 12 10 3 15 9 8
Unsorted array: 34 7 23 32 5 62 32 2 25 12 10 3 15 9 8
Pass 1: 7 23 32 5 34 32 2 25 12 10 3 15 9 8
Pass 2: 7 23 5 32 32 2 25 12 10 3 15 9 8
Pass 3: 7 5 23 32 2 25 12 10 3 15 9 8
Pass 4: 5 7 23 32 2 25 12 10 3 15 9 8
Pass 5: 5 7 23 2 32 25 12 10 3 15 9 8
Pass 6: 5 7 2 23 25 32 12 10 3 15 9 8
Pass 7: 5 2 7 23 25 12 10 3 15 9 8
Pass 8: 2 5 7 23 12 10 3 15 9 8
Pass 9: 2 5 7 12 23 10 3 15 9 8
Pass 10: 2 5 7 10 12 23 3 15 9 8
Pass 11: 2 5 7 10 12 3 23 15 9 8
Pass 12: 2 5 7 10 3 12 15 23 9 8
Pass 13: 2 5 7 3 10 12 15 9 23 8
Pass 14: 2 5 3 7 10 12 9 15 23 8
Pass 15: 2 3 5 7 10 9 12 15 23 34
Sorted array: 2 3 5 7 8 9 10 12 15 23 32 34 62
4. Write a java program to sort an array of 10 numbers using Insertion Sort.
Code:
import java.util.Scanner;

public class InsertionSortDemo {


public static void insertionSort(int[] list, int n) {
int key, hold, walker;
for (key = 1; key < n; key++) { hold = list[key];
walker = key - 1;
while (walker >= 0 && hold < list[walker]) { list[walker + 1] = list[walker];
walker--;
}
list[walker + 1] = hold;
System.out.print("\nPass " + key + ": "); for (int i = 0; i < n; i++) {
System.out.print(list[i] + " ");
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); int n = 10;
int[] num = new int[n];
System.out.println("Enter 10 elements:"); for (int i = 0; i < n; i++) {
num[i] = scanner.nextInt();
}

System.out.print("\nUnsorted array: "); for (int i = 0; i < n; i++) {


System.out.print(num[i] + " ");
}
insertionSort(num, n);
System.out.print("\n\nSorted array: "); for (int i = 0; i < n; i++) {
System.out.print(num[i] + " ");
}
scanner.close();
}
}

OUTPUT:
Enter 10 elements:
34 7 23 32 5 62 32 2 25 12

Unsorted array: 34 7 23 32 5 62 32 2 25 12
Pass 1: 7 34 23 32 5 62 32 2 25 12
Pass 2: 7 23 34 32 5 62 32 2 25 12
Pass 3: 7 23 32 34 5 62 32 2 25 12
Pass 4: 5 7 23 32 34 62 32 2 25 12
Pass 5: 5 7 23 32 32 34 62 2 25 12
Pass 6: 2 5 7 23 32 32 34 62 25 12
Pass 7: 2 5 7 23 25 32 32 34 62 12
Pass 8: 2 5 7 12 23 25 32 32 34 62

Sorted array: 2 5 7 12 23 25 32 32 34 62
5. Write a java implement a Selection Sort algorithm for sorting 10 numbers.
Code:
import java.util.Scanner;
public class SelectionSortDemo {
public static void selectionSort(int[] list, int n) {
for (int hold = 0; hold < n - 1; hold++) {
int pos = hold;
for (int walker = hold + 1; walker < n; walker++) {
if (list[walker] < list[pos]) { pos = walker;
}
}
if (hold != pos) {
int temp = list[pos];
list[pos] = list[hold]; list[hold] = temp;
}
System.out.print("Pass " + (hold + 1) + ": ");
for (int i = 0; i < n; i++) { System.out.print(list[i] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = 10;
int[] num = new int[n];

System.out.println("Enter 10 elements:");
for (int i = 0; i < n; i++) {
num[i] = scanner.nextInt();
}
System.out.print("\nUnsorted array: ");
for (int i = 0; i < n; i++) { System.out.print(num[i] + " ");
}
System.out.println();
selectionSort(num, n);
System.out.print("\nSorted array: ");
for (int i = 0; i < n; i++) { System.out.print(num[i] + " ");
}
scanner.close();
}
}
OUTPUT:
Enter 10 elements:
34
7
23
32
5
62
32
2
25
12

Unsorted array: 34 7 23 32 5 62 32 2 25 12
Pass 1: 7 34 23 32 5 62 32 2 25 12
Pass 2: 7 23 34 32 5 62 32 2 25 12
Pass 3: 7 23 32 34 5 62 32 2 25 12
Pass 4: 5 7 23 32 34 62 32 2 25 12
Pass 5: 5 7 23 32 32 34 62 2 25 12
Pass 6: 2 5 7 23 32 32 34 62 25 12
Pass 7: 2 5 7 23 25 32 32 34 62 12
Pass 8: 2 5 7 12 23 25 32 32 34 62
Pass 9: 2 5 7 12 23 25 32 32 34 62

Sorted array: 2 5 7 12 23 25 32 32 34 62
6. Write a Java program to sort an array of 15 numbers using Shell Sort. Take input from
num.txt file.
Code:
import java.io.File;
import java.io.FileNotFoundException; import java.util.Scanner;

public class ShellSort {


public static void shellSort(int[] array) { int n = array.length;
for (int gap = n / 2; gap > 0; gap /= 2) { for (int i = gap; i < n; i++) {
int temp = array[i]; int j;
for (j = i; j >= gap && array[j - gap] > temp; j -= gap) { array[j] = array[j - gap];
}
array[j] = temp;
}
}
}

public static void main(String[] args) { int[] numbers = new int[15];


try {
Scanner scanner = new Scanner(new File("num.txt")); int index = 0;
while (scanner.hasNextInt() && index < 15) { numbers[index++] = scanner.nextInt();
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage()); return;
}
System.out.print("Unsorted array: "); for (int num : numbers) {
System.out.print(num + " ");
}
System.out.println(); shellSort(numbers);
System.out.print("Sorted array: "); for (int num : numbers) {
System.out.print(num + " ");
}
}
}

OUTPUT:

num.txt 34
7
23
32
5
62
32
2
25
12
10
3
15
9
8

Unsorted array: 34 7 23 32 5 62 32 2 25 12 10 3 15 9 8
Sorted array: 2 3 5 7 8 9 10 12 15 23 25 32 32 34 62

You might also like