Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting #7

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Binary file modified out/production/RecursionLearn/com/learning/java/Main.class
Binary file not shown.
84 changes: 14 additions & 70 deletions src/com/learning/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,76 +6,20 @@ public class Main

public static void main(String[] args) {

// reverse the integer number

//int result = Recursion.rev(123, 0);
//System.out.println(result);

//convert digits to words
//Recursion.digitToWords(987654321);

// find power of numbers
/*int num = 4;
int pow = 3;
int res = Recursion.pow(num,pow);
System.out.printf("The power of %s to %s is %s ", num, pow, res);*/

// find the arithmetic sum from 1 to N
/*int result = Recursion.arithmeticSum(25,1);
System.out.print(result);*/

//System.out.println(15 / 18);
/*int gcd = Recursion.euclidGCD(18,12);
System.out.println(gcd);*/

// convert decimal number to binary number
/*int decimalToBinary = Recursion.decimalToBinary(25);
System.out.println(decimalToBinary);*/

int[] arr = { 1, 9, 2, 32, 8, 76, 34, 12};
/*ArrayProblems arrayProblems = new ArrayProblems();
int result = arrayProblems.peakEl(arr);
System.out.print("Peak element index is: "+result);*/

// finding max and min element in an array
/*int[] response = ArrayProblems.findMaxMinEl(arr);
System.out.println("Minimum element is: "+response[0]);
System.out.println("Maximum element is: "+response[1]);*/

// Reverse an array
/* System.out.println("The reverse array is below");

// i. Iteration
*//*int[] response = ArrayProblems.reverseArray(arr);*//*

// ii. Recursion
int[] response = ArrayProblems.reverseArrayUsingRecursion(arr,0,arr.length -1);

for (int j : response) {
System.out.println(j);
}*/

// sort an array using merge sort

/*ArrayProblems.sortMerge(arr, 0, arr.length-1);
for(int i : arr){
System.out.println(i);
}*/

// sort the 0s 1s 2s
/*int[] arr012 = {1,2,1,2,0,2,2,1,0};
ArrayProblems.sort012(arr012,9);
for(int i : arr012) {
System.out.println(i);
}*/
/*int[] newAr = {1,-1,3,2,0,-7,-5,11,6};
int[] result = ArrayProblems.seperateNumbers(newAr,8);
for (int i=0; i < result.length; i++){
System.out.println(result[i]);
}*/
int[] first ={1,2,2,2,3,3,3,3,3,3,4,4,5,6,6,7,7,8};
int[] second ={3,3,3,3,4,4,5,6,6,7,7,8,9,9,9,9,9,9,10,12,24,25,25};
ArrayProblems.doUnion(first, first.length, second, second.length);
int[] arr = {5,2,4,6,7,1,3,2,4,9};
// ascending order insertion sort
// Sorting.insertionSort(arr);

// descending order insertion sort
// Sorting.insertionSortDescending(arr);
// for (int i=0; i< arr.length; i++) {
// System.out.print(arr[i]+" ");
// }
// Sorting using quick sort algorithm
Sorting.quickSort(arr,0, arr.length-1);
for (int i=0; i< arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}

61 changes: 61 additions & 0 deletions src/com/learning/java/PermuationInString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.learning.java;
/*Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise.

In other words, return true if one of s1's permutations is the substring of s2.
*/
public class PermuationInString {
static boolean flag = false;
public static boolean checkInclusion(String s1, String s2) {
PermuationInString.permute(s1, s2, 0);
return flag;
}
public static String swap(String s, int i0, int i1) {
if (i0 == i1)
return s;
String s1 = s.substring(0, i0);
String s2 = s.substring(i0 + 1, i1);
String s3 = s.substring(i1 + 1);
return s1 + s.charAt(i1) + s2 + s.charAt(i0) + s3;
}

static void permute(String s1, String s2, int l) {
if (l == s1.length()) {
if (s2.contains(s1))
flag = true;
} else {
for (int i = l; i < s1.length(); i++) {
s1 = swap(s1, l, i);
permute(s1, s2, l + 1);
s1 = swap(s1, l, i);
}
}
}

public static boolean checkInclusionOne(String s1, String s2) {
if (s1.length() > s2.length())
return false;
int[] s1map = new int[26];
int[] s2map = new int[26];
for (int i = 0; i < s1.length(); i++) {
int a = s1.charAt(i) - 'a';
int b = s2.charAt(i) - 'a';
s1map[a]++;
s2map[b]++;
}
for (int i = 0; i < s2.length() - s1.length(); i++) {
if (matches(s1map, s2map))
return true;
s2map[s2.charAt(i + s1.length()) - 'a']++;
s2map[s2.charAt(i) - 'a']--;
}
return matches(s1map, s2map);
}

public static boolean matches(int[] s1map, int[] s2map) {
for (int i = 0; i < 26; i++) {
if (s1map[i] != s2map[i])
return false;
}
return true;
}
}
21 changes: 21 additions & 0 deletions src/com/learning/java/SearchDFSBFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.learning.java;

public class SearchDFSBFS {
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
int color = image[sr][sc];
if (color != newColor){
dfs (image, sr, sc, color, newColor);
}
return image;
}

public void dfs (int[][] image, int r, int c, int color, int newColor) {
if (image[r][c] == color) {
image[r][c] = newColor;
if (r >=1) dfs(image, r-1, c, color, newColor); // up
if (c >=1) dfs(image, r , c-1, color, newColor); //left
if (r+1 < image.length) dfs(image, r+1, c, color, newColor); // down
if (c+1 < image[0].length) dfs(image, r, c+1, color, newColor); //right
}
}
}
40 changes: 40 additions & 0 deletions src/com/learning/java/SlidingWindowProblems.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.learning.java;

import java.util.*;
class SlidingWindowProblems {
public static int longestSubstring(String s) {
String test = "";
if (s.isEmpty()){
return 0;
} else if (s.length() == 1) {
return 1;
}
int maxLength =0;
for (char ch : s.toCharArray()) {
String current = String.valueOf(ch);
if (test.contains(current)) {
test = test.substring(test.indexOf(current)+1);
}
test += current;
maxLength = Math.max(test.length(), maxLength);
}
return maxLength;
}
public static int longestSubstringWithHash(String s) {
if (s.isEmpty()){
return 0;
} else if (s.length() == 1) {
return 1;
}
HashMap<Character, Integer> seen = new HashMap<>();
int maxLength =0, start=0;
for (int end=0; end < s.length(); end++){
if (seen.containsKey(s.charAt(end))) {
start = Math.max(start, seen.get(s.charAt(end))+1);
}
seen.put(s.charAt(end), end);
maxLength = Math.max(maxLength, end-start+1);
}
return maxLength;
}
}
63 changes: 63 additions & 0 deletions src/com/learning/java/Sorting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.learning.java;

public class Sorting {
/** 1. Start iteration from 1 as the single element is assumed to be sorted always
* 2. Take the first element from unsorted sub array ( currentValue = arr[i] )
* 3. Take the last element from the sorted sub array ( j = i-1 )
* 4. While the last elements from the sorted sub array is greater than the current value
* move those elements their one step ahead ahead their current position.
* 5. Insert the current value at its position in sorted sub array ( j+1 ).
**/
public static void insertionSort(int[] arr) {
if (arr == null || arr.length == 0) {
return;
}
for (int i=1;i< arr.length;i++) {
int currentValue = arr[i];
int j = i-1;
while (j >= 0 && arr[j] > currentValue) {
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = currentValue;
}
}
public static void insertionSortDescending(int[] arr) {
if (arr == null || arr.length == 0) {
return;
}
for (int i=1; i< arr.length; i++) {
int currentValue = arr[i];
int j = i-1;
while (j >= 0 && currentValue > arr[j]) {
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = currentValue;
}
}
public static void quickSort(int[] arr, int left, int right) {
if (left >= right) return;
int pivotIndex = findPivot(arr, left, right);
quickSort(arr, left, pivotIndex-1);
quickSort(arr, pivotIndex+1, right);
}
public static int findPivot(int[] arr, int low, int high) {
// selecting last element of an array as pivot element
int pivot = arr[high];
int pointer = low-1;
for (int i=low; i < high; i++) {
if (arr[i] <= pivot) {
pointer++;
swap(arr, pointer, i);
}
}
swap(arr, pointer+1, high); // swapping the pivot element with the element just ahead of left sub array
return (pointer+1);
}
private static void swap(int[] arr, int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
}