Skip to content

Commit 9debc93

Browse files
author
Deepak Malik
committed
Algorithms in Java - Initial Commit
1 parent 5cf9273 commit 9debc93

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Algorithms-in-Java
3+
* BinarySearch.java
4+
*/
5+
package com.deepak.algorithms.Searching;
6+
7+
/**
8+
* Class for BinarySearch implementation
9+
* @author Deepak Malik
10+
*/
11+
public class BinarySearch {
12+
13+
/**
14+
* Main method to start the flow of program
15+
* @param args
16+
*/
17+
public static void main(String[] args) {
18+
int[] sortedValues = {7, 10, 10, 20, 25, 32, 40, 46, 47, 49, 54, 55, 61, 63, 65, 83, 84, 93};
19+
int valueToBeSearched = 84;
20+
System.out.println("******************* BINARY SEARCH *******************");
21+
performBinarySearch(sortedValues, valueToBeSearched);
22+
}
23+
24+
/**
25+
* <p>Binary Search
26+
* Few points to note here,
27+
* 1. Binary search works only on sorted collection
28+
* 2. Ex. Looking up a name in Telephone directory
29+
* 3. Each time we make a comparison, we eliminate half of the list </p>
30+
*
31+
* <p> Algorithm :
32+
* 1. Find the mid index in the list and compare it with the search term
33+
* 2. If search term is smaller then the element at mid index, eliminate upper half, else eliminate lower half
34+
* 3. If it is equal, break the loop as we have found our element.
35+
* 4. Keep running the loop until high >= low.
36+
* 5. return index of matching item, or -1 if not found
37+
* </p>
38+
*
39+
* <p>Complexity :
40+
* 1. Best Case : What is the fewer number of iterations to find the item?
41+
* => Best case is when search term is at the first try
42+
* => Number of comparisons in this case is 1
43+
*
44+
* 2. Worst Case : What is the most number of comparisons needed to find the item?
45+
* => Worst case is when search term is not at all in the array
46+
* => If our array is of size N, we need N comparisons for worst case
47+
*
48+
* 3. Average Case : On an Average, how many comparisons are needed to find the element in the array?
49+
* => On an average, search term is anywhere in the list
50+
* </p>
51+
*/
52+
private static void performBinarySearch(int[] iListOfValues, int iValueToBeSearched) {
53+
int index = -1;
54+
int low = 0;
55+
int mid;
56+
int high = iListOfValues.length - 1;
57+
while (high >= low) {
58+
mid = (high + low)/2;
59+
if (iValueToBeSearched < iListOfValues[mid]) {
60+
high = mid - 1;
61+
} else if (iValueToBeSearched > iListOfValues[mid]) {
62+
low = mid + 1;
63+
} else {
64+
index = mid;
65+
break;
66+
}
67+
}
68+
if (index != -1) {
69+
System.out.println("Element found in the list at position = " + index);
70+
} else {
71+
System.out.println("Element not found in the list");
72+
}
73+
}
74+
75+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Algorithms-in-Java
3+
* LinearSearch.java
4+
*/
5+
package com.deepak.algorithms.Searching;
6+
7+
/**
8+
* Class for LinearSearch implementation
9+
* @author Deepak Malik
10+
*/
11+
public class LinearSearch {
12+
13+
/**
14+
* Main method to start the flow of program
15+
* @param args
16+
*/
17+
public static void main(String[] args) {
18+
int[] nonSortedValues = {7, 10, 47, 40, 83, 84, 65, 61, 32, 55, 49, 46, 25, 20, 93, 63, 54, 10};
19+
int iValueToBeSearched = 61;
20+
System.out.println("******************* LINEAR SEARCH *******************");
21+
performLinearSearch(nonSortedValues, iValueToBeSearched);
22+
}
23+
24+
/**
25+
* <p>Linear Search is also called Sequential Search
26+
* Few point to note here,
27+
* 1. It can be really slow with large number of elemets in the collection
28+
* ex. If we have 1000000 items in the list, on an average it will take 500000 number of comparisons
29+
* 2. To be used when we are not sure about the sort order of the list. </p>
30+
*
31+
* <p>Algorithm :
32+
* 1. Loop through each item in the list
33+
* 2. Compare search term to the current item in the list
34+
* 3. If matches, save index of the matching item and break
35+
* 4. return index of matching item, or -1 if not found
36+
* </p>
37+
*
38+
* <p>Complexity :
39+
* 1. Best Case : What is the fewer number of iterations to find the item?
40+
* => Best case is when search term is at the first slot of the array
41+
* => Number of comparisons in this case is 1
42+
*
43+
* 2. Worst Case : What is the most number of comparisons needed to find the item?
44+
* => Worst case is when search term is at the last slot of the array or not at all in the array
45+
* => If our array is of size N, we need N comparisons for worst case
46+
*
47+
* 3. Average Case : On an Average, how many comparisons are needed to find the element in the array?
48+
* => On an average, search term will be in the middle of the array
49+
* => For an array of size N, number of comparisons will be N/2
50+
* </p>
51+
*/
52+
private static void performLinearSearch(int[] iListOfValues, int iValueToBeSearched) {
53+
int index = -1;
54+
for (int i=0; i < iListOfValues.length; i++) {
55+
if (iListOfValues[i] == iValueToBeSearched) {
56+
index = i;
57+
break;
58+
}
59+
}
60+
if (index == -1) {
61+
System.out.println("Element not found in the collection");
62+
} else {
63+
System.out.println("Element found in the collection at index = " + index);
64+
}
65+
}
66+
67+
}

0 commit comments

Comments
 (0)