From bcfcc29aa88f4f996ce4a82f40ad0e24a7ac26fc Mon Sep 17 00:00:00 2001 From: pojith raj R <126692672+pojith@users.noreply.github.com> Date: Mon, 25 Mar 2024 21:41:57 +0530 Subject: [PATCH 1/3] Create Breadth-First Search (BFS) Searching.java Signed-off-by: pojith raj R <126692672+pojith@users.noreply.github.com> --- ...Breadth-First Search (BFS) Searching .java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 Java/Algorithm/SearchingAlgorithm/Breadth-First Search (BFS) Searching .java 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..cd9dfccc --- /dev/null +++ b/Java/Algorithm/SearchingAlgorithm/Breadth-First Search (BFS) Searching .java @@ -0,0 +1,71 @@ +import java.util.*; + +public class BFS { + private int V; // Number of vertices + private LinkedList adj[]; // Adjacency list representation of the graph + + public BFS(int v) { + V = v; + adj = new LinkedList[v]; + for (int i = 0; i < v; ++i) + adj[i] = new LinkedList(); + } + + // Function to add an edge into the graph + void addEdge(int v, int w) { + adj[v].add(w); + } + + // BFS traversal starting from a given source vertex + 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 the queue and print it + s = queue.poll(); + System.out.print(s + " "); + + // Get all adjacent vertices of the dequeued vertex s. If an adjacent vertex has not been visited, + // then mark it visited and enqueue it + Iterator i = adj[s].listIterator(); + while (i.hasNext()) { + int n = i.next(); + if (!visited[n]) { + visited[n] = true; + queue.add(n); + } + } + } + } + + public static void main(String args[]) { + Scanner scanner = new Scanner(System.in); + + System.out.print("Enter the number of vertices: "); + int V = scanner.nextInt(); + + BFS graph = new BFS(V); + + System.out.println("Enter the edges (vertex pairs, -1 to stop): "); + while (true) { + int v1 = scanner.nextInt(); + if (v1 == -1) break; + int v2 = scanner.nextInt(); + graph.addEdge(v1, v2); + } + + System.out.print("Enter the starting vertex for BFS: "); + int startVertex = scanner.nextInt(); + + System.out.println("Breadth First Traversal starting from vertex " + startVertex + ":"); + graph.BFS(startVertex); + } +} From c09f28bef4b17c85b8a77ffcb4661ed8ba5ba765 Mon Sep 17 00:00:00 2001 From: pojith raj R <126692672+pojith@users.noreply.github.com> Date: Thu, 2 May 2024 09:53:26 +0530 Subject: [PATCH 2/3] Update Breadth-First Search (BFS) Searching .java Signed-off-by: pojith raj R <126692672+pojith@users.noreply.github.com> --- ...Breadth-First Search (BFS) Searching .java | 72 ++++++++++++------- 1 file changed, 48 insertions(+), 24 deletions(-) diff --git a/Java/Algorithm/SearchingAlgorithm/Breadth-First Search (BFS) Searching .java b/Java/Algorithm/SearchingAlgorithm/Breadth-First Search (BFS) Searching .java index cd9dfccc..c29145e5 100644 --- a/Java/Algorithm/SearchingAlgorithm/Breadth-First Search (BFS) Searching .java +++ b/Java/Algorithm/SearchingAlgorithm/Breadth-First Search (BFS) Searching .java @@ -1,40 +1,44 @@ import java.util.*; -public class BFS { +public class Graph { private int V; // Number of vertices - private LinkedList adj[]; // Adjacency list representation of the graph + private LinkedList adj[]; // Adjacency List - public BFS(int v) { + // Constructor + Graph(int v) { V = v; adj = new LinkedList[v]; for (int i = 0; i < v; ++i) adj[i] = new LinkedList(); } - // Function to add an edge into the graph + // Add an edge to the graph void addEdge(int v, int w) { - adj[v].add(w); + if (v >= 0 && v < V && w >= 0 && w < V) { + adj[v].add(w); + } else { + throw new IllegalArgumentException("Invalid edge: " + v + " -> " + w); + } } - // BFS traversal starting from a given source vertex + // 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<>(); + 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 the queue and print it + // Dequeue a vertex from queue and print it s = queue.poll(); System.out.print(s + " "); - // Get all adjacent vertices of the dequeued vertex s. If an adjacent vertex has not been visited, - // then mark it visited and enqueue it + // Get all adjacent vertices of the dequeued vertex s. Iterator i = adj[s].listIterator(); while (i.hasNext()) { int n = i.next(); @@ -46,26 +50,46 @@ void BFS(int s) { } } + // Driver method to public static void main(String args[]) { Scanner scanner = new Scanner(System.in); - System.out.print("Enter the number of vertices: "); - int V = scanner.nextInt(); + 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."); + } - BFS graph = new BFS(V); + Graph g = new Graph(V); - System.out.println("Enter the edges (vertex pairs, -1 to stop): "); - while (true) { - int v1 = scanner.nextInt(); - if (v1 == -1) break; - int v2 = scanner.nextInt(); - graph.addEdge(v1, v2); - } + 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.print("Enter the starting vertex for BFS: "); - int startVertex = scanner.nextInt(); + 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("Breadth First Traversal starting from vertex " + startVertex + ":"); - graph.BFS(startVertex); + 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(); + } } } From 65d7273b60af8b37bbbf37c350064fa1743beb32 Mon Sep 17 00:00:00 2001 From: pojith raj R <126692672+pojith@users.noreply.github.com> Date: Thu, 2 May 2024 09:59:03 +0530 Subject: [PATCH 3/3] Bucket sort Signed-off-by: pojith raj R <126692672+pojith@users.noreply.github.com> --- Java/Algorithm/SortingAlgorithm/Bucket sort | 80 +++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 Java/Algorithm/SortingAlgorithm/Bucket sort 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(); + } +}