Skip to content

Commit

Permalink
adding more tests samples
Browse files Browse the repository at this point in the history
  • Loading branch information
aisabel committed Nov 8, 2023
1 parent 105c6a8 commit 1a4503a
Show file tree
Hide file tree
Showing 78 changed files with 980 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions src/main/java/CommonDataStructuresAndAlgorithms/BinarySearch.java
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;
}
}
18 changes: 18 additions & 0 deletions src/main/java/CommonDataStructuresAndAlgorithms/LinearSearch.java
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+",");
}
}
}
26 changes: 26 additions & 0 deletions src/main/java/CommonDataStructuresAndAlgorithms/Recursion.java
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
}
}

Loading

0 comments on commit 1a4503a

Please sign in to comment.