diff --git a/BinarySearch.java b/BinarySearch.java index 018c5c2..91710c0 100644 --- a/BinarySearch.java +++ b/BinarySearch.java @@ -1,7 +1,7 @@ public class BinarySearch { public static void main(String[] args) { BinarySearch b = new BinarySearch(); - int[] array = new int[]{3,4,5,6,6,12,16}; + int[] array = new int[] { 3, 4, 5, 6, 6, 12, 16 }; System.out.println(b.closest(array, 10)); } @@ -31,13 +31,18 @@ public int binarySearch(int[] array, int target) { } /** - * Search in 2D. - * Given a 2D matrix that contains integers only, which each row is sorted in an ascending order. The first element of next row is larger than (or equal to) the last element of previous row. - * Given a target number, returning the position that the target locates within the matrix. If the target number does not exist in the matrix, return {-1, -1}. + * Search in 2D + *

+ * Given a 2D matrix that contains integers only, which each row is sorted in an + * ascending order. The first element of next row is larger than (or equal to) + * the last element of previous row. + * Given a target number, returning the position that the target locates within + * the matrix. If the target number does not exist in the matrix, return {-1, + * -1}. */ public int[] search(int[][] matrix, int target) { if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { - return new int[]{-1, -1}; + return new int[] { -1, -1 }; } int H = matrix.length; int W = matrix[0].length; @@ -46,12 +51,12 @@ public int[] search(int[][] matrix, int target) { int right = H * W - 1; int mid; - while (left <= right){ + while (left <= right) { mid = left + (right - left) / 2; int y = mid % W; int x = mid / W; if (matrix[x][y] == target) { - return new int[]{x, y}; + return new int[] { x, y }; } if (matrix[x][y] > target) { left = mid + 1; @@ -60,12 +65,14 @@ public int[] search(int[][] matrix, int target) { } } - return new int[]{-1, -1}; + return new int[] { -1, -1 }; } /** * Closest - *Given a target integer T and an integer array A sorted in ascending order, find the index i in A such that A[i] is closest to T. + *

+ * Given a target integer T and an integer array A sorted in ascending order, + * find the index i in A such that A[i] is closest to T. */ public int closest(int[] array, int target) { // Write your solution here @@ -89,8 +96,11 @@ public int closest(int[] array, int target) { } /** - * First Occur. - * Given a target integer T and an integer array A sorted in ascending order, find the index of the first occurrence of T in A or return -1 if there is no such index. + * First Occur + *

+ * Given a target integer T and an integer array A sorted in ascending order, + * find the index of the first occurrence of T in A or return -1 if there is no + * such index. */ public int firstOccur(int[] array, int target) { if (array == null || array.length == 0) { @@ -112,8 +122,11 @@ public int firstOccur(int[] array, int target) { } /** - * K Cloest. - * Given a target integer T, a non-negative integer K and an integer array A sorted in ascending order, find the K closest numbers to T in A. If there is a tie, the smaller elements are always preferred. + * K Cloest + *

+ * Given a target integer T, a non-negative integer K and an integer array A + * sorted in ascending order, find the K closest numbers to T in A. If there is + * a tie, the smaller elements are always preferred. */ public int[] kClosest(int[] array, int target, int k) { if (array == null || array.length == 0) { @@ -143,8 +156,11 @@ public int[] kClosest(int[] array, int target, int k) { } /** - * Smallest Element Larger than Target. - * Given a target integer T and an integer array A sorted in ascending order, find the index of the smallest element in A that is larger than T or return -1 if there is no such index. + * Smallest Element Larger than Target + *

+ * Given a target integer T and an integer array A sorted in ascending order, + * find the index of the smallest element in A that is larger than T or return + * -1 if there is no such index. */ public int smallestElementLargerThanTarget(int[] array, int target) { if (array == null || array.length == 0 || array[array.length - 1] <= target) { @@ -171,8 +187,12 @@ public int smallestElementLargerThanTarget(int[] array, int target) { } /** - * Search In Unknown Sized Sorted Array. - * Given a integer dictionary A of unknown size, where the numbers in the dictionary are sorted in ascending order, determine if a given target integer T is in the dictionary. Return the index of T in A, return -1 if T is not in A. + * Search In Unknown Sized Sorted Array + *

+ * Given a integer dictionary A of unknown size, where the numbers in the + * dictionary are sorted in ascending order, determine if a given target integer + * T is in the dictionary. Return the index of T in A, return -1 if T is not in + * A. */ public int search(Dictionary dict, int target) { int size = 1; diff --git a/RecursionI.java b/RecursionI.java new file mode 100644 index 0000000..37c2b47 --- /dev/null +++ b/RecursionI.java @@ -0,0 +1,173 @@ +public class RecursionI { + public static void main(String[] args) { + RecursionI r = new RecursionI(); + int[] array = new int[] { 3, 5, 1, 2, 4, 8 }; + r.mergeSort(array); + } + + /** + * a to the power of b. + *

+ * Evaluate a to the power of b, assuming both a and b are integers and b is + * non-negative. + */ + public long power(int a, int b) { + if (b == 0) { + return 1; + } + if (b == 1) { + return a; + } + long temp = power(a, b / 2); + return temp * temp * power(a, b % 2); + } + + /** + * Selection Sort + *

+ * an array of integers, sort the elements in the array in ascending order. The + * selection sort algorithm should be used to solve this problem. + */ + public int[] solve(int[] array) { + for (int i = 0; i < array.length - 1; i++) { + int minIndex = i; + for (int j = i + 1; j < array.length; j++) { + if (array[j] < array[minIndex]) { + minIndex = j; + } + } + swap(array, i, minIndex); + } + return array; + } + + /** + * merge sort + *

+ * Given an array of integers, sort the elements in the array in ascending + * order. The merge sort algorithm should be used to solve this problem. + */ + public int[] mergeSort(int[] array) { + if (array == null || array.length == 0) { + return array; + } + mergeSort(array, 0, array.length - 1); + return array; + } + + private void mergeSort(int[] array, int left, int right) { + if (right == left) { + return; + } + int mid = left + (right - left) / 2; + mergeSort(array, left, mid); + mergeSort(array, mid + 1, right); + merge(array, left, right, mid); + } + + private void merge(int[] array, int left, int right, int mid) { + int[] aux = new int[mid - left + 1]; + for (int i = 0; i < mid - left + 1; i++) { + aux[i] = array[left + i]; + } + + int i = 0; + int j = mid + 1; + int k = left; + while (k <= right) { + if (j == right + 1) { + array[k++] = aux[i++]; + } else if (i == mid - left + 1) { + array[k++] = array[j++]; + } else { + if (array[j] < aux[i]) { + array[k++] = array[j++]; + } else { + array[k++] = aux[i++]; + } + } + } + } + + /** + * quick sort. Given an array of integers, sort the elements in the array in + * ascending order. The quick sort algorithm should be used to solve this + * problem. + */ + public int[] quickSort(int[] array) { + if (array == null || array.length == 0) { + return array; + } + quickSort(array, 0, array.length - 1); + + return array; + } + + private void quickSort(int[] array, int left, int right) { + if (right <= left) { + return; + } + int pivot = partition(array, left, right); + quickSort(array, left, pivot - 1); + quickSort(array, pivot + 1, right); + } + + private int partition(int[] array, int left, int right) { + int pivot = left + (int) (Math.random() * (right - left + 1)); + swap(array, pivot, right--); + pivot = right; + int k = left; + while (k <= right) { + if (array[k] < array[pivot]) { + swap(array, left++, k++); + } else { + swap(array, k, right--); + } + } + swap(array, k, pivot); + return k; + } + + /** + * Move 0s To The End I + *

+ * Given an array of integers, move all the 0s to the right end of the array. + * The relative order of the elements in the original array does not need to be + * maintained. + */ + public int[] moveZero(int[] array) { + return new int[0]; + } + + /** + * Rainbow Sort + *

+ * Given an array of balls, where the color of the balls can only be Red, Green + * or Blue, sort the balls such that all the Red balls are grouped on the left + * side, all the Green balls are grouped in the middle and all the Blue balls + * are grouped on the right side. (Red is denoted by -1, Green is denoted by 0, + * and Blue is denoted by 1). + */ + public int[] rainbowSort(int[] array) { + int i = 0; + int j = array.length - 1; + int k = 0; + + while (k <= j) { + if (array[k] == -1) { + swap(array, i++, k++); + } else if (array[k] == 1) { + swap(array, k, j--); + } else { + k++; + } + } + return array; + } + + private static void swap(int[] array, int a, int b) { + int temp = array[a]; + array[a] = array[b]; + array[b] = temp; + } +} diff --git a/utils/Common.java b/utils/Common.java new file mode 100644 index 0000000..7980e6b --- /dev/null +++ b/utils/Common.java @@ -0,0 +1,9 @@ +package utils; + +public class Common { + public static void swap(int[] array, int a, int b) { + int temp = array[a]; + array[a] = array[b]; + array[b] = array[temp]; + } +}