-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package CommonDataStructuresAndAlgorithms; | ||
|
||
public class BinarySearch { | ||
/*Binary Search starts from the middle point in the array and then evaluates if target | ||
meets conditions from there decrement or increment the search until find the number | ||
*/ | ||
public static void main(String[] args) { | ||
int[] arr = {1, 3, 5, 7, 9, 11, 13, 15, 19, 24, 25}; | ||
int target = 7; | ||
int index = binarySearch(arr, target); | ||
if (index != -1) { | ||
System.out.println("element found at index:" + index); | ||
} else { | ||
System.out.println("Element not found in the array"); | ||
} | ||
} | ||
|
||
public static int binarySearch(int[] arr, int target) { | ||
int low = 0; | ||
int high = arr.length - 1; | ||
|
||
while (low <= high) { | ||
int mid = low + (high - low) / 2; | ||
|
||
if (arr[mid] == target) { | ||
return mid; | ||
} else if (arr[mid] < target) { | ||
low = mid + 1; | ||
} else { | ||
high = mid - 1; | ||
} | ||
} | ||
return -1; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package CommonDataStructuresAndAlgorithms; | ||
|
||
public class LinearSearch { | ||
//Linear search directly in order until it finds the element | ||
|
||
public static void main(String[] args) { | ||
int [] numbers = new int[]{4,7,1,9,8,3,5,0}; | ||
|
||
//count through numbers | ||
for(int num: numbers){ | ||
if(num == 8){ | ||
System.out.println("->"+num); | ||
break; | ||
} | ||
System.out.println(num+","); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package CommonDataStructuresAndAlgorithms; | ||
|
||
public class Recursion { | ||
/* | ||
5 | ||
5+4 | ||
4+3 | ||
3+2 | ||
2+1 | ||
1 | ||
=15 | ||
*/ | ||
public static int sum(int num){ | ||
if(num ==1){ | ||
return 1; | ||
} else { | ||
return num+sum(num-1); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
int number =10; | ||
int result = sum(number); | ||
System.out.println("Sum of numbers from 1 to "+number+" is "+result); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package CommonDataStructuresAndAlgorithms; | ||
|
||
public class RecursionArrayExample { | ||
/* | ||
Recursion: The process in which a function calls itself directly or indirectly | ||
is called recursion and the corresponding function is called a recursive function. | ||
A recursive function solves a particular problem by calling a copy of itself | ||
and solving smaller sub problems of the original problems. | ||
Explanation: Given int[] arr = {1,2,7,4,8}; | ||
8 + SumOfDigits(1,2,7,4) | ||
4 + SumOfDigits(1,2,7) | ||
7 + SumOfDigits(1,2) | ||
2 + SumOfDigits(1) | ||
*Then 1 therefore is the smaller solution and smaller result. | ||
The algorithmic steps for implementing recursion in a function are as follows: | ||
Step1 - Define a base case: Identify the simplest case for which the solution is known or trivial. | ||
This is the stopping condition for the recursion, as it prevents the function from infinitely calling itself. | ||
Step2 - Define a recursive case: Define the problem in terms of smaller sub problems. | ||
Break the problem down into smaller versions of itself, and call the function recursively to solve each sub problem. | ||
Step3 - Ensure the recursion terminates: Make sure that the recursive function eventually reaches the base case, | ||
and does not enter an infinite loop. | ||
Step4 - Combine the solutions: Combine the solutions of the sub problems to solve the original problem. | ||
*/ | ||
static int[] arr = {1,2,7,4,8}; | ||
public static void main(String[] args) { | ||
|
||
int value = SumOfDigits(arr.length-1);//get all including the zero index | ||
System.out.println("Sum of all elements is "+value);//Prints sum of all values = 22 | ||
} | ||
|
||
private static int SumOfDigits(int n){ | ||
if(n==0) //Base case(the small version of the problem) = zero index we have value= 1 | ||
return arr[n]; //returns = 1 value, ensuring recursion terminates. | ||
|
||
//When method calls itself several times, its called recursion. | ||
return arr[n] + SumOfDigits(n-1); //Recursive case | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package CommonDataStructuresAndAlgorithms; | ||
|
||
public class RecursionFactorialExample { | ||
/* | ||
Finding Factorial is an example of recursion | ||
1! = 1 | ||
2! = 2x1 | ||
3! = 3x2x1 | ||
4! = 4x3x2x1 | ||
5! = 5x4x3x2x1 = 120 | ||
5 = (4x3x2x1) = 4! = 24 | ||
4 x3! = 6 | ||
3 x2! = 2 | ||
2 x 1! = 1 | ||
*/ | ||
|
||
public static void main(String[] args) { | ||
|
||
int fact_value =Factorial(5); | ||
System.out.println("Factorial of all elements is "+fact_value);//120 | ||
|
||
} | ||
|
||
private static int Factorial(int n){ | ||
if(n==1) { //Base case(the small version of the problem) = zero index we have value= 1 | ||
return 1; //returns = 1 value, ensuring recursion terminates. | ||
} | ||
//When method calls itself several times, its called recursion. | ||
return n * Factorial(n-1); //Recursive case | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package CommonDataStructuresAndAlgorithms; | ||
|
||
public class RecursionFibonacci { | ||
/* | ||
The Fibonacci sequence is a set of steadily increasing numbers | ||
where each number is equal to the sum of the preceding two numbers. | ||
Example: 0,1,1,2,3,5,8,13,21 | ||
*/ | ||
|
||
public static void main(String[] args) { | ||
int fib_value = Fibonacci(8); | ||
System.out.println("Fibbonacci "+fib_value);//Prints resulting number | ||
} | ||
|
||
private static int Fibonacci(int n){ | ||
if(n==0 || n==1) //Base case(the small version of the problem) = zero index we have value= 1 | ||
return n; // ensuring recursion terminates. | ||
|
||
//When method calls itself several times, its called recursion. | ||
return Fibonacci(n-1) + Fibonacci(n-2); //Recursive case | ||
} | ||
} | ||
|