Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.

Commit 75aed97

Browse files
authored
Merge branch 'master' into my-branch
2 parents 90b7ba7 + b3a5b7f commit 75aed97

File tree

10 files changed

+464
-33
lines changed

10 files changed

+464
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
- [Greedy Algorithms](greedy-algorithms)
2222
- [Graphs](graphs)
2323
- [Math](math)
24-
- [Neutral Network](neutral-network)
24+
- [Neural Network](neural-network)
2525
- [Ciphers](ciphers)
2626
- [Data Structures](data-structures)
2727
- [Dynamic Programming](dynamic-programming)

data-structures/Stack.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
public class Stack {
2+
private int maxSize;
3+
private long[] stackArray;
4+
private int top;
5+
6+
public Stack(int s) {
7+
maxSize = s;
8+
stackArray = new long[maxSize];
9+
top = -1;
10+
}
11+
12+
public void push(long j) {
13+
stackArray[++top] = j;
14+
}
15+
16+
public long pop() {
17+
return stackArray[top--];
18+
}
19+
20+
public long peek() {
21+
return stackArray[top];
22+
}
23+
24+
public boolean isEmpty() {
25+
return (top == -1);
26+
}
27+
28+
public boolean isFull() {
29+
return (top == maxSize - 1);
30+
}
31+
32+
public static void main(String[] args) {
33+
Stack theStack = new Stack(10);
34+
theStack.push(10);
35+
theStack.push(20);
36+
theStack.push(30);
37+
theStack.push(40);
38+
theStack.push(50);
39+
40+
while (!theStack.isEmpty()) {
41+
long value = theStack.pop();
42+
System.out.print(value);
43+
System.out.print(" ");
44+
}
45+
System.out.println("");
46+
}
47+
}

data-structures/UseStack.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Stack
2+
{
3+
int stcksize=100;
4+
int stck[] = new int[stcksize];
5+
int tos;
6+
Stack()
7+
{
8+
tos=-1;
9+
}
10+
void push(int item)
11+
{
12+
if(tos==stcksize-1)
13+
System.out.println("Stack Overflow");
14+
else
15+
stck[++tos]=item;
16+
}
17+
int pop()
18+
{
19+
if(tos<0)
20+
{
21+
System.out.println("Stack Underflow.");
22+
return 0;
23+
}
24+
else
25+
return stck[tos--];
26+
}
27+
}
28+
public class UseStack
29+
{
30+
public static void main(String args[])
31+
{
32+
Stack s=new Stack();
33+
s.push(1);
34+
s.push(2);
35+
s.push(3);
36+
s.pop(); // return 3 as it was the last inserted element
37+
}
38+
}

dynamic-programming/Kadane.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package AlgorithmsAndDS;
2+
3+
public class Kadane {
4+
5+
public static void main(String[] args) {
6+
int[] arr= {2,3,-6,1,2,3,-4,5};
7+
System.out.println(maximumSubArraySum(arr));
8+
//System.out.println(kadaneSum(arr));
9+
}
10+
public static int maximumSubArraySum(int[] arr) {
11+
int osum=arr[0];
12+
int csum=arr[0];
13+
for(int i=1;i<arr.length;i++) {
14+
if(csum+arr[i]<arr[i]) {
15+
csum=arr[i];
16+
}
17+
else {
18+
csum+=arr[i];
19+
}
20+
if(csum>osum) {
21+
osum=csum;
22+
}
23+
}
24+
return osum;
25+
}
26+
27+
28+
29+
30+
public static int kadaneSum(int[] arr) {
31+
int osum=arr[0];
32+
int csum=arr[0];
33+
for(int i=1;i<arr.length;i++) {
34+
if(csum+arr[i]<arr[i]) {
35+
csum=arr[i];
36+
}
37+
else
38+
{
39+
csum+=arr[i];
40+
}
41+
if(csum>osum) {
42+
osum=csum;
43+
}
44+
}
45+
46+
return osum;
47+
}
48+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Finding Longest Common Subsequence of Two Strings Using Dynamic Programming
2+
import java.util.Scanner;
3+
public class LongestCommonSubsequence
4+
{
5+
int lcs(char[] X, char[] Y, int m, int n)
6+
{
7+
int DP[][] = new int[m+1][n+1];
8+
// Bottom-Up Approach for dp
9+
for(int i=0;i<=m;i++)
10+
{
11+
for(int j=0;j<=n;j++)
12+
{
13+
if(i==0 || j==0)
14+
DP[i][j]=0;
15+
else if(X[i-1]==Y[j-1])
16+
DP[i][j]=DP[i-1][j-1]+1;
17+
else
18+
DP[i][j] = max(DP[i-1][j], DP[i][j-1]);
19+
}
20+
}
21+
return DP[m][n];
22+
}
23+
int max(int a, int b)
24+
{
25+
if(a>b)
26+
return a;
27+
else
28+
return b;
29+
}
30+
public static void main(String[] args)
31+
{
32+
LongestCommonSubsequence obj=new LongestCommonSubsequence();
33+
String s1="",s2="";
34+
Scanner scan=new Scanner(System.in);
35+
System.out.println("Enter 1st String");
36+
s1=scan.next();
37+
System.out.println("Enter 2nd String");
38+
s2=scan.next();
39+
char[] X=s1.toCharArray();
40+
char[] Y=s2.toCharArray();
41+
int m=X.length;
42+
int n=Y.length;
43+
System.out.println("Length of Longest Common Subsequence is: "+obj.lcs(X,Y,m,n));
44+
}
45+
}

dynamic-programming/fibonacci.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
// Fibonacci Series using Dynamic Programming
3+
class fibonacci
4+
{
5+
static int fib(int n)
6+
{
7+
/* Declare an array to store Fibonacci numbers. */
8+
int f[] = new int[n+2]; // 1 extra to handle case, n = 0
9+
int i;
10+
11+
/* 0th and 1st number of the series are 0 and 1*/
12+
f[0] = 0;
13+
f[1] = 1;
14+
15+
for (i = 2; i <= n; i++)
16+
{
17+
/* Add the previous 2 numbers in the series
18+
and store it */
19+
f[i] = f[i-1] + f[i-2];
20+
}
21+
22+
return f[n];
23+
}
24+
25+
public static void main (String args[])
26+
{
27+
int n = 9;
28+
System.out.println(fib(n));
29+
}
30+
}
31+
/* This code is contributed by mokshagna517 */

graph/BinaryTree.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
7+
/**
8+
*
9+
* @author miqdad
10+
*/
11+
class Node {
12+
13+
Node left, right;
14+
int value;
15+
16+
public Node(int value) {
17+
this.value = value;
18+
}
19+
20+
public Node search(int value) {
21+
if (this.value == value) {
22+
return this;
23+
} else if (value < this.value && this.left != null) {
24+
this.left.search(value);
25+
} else if (value > this.value && this.right != null) {
26+
this.right.search(value);
27+
}
28+
29+
return null;
30+
}
31+
32+
public void visit() {
33+
if (this.left != null) {
34+
this.left.visit();
35+
}
36+
System.out.print(this.value + " ");
37+
if (this.right != null) {
38+
this.right.visit();
39+
}
40+
}
41+
42+
public void addNode(Node node) {
43+
if (node.value < this.value) {
44+
if (this.left == null) {
45+
this.left = node;
46+
} else {
47+
this.left.addNode(node);
48+
}
49+
} else {
50+
if (this.right == null) {
51+
this.right = node;
52+
} else {
53+
this.right.addNode(node);
54+
}
55+
}
56+
}
57+
}
58+
59+
class Tree {
60+
61+
Node root;
62+
63+
public void addValue(int value) {
64+
Node node = new Node(value);
65+
if (this.root == null) {
66+
this.root = node;
67+
} else {
68+
this.root.addNode(node);
69+
}
70+
}
71+
72+
public void binaryTreeSort() {
73+
this.root.visit();
74+
}
75+
}
76+
77+
public class BinaryTree {
78+
79+
public static void main(String[] args) {
80+
int[] nums = {3, 2, 5, 6, 7, 1, 9, 0};
81+
Tree tree = new Tree();
82+
for (int n : nums) {
83+
tree.addValue(n);
84+
}
85+
86+
tree.binaryTreeSort();
87+
System.out.println("");
88+
}
89+
}

math/MatrixMultiply.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
public class MatrixMultiply {
3+
4+
/**
5+
* Multiples 2 2-dimensional matrices together given their dimensions are compatible
6+
* @param mat_a A 2-Dimensional matrix to be multiplied
7+
* @param mat_b A 2-Dimensional matrix to be multiplied
8+
* @return The product of the two matrices if compatible dimensions, null otherwise
9+
*/
10+
public static double[][] matrixMultiply2D(double[][] mat_a, double[][] mat_b)
11+
{
12+
int aRows = mat_a.length;
13+
int aColumns = mat_a[0].length;
14+
int bRows = mat_b.length;
15+
int bColumns = mat_b[0].length;
16+
17+
if (aColumns != bRows)
18+
throw new IllegalArgumentException("A:Rows: " + aColumns + " did not match B:Columns " + bRows + ".");
19+
20+
double[][] mat_result = new double[aRows][bColumns];
21+
for (int i = 0; i < aRows; i++) {
22+
for (int j = 0; j < bColumns; j++) {
23+
mat_result[i][j] = 0.00000;
24+
}
25+
}
26+
27+
for (int i = 0; i < aRows; i++) { // aRow
28+
for (int j = 0; j < bColumns; j++) { // bColumn
29+
for (int k = 0; k < aColumns; k++) { // aColumn
30+
mat_result[i][j] += mat_a[i][k] * mat_b[k][j];
31+
}
32+
}
33+
}
34+
return mat_result;
35+
}
36+
37+
38+
}
39+
40+

sorting/CountingSort.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
7+
/**
8+
*
9+
* @author miqdad
10+
*/
11+
public class CountingSort {
12+
13+
static int[] countingSort(int[] nums, int maxNumber) {
14+
int[] temp = new int[maxNumber + 1];
15+
int[] result = new int[nums.length];
16+
for (int i = 0; i < nums.length; i++) {
17+
temp[nums[i]]++;
18+
}
19+
20+
int index = 0;
21+
for(int i=0; i<temp.length; i++){
22+
while(temp[i] > 0){
23+
result[index++] = i;
24+
temp[i]--;
25+
}
26+
}
27+
28+
return result;
29+
}
30+
31+
static void printArray(int[] nums) {
32+
for (int num : nums) {
33+
System.out.println(num);
34+
}
35+
}
36+
37+
public static void main(String[] args) {
38+
int[] nums = {3, 2, 5, 6, 7, 1, 9, 0, 8, 6};
39+
nums = countingSort(nums, 10);
40+
printArray(nums);
41+
}
42+
}

0 commit comments

Comments
 (0)