diff --git a/Java/Algorithm/SearchingAlgorithm/Breadth-First Search (BFS) Searching .java b/Java/Algorithm/SearchingAlgorithm/Breadth-First Search (BFS) Searching .java new file mode 100644 index 00000000..c29145e5 --- /dev/null +++ b/Java/Algorithm/SearchingAlgorithm/Breadth-First Search (BFS) Searching .java @@ -0,0 +1,95 @@ +import java.util.*; + +public class Graph { + private int V; // Number of vertices + private LinkedList adj[]; // Adjacency List + + // Constructor + Graph(int v) { + V = v; + adj = new LinkedList[v]; + for (int i = 0; i < v; ++i) + adj[i] = new LinkedList(); + } + + // Add an edge to the graph + void addEdge(int v, int w) { + if (v >= 0 && v < V && w >= 0 && w < V) { + adj[v].add(w); + } else { + throw new IllegalArgumentException("Invalid edge: " + v + " -> " + w); + } + } + + // BFS traversal from a given source s + void BFS(int s) { + // Mark all the vertices as not visited + boolean visited[] = new boolean[V]; + + // Create a queue for BFS + LinkedList queue = new LinkedList(); + + // Mark the current node as visited and enqueue it + visited[s] = true; + queue.add(s); + + while (queue.size() != 0) { + // Dequeue a vertex from queue and print it + s = queue.poll(); + System.out.print(s + " "); + + // Get all adjacent vertices of the dequeued vertex s. + Iterator i = adj[s].listIterator(); + while (i.hasNext()) { + int n = i.next(); + if (!visited[n]) { + visited[n] = true; + queue.add(n); + } + } + } + } + + // Driver method to + public static void main(String args[]) { + Scanner scanner = new Scanner(System.in); + + try { + System.out.print("Enter the number of vertices: "); + int V = scanner.nextInt(); + if (V <= 0) { + throw new IllegalArgumentException("Number of vertices must be positive."); + } + + Graph g = new Graph(V); + + System.out.println("Enter the number of edges: "); + int E = scanner.nextInt(); + if (E < 0) { + throw new IllegalArgumentException("Number of edges must be non-negative."); + } + + System.out.println("Enter the edges (source destination): "); + for (int i = 0; i < E; i++) { + int source = scanner.nextInt(); + int destination = scanner.nextInt(); + g.addEdge(source, destination); + } + + System.out.print("Enter the starting vertex for BFS: "); + int startVertex = scanner.nextInt(); + if (startVertex < 0 || startVertex >= V) { + throw new IllegalArgumentException("Starting vertex is out of bounds."); + } + + System.out.println("BFS Traversal starting from vertex " + startVertex + ":"); + g.BFS(startVertex); + } catch (InputMismatchException e) { + System.out.println("Invalid input. Please enter integers only."); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } finally { + scanner.close(); + } + } +} diff --git a/Java/Algorithm/SortingAlgorithm/Bucket sort b/Java/Algorithm/SortingAlgorithm/Bucket sort new file mode 100644 index 00000000..f3babf6e --- /dev/null +++ b/Java/Algorithm/SortingAlgorithm/Bucket sort @@ -0,0 +1,80 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.InputMismatchException; +import java.util.Scanner; + +public class BucketSort { + public static void bucketSort(double[] arr) { + int n = arr.length; + if (n <= 0) return; + + // Create empty buckets + ArrayList[] buckets = new ArrayList[n]; + for (int i = 0; i < n; i++) { + buckets[i] = new ArrayList<>(); + } + + // Scatter the elements into the buckets + for (int i = 0; i < n; i++) { + int bucketIndex = (int) (n * arr[i]); + buckets[bucketIndex].add(arr[i]); + } + + // Sort each bucket + for (int i = 0; i < n; i++) { + Collections.sort(buckets[i]); + } + + // Concatenate the buckets + int index = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < buckets[i].size(); j++) { + arr[index++] = buckets[i].get(j); + } + } + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + try { + System.out.print("Enter the number of elements: "); + int n = scanner.nextInt(); + if (n <= 0) { + throw new IllegalArgumentException("Number of elements must be positive."); + } + + double[] arr = new double[n]; + + System.out.println("Enter the elements (between 0 and 1): "); + for (int i = 0; i < n; i++) { + double element = scanner.nextDouble(); + if (element < 0 || element > 1) { + throw new IllegalArgumentException("Elements must be between 0 and 1."); + } + arr[i] = element; + } + + System.out.println("Original array:"); + printArray(arr); + + bucketSort(arr); + + System.out.println("Sorted array:"); + printArray(arr); + } catch (InputMismatchException e) { + System.out.println("Invalid input. Please enter valid numbers."); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } finally { + scanner.close(); + } + } + + public static void printArray(double[] arr) { + for (double value : arr) { + System.out.print(value + " "); + } + System.out.println(); + } +} diff --git a/Java/Algorithm/SortingAlgorithm/Heap sort b/Java/Algorithm/SortingAlgorithm/Heap sort new file mode 100644 index 00000000..0f036c9b --- /dev/null +++ b/Java/Algorithm/SortingAlgorithm/Heap sort @@ -0,0 +1,85 @@ +import java.util.Arrays; +import java.util.InputMismatchException; +import java.util.Scanner; + +public class HeapSort { + + // Function to perform heap sort + public static void heapSort(int[] arr) { + int n = arr.length; + + // Build heap (rearrange array) + for (int i = n / 2 - 1; i >= 0; i--) { + heapify(arr, n, i); + } + + // One by one extract an element from heap + for (int i = n - 1; i > 0; i--) { + // Move current root to end + int temp = arr[0]; + arr[0] = arr[i]; + arr[i] = temp; + + // call max heapify on the reduced heap + heapify(arr, i, 0); + } + } + + // To heapify a subtree rooted with node i which is an index in arr[] + public static void heapify(int[] arr, int n, int i) { + int largest = i; // Initialize largest as root + int l = 2 * i + 1; // left = 2*i + 1 + int r = 2 * i + 2; // right = 2*i + 2 + + // If left child is larger than root + if (l < n && arr[l] > arr[largest]) { + largest = l; + } + + // If right child is larger than largest so far + if (r < n && arr[r] > arr[largest]) { + largest = r; + } + + // If largest is not root + if (largest != i) { + int swap = arr[i]; + arr[i] = arr[largest]; + arr[largest] = swap; + + // Recursively heapify the affected sub-tree + heapify(arr, n, largest); + } + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + try { + System.out.print("Enter the number of elements: "); + int n = scanner.nextInt(); + if (n <= 0) { + throw new IllegalArgumentException("Number of elements must be positive."); + } + + int[] arr = new int[n]; + + System.out.println("Enter the elements:"); + for (int i = 0; i < n; i++) { + arr[i] = scanner.nextInt(); + } + + System.out.println("Original array: " + Arrays.toString(arr)); + + heapSort(arr); + + System.out.println("Sorted array: " + Arrays.toString(arr)); + } catch (InputMismatchException e) { + System.out.println("Invalid input. Please enter integers only."); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } finally { + scanner.close(); + } + } +} diff --git a/Java/Algorithm/SortingAlgorithm/radix sort b/Java/Algorithm/SortingAlgorithm/radix sort new file mode 100644 index 00000000..a5cd183f --- /dev/null +++ b/Java/Algorithm/SortingAlgorithm/radix sort @@ -0,0 +1,86 @@ +import java.util.Arrays; +import java.util.InputMismatchException; +import java.util.Scanner; + +public class RadixSort { + + // Function to find the maximum element in the array + public static int getMax(int[] arr) { + int max = arr[0]; + for (int i = 1; i < arr.length; i++) { + if (arr[i] > max) { + max = arr[i]; + } + } + return max; + } + + // A function to do counting sort of arr[] according to the digit represented by exp + public static void countSort(int[] arr, int exp) { + int n = arr.length; + int[] output = new int[n]; // output array + int[] count = new int[10]; + Arrays.fill(count, 0); + + // Store count of occurrences in count[] + for (int i = 0; i < n; i++) { + count[(arr[i] / exp) % 10]++; + } + + // Change count[i] so that count[i] now contains actual position of this digit in output[] + for (int i = 1; i < 10; i++) { + count[i] += count[i - 1]; + } + + // Build the output array + for (int i = n - 1; i >= 0; i--) { + output[count[(arr[i] / exp) % 10] - 1] = arr[i]; + count[(arr[i] / exp) % 10]--; + } + + // Copy the output array to arr[], so that arr[] now contains sorted numbers according to the current digit + System.arraycopy(output, 0, arr, 0, n); + } + + // The main function to that sorts arr[] of size n using Radix Sort + public static void radixSort(int[] arr) { + int max = getMax(arr); + + // Do counting sort for every digit. + // Instead of passing digit number, exp is passed. exp is 10^i where i is the current digit number + for (int exp = 1; max / exp > 0; exp *= 10) { + countSort(arr, exp); + } + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + try { + System.out.print("Enter the number of elements: "); + int n = scanner.nextInt(); + if (n <= 0) { + throw new IllegalArgumentException("Number of elements must be positive."); + } + + int[] arr = new int[n]; + + System.out.println("Enter the elements:"); + for (int i = 0; i < n; i++) { + arr[i] = scanner.nextInt(); + } + + System.out.println("Original array: " + Arrays.toString(arr)); + + radixSort(arr); + + System.out.println("Sorted array: " + Arrays.toString(arr)); + } catch (InputMismatchException e) { + System.out.println("Invalid input. Please enter integers only."); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } finally { + scanner.close(); + } + } +}