-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjava Algorithms.txt
143 lines (129 loc) · 3.52 KB
/
java Algorithms.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
Reverse the given Array without using built in function
public class ReverseArray {
static int [] a;
public static void reverseIteration(){
int start =0;
int end = a.length-1;
while(start<=end){
int temp = a[start];
a[start] = a[end];
a[end] = temp;
start++;
end--;
}
}
public static void reverseRecursive(int start, int end){
if(start<=end){
int temp = a[start];
a[start] = a[end];
a[end] = temp;
start++;
end--;
reverseRecursive(start, end);
}
}
public static void main(String[] args) {
a = new int []{1,2,3,4,5};
System.out.println("Original Array" + Arrays.toString(a));
reverseIteration();
System.out.println("Reversed - Array(Iteration):" + Arrays.toString(a));
reverseRecursive(0,a.length-1);
System.out.println("Reversed Again - Array(Recursion):" + Arrays.toString(a));
}
}
Print All Elements of Two Dimensional Array in Spiral:
public class Print2DArrayInSpiral {
public int arrA[][] = { { 1, 2, 3, 4, 5 }, { 18, 19, 20, 21, 6 },
{ 17, 28, 29, 22, 7 }, { 16, 27, 30, 23, 8 },
{ 15, 26, 25, 24, 9 }, { 14, 13, 12, 11, 10 } };
public int printSpiral(int row_S, int row_E, int col_S, int col_E,
boolean reverse, boolean rowPrint) {
if (row_S > row_E && col_S > col_E) {
return 1;
}
if (rowPrint == false) {
if (reverse == false) {
for (int i = col_S; i <= col_E; i++) {
System.out.print(" " + arrA[row_S][i]);
}
}
row_S++;
rowPrint = true;
reverse = false;
}
if (rowPrint == true) {
if (reverse == false) {
for (int i = row_S; i <= row_E; i++) {
System.out.print(" " + arrA[i][col_E]);
}
}
col_E--;
rowPrint = false;
reverse = true;
}
if (rowPrint == false) {
if (reverse == true) {
for (int i = col_E; i >= col_S; i--) {
System.out.print(" " + arrA[row_E][i]);
}
}
row_E--;
rowPrint = true;
reverse = true;
}
if (rowPrint == true) {
if (reverse == true) {
for (int i = row_E; i >= row_S; i--) {
System.out.print(" " + arrA[i][col_S]);
}
}
col_S++;
rowPrint = false;
reverse = false;
}
printSpiral(row_S, row_E, col_S, col_E, reverse, rowPrint);
return 0;
}
public static void main(String args[]) {
Print2DArrayInSpiral p = new Print2DArrayInSpiral();
p.printSpiral(0, 5, 0, 4, false, false);
}
}
Longest Common Substring
public class LongestCommonSubString {
public static int find(char [] A, char [] B){
int [][]LCS = new int [A.length+1][B.length+1];
// if A is null then LCS of A, B =0
for(int i=0;i<=B.length;i++){
LCS[0][i]=0;
}
// if B is null then LCS of A, B =0
for(int i=0;i<=A.length;i++){
LCS[i][0]=0;
}
//fill the rest of the matrix
for(int i=1;i<=A.length;i++){
for(int j=1;j<=B.length;j++){
if(A[i-1]==B[j-1]){
LCS[i][j] = LCS[i-1][j-1] +1;
}else{
LCS[i][j] = 0;
}
}
}
int result = -1;
for(int i=0;i<=A.length;i++){
for(int j=0;j<=B.length;j++){
if(result<LCS[i][j]){
result = LCS[i][j];
}
}
}
return result;
}
public static void main(String[] args) {
String A = "tutorialhorizon";
String B = "dynamictutorialProgramming";
System.out.println("LCS length : " + find(A.toCharArray(), B.toCharArray()));
}
}