Skip to content

Commit 7e9292f

Browse files
Merge pull request #1 from wildchaser1703/wildchaser1703-patch-1
Added Searching Algorithms
2 parents 4ed9867 + f555bce commit 7e9292f

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

java/searching/BFS.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Implementation of BFS in Java
2+
// BFS(int s) traverses vertices reachable from s.
3+
import java.io.*;
4+
import java.util.*;
5+
6+
class BFS
7+
{
8+
private int V; // No. of vertices
9+
private LinkedList<Integer> adjacent[]; //Adjacency Lists
10+
11+
// Constructor
12+
BFS(int v)
13+
{
14+
V = v;
15+
adjacent = new LinkedList[v];
16+
for (int i=0; i<v; ++i)
17+
adjacent[i] = new LinkedList();
18+
}
19+
20+
// Function to add an edge into the graph
21+
void addEdge(int v,int w)
22+
{
23+
adjacent[v].add(w);
24+
}
25+
26+
// prints BFS traversal from a given source s
27+
void BFS(int s)
28+
{
29+
// Mark all the vertices as not visited
30+
// False
31+
boolean visited[] = new boolean[V];
32+
33+
// Create a queue for BFS
34+
LinkedList<Integer> queue = new LinkedList<Integer>();
35+
36+
// Mark the current node as visited and enqueue it
37+
visited[s]=true;
38+
queue.add(s);
39+
40+
while (queue.size() != 0)
41+
{
42+
// Dequeue a vertex from queue and print it
43+
s = queue.poll();
44+
System.out.print(s+" ");
45+
46+
// Get all adjacent vertices of the dequeued vertex s
47+
// If a adjacent has not been visited, then mark it
48+
// visited and enqueue it
49+
Iterator<Integer> i = adjacent[s].listIterator();
50+
while (i.hasNext())
51+
{
52+
int n = i.next();
53+
if (!visited[n])
54+
{
55+
visited[n] = true;
56+
queue.add(n);
57+
}
58+
}
59+
}
60+
}
61+
62+
// Driver code to check implementation
63+
public static void main(String args[])
64+
{
65+
BFS bfs = new BFS(4);
66+
67+
bfs.addEdge(0, 1);
68+
bfs.addEdge(0, 2);
69+
bfs.addEdge(1, 2);
70+
bfs.addEdge(2, 0);
71+
bfs.addEdge(2, 3);
72+
bfs.addEdge(3, 3);
73+
74+
System.out.println("Breadth First Traversal "+
75+
"(starting from vertex 2)");
76+
77+
bfs.BFS(2);
78+
}
79+
}

java/searching/BinarySearch.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Code for Binary Search in Java
2+
// Assume array is sorted
3+
// Array is divided into two parts
4+
// Based on the position of mid
5+
// If element is found returns the index
6+
// Else returns -1
7+
class BinarySearch{
8+
int bSearch(int arr[], int left, int right, int element)
9+
{
10+
if (right >= left) {
11+
int mid = left + (right - left) / 2;
12+
13+
// If the element is present at mid
14+
if (arr[mid] == element)
15+
return mid;
16+
17+
// If element is smaller than mid
18+
// search in left subarray
19+
if (arr[mid] > element)
20+
return bSearch(arr, left, mid - 1, element);
21+
22+
// If element is greater than mid
23+
// search in right subarray
24+
return bSearch(arr, mid + 1, right, element);
25+
}
26+
27+
// If not found, return -1
28+
return -1;
29+
}
30+
31+
// Driver code to test
32+
public static void main(String args[])
33+
{
34+
BinarySearch elt = new BinarySearch();
35+
int arr[] = { 5, 10, 70, 100, 499 };
36+
int size = arr.length;
37+
int x = 5;
38+
int result = elt.bSearch(arr, 0, size - 1, x);
39+
if (result == -1)
40+
System.out.println("Element not present");
41+
else
42+
System.out.println("Element found at index " + result);
43+
}
44+
}

java/searching/LinearSearch.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Code for linearly searching x in arr[].
2+
//If found returns the location.
3+
//If not found returns -1.
4+
public class LinearSearch {
5+
public static int search(int arr[], int x) {
6+
int size = arr.length;
7+
for (int i = 0; i < size; i++) {
8+
if (arr[i] == x)
9+
return i;
10+
}
11+
return -1;
12+
}
13+
14+
// Driver code
15+
public static void main(String args[]) {
16+
int arr[] = {11, 41, 47, 108, 490};
17+
int x = 14;
18+
19+
// Function call
20+
int result = search(arr, x);
21+
if (result == -1)
22+
System.out.print("Element is not present in array");
23+
else
24+
System.out.print("Element is present at index " + result);
25+
}
26+
}

0 commit comments

Comments
 (0)