Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions Java/Dynamic_Programming/LongestCommonSubsequence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package Java.Dynamic_Programming;
import java.util.*;
public class LongestCommonSubsequence {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

String a = scan.next();
String b = scan.next();

int m = a.length();
int n = b.length();

LongestCommonSubsequence lcs = new LongestCommonSubsequence();
lcs.getLCS(a,b,m,n);

scan.close();
}
private void getLCS(String a, String b, int m, int n) {
int dp[][] = new int[m+1][n+1];
for(int i=0;i<=m;i++){
for(int j=0;j<=n;j++){
if(i==0 || j==0){
dp[i][j] = 0;
}
else if(a.charAt(i-1) == b.charAt(j-1)){
dp[i][j] = 1 + dp[i-1][j-1];
}
else{
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
}
}
}
int len = dp[m][n];

String res = "";
int i = m;
int j = n;
while (i > 0 && j > 0) {
if (a.charAt(i - 1) == b.charAt(j - 1)) {
res += Character.toString(a.charAt(i - 1));
i--;
j--;
}

else if (dp[i - 1][j] > dp[i][j - 1])
i--;
else
j--;
}
System.out.println("Length of lCS is : "+ len);
System.out.println("LCS string is : "+ new StringBuilder(res).reverse().toString());
}
}
52 changes: 52 additions & 0 deletions Java/Dynamic_Programming/LongestPalindromicSubsequence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package Java.Dynamic_Programming;
import java.util.*;
public class LongestPalindromicSubsequence {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

String a = scan.next();
int m = a.length();

LongestPalindromicSubsequence lps = new LongestPalindromicSubsequence();
lps.getLPS(a,m);

scan.close();
}
private void getLPS(String a,int m) {
String b = new StringBuilder(a).reverse().toString();

int dp[][] = new int[m+1][m+1];
for(int i=0;i<=m;i++){
for(int j=0;j<=m;j++){
if(i==0 || j==0){
dp[i][j] = 0;
}
else if(a.charAt(i-1) == b.charAt(j-1)){
dp[i][j] = 1 + dp[i-1][j-1];
}
else{
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
}
}
}
int len = dp[m][m];

String res = "";
int i = m;
int j = m;
while (i > 0 && j > 0) {
if (a.charAt(i - 1) == b.charAt(j - 1)) {
res += Character.toString(a.charAt(i - 1));
i--;
j--;
}

else if (dp[i - 1][j] > dp[i][j - 1])
i--;
else
j--;
}
System.out.println("Length of LPS is : "+ len);
System.out.println("LPS string is : "+ new StringBuilder(res).reverse().toString());
}
}
78 changes: 78 additions & 0 deletions Java/Sorting/QuickSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
*
* 1. Quick Sort is an inplace sorting algorithm based on Divide and Conquer Approach.
* 2. First a pivot element is choosen and partitions are made around the choosen pivot.
* 3. Selection of pivot element decides the time complexity of algorithm.
*
*/

package Java.Sorting;

import java.util.*;
public class QuickSort {
static void swap(int[] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

static int partition(int[] arr, int low, int high)
{
int pivot = arr[high];
int i = (low - 1);

for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, high);
return (i + 1);
}

static void quickSort(int[] arr, int low, int high)
{
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

static void printArray(int[] arr, int size)
{
for (int i = 0; i < size; i++)
System.out.print(arr[i] + " ");

System.out.println();
}

public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();

int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[i] = scan.nextInt();
}

System.out.println("Array before sorting");
printArray(arr, n);
System.out.println();

quickSort(arr, 0, n - 1);

System.out.println("Array after sorting ");
printArray(arr, n);
System.out.println();

scan.close();
}
}


// Contributor : Rishabh Srivastava
// Github Link : https://github.com/RishabhSrivastava1423/