Note built-in in Java: binarySearch, fill, sort, isEqual
//would be nice to add test cases;
package pg_quicksearch;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
int[] arr = IntegerArrayMethods.getRandArray(15, 10);
System.out.println("----------------------");
System.out.println(Arrays.toString(arr));
IntegerArrayMethods.shakerSort(arr);
System.out.println(Arrays.toString(arr));
System.out.println("======================");
}
}
}
public class IntArrayRtrn {
public static int getMax(int[] myArr){
int res = myArr[0];
for(int i = 1; i < myArr.length; i++){
if (myArr[i] > res) res = myArr[i];
}
return res;
}
public static int getMin(int[] myArr){
int res = myArr[0];
for(int i = 1; i < myArr.length; i++){
if (myArr[i] < res) res = myArr[i];
}
return res;
}
public static int getSum(int[] myArr){
int res = myArr[0];
for(int i = 1; i < myArr.length; i++){
res += myArr[i];
}
return res;
}
public static double getAverage(int[] myArr){
return getSum(myArr) / myArr.length;
}
//new array; original array is the same
public static int[] getReverseNew(int[] myArr){
int[] revArr = new int[myArr.length];
for(int i = 0; i < myArr.length; i++){
revArr[i] = myArr[(myArr.length - i - 1)];
}
return revArr;
}
public static int getMaxIndex(int[] myArr){
int myMax = getMax(myArr);
int i;
for(i = 0; i < myArr.length - 1; i++){
if (myArr[i] == myMax) return i;
}
return i;
}
public static int getMinIndex(int[] myArr){
int myMin = getMin(myArr);
int i;
for(i = 0; i < myArr.length - 1; i++){
if (myArr[i] == myMin) return i;
}
return i;
}
public static int getMinInRange(int [] myArr, int left, int right){
return myArr[getMaxIndexInRange(myArr, left, right)];
}
public static int getMaxInRange(int [] myArr, int left, int right){
return myArr[getMaxIndexInRange(myArr, left, right)];
}
public static int getMinIndexInRange(int [] myArr, int left, int right){
int myMin = myArr[left];
int i;
int res = left;
for(i = left + 1; i <= Math.min(myArr.length - 1, right); i++){
if (myArr[i] < myMin) {
myMin = myArr[i];
res = i;
}
}
return res;
}
public static int getMaxIndexInRange(int [] myArr, int left, int right){
int myMax = myArr[left];
int i;
int res = left;
for(i = left + 1; i <= Math.min(myArr.length - 1, right); i++){
if (myArr[i] > myMax) {
myMax = myArr[i];
res = i;
}
}
return res;
}
public static int [] merge(int [] arrLeft, int[] arrRight){
int[] resArr = new int[arrLeft.length + arrRight.length];
for(int i = 0; i < resArr.length; i++){
if (i < arrLeft.length) {
resArr[i] = arrLeft[i];
}else{
resArr[i] = arrRight[i-arrLeft.length];
}
}
return resArr;
}
public static int [] copy(int [] arrLeft){
int[] resArr = new int[arrLeft.length];
for(int i = 0; i < arrLeft.length; i++) resArr[i] = arrLeft[i];
return resArr;
}
//returns blank if nothing found; and if error. want to differentiate.
public static int[] getIndexesOf(int [] myArr, int val){
int j = 0;
for (int i = 0; i < myArr.length; i++) if(val == myArr[i]) j++;
int res[] = new int[j];
int k = 0;
for (int i = 0; i < myArr.length; i++){
if(val == myArr[i]) {
res[k] = i;
k++;
}
}
return res;
}
public static boolean contains(int []myArr, int val){
for (int i = 0; i < myArr.length; i++){
if(val == myArr[i]) return true;
}
return false;
}
public static boolean containsInOrdered(int[] arr, int val){
return IntegerArrayMethods.binarySearch(arr, val) > 0 ? true : false ;
}
public static int indexOf(int []myArr, int val){
for (int i = 0; i < myArr.length; i++){
if(val == myArr[i]) return i;
}
return - 1;
}
public static int indexOf(int []myArr, int val){
for (int i = 0; i < myArr.length; i++){
if(val == myArr[i]) return i;
}
return - 1;
}
public static int lastIndexOf(int []myArr, int val){
int res = -1;
for (int i = 0; i < myArr.length; i++){
if(val == myArr[i]) res = i;
}
return res;
}
}
import java.util.Random;
public class IntegerArrayMethods {
private static Random gen = new Random();
public static void printArray(int myArr[]){
for (int i = 0; i < myArr.length; i++) {
System.out.print(myArr[i] + " ");
}
System.out.println();
}
public static int[] getRandArray(int arrLen, int arrMaxMember){
int[] myArr = new int[arrLen];
for (int i = 0; i < myArr.length; i++) {
myArr[i] = gen.nextInt(arrMaxMember);
}
return myArr;
}
public static void getReverseItself(int [] myArr){
for(int i = 0, j = myArr.length - 1; i < j; i++, j--){
getSwap(myArr, i, j);
}
}
public static void getSwap(int [] myArr, int left, int right){
int temp = myArr[left];
myArr[left] = myArr[right];
myArr[right] = temp;
}
public static void sortSelect(int [] myArr, boolean dir){
for(int i = myArr.length - 1; i > 0; i-- ) {
if(dir) getSwap(myArr, IntArrayRtrn.getMaxIndexInRange(myArr, 0, i), i);
else getSwap(myArr, IntArrayRtrn.getMaxIndexInRange(myArr, 0, i), i);
}
}
public static void shuffle(int [] myArr){
for (int i = myArr.length; i > 0; i--){
getSwap(myArr, i-1, gen.nextInt(i));
}
}
public static void bubbleSort(int [] myArr, boolean toUp) {
if (toUp == true) {
for(int i = myArr.length-1; bubblePass(myArr, true, i); i--);
} else {
for(int i = 0; bubblePass(myArr, false, i); i++);
}
}
public static boolean bubblePass(int [] myArr, boolean toUp, int limit){
boolean res = false;
if (toUp == true) {
for(int i = 1; i <= limit; i++) {
if(myArr[i-1] > myArr[i]) {
getSwap(myArr, i, i-1);
res = true;
}
}
} else {
for(int i = myArr.length - 2; i >= limit; i--) {
if(myArr[i+1] < myArr[i]) {
getSwap(myArr, i+1, i);
res = true;
}
}
}
return res;
}
private static boolean maxToEnd(int[] arr, int begin, int end){
boolean isSwapped = false;
for(int i = begin + 1; i <=end; i++){
if (arr[i] < arr[i-1]) {
getSwap(arr, i-1, i);
isSwapped = true;
}
}
return isSwapped;
}
private static boolean minToBegin(int[] arr, int begin, int end){
boolean isSwapped = false;
for(int i = end; i > begin; i--){
if (arr[i] < arr[i-1]) {
getSwap(arr, i-1, i);
isSwapped = true;
}
}
return isSwapped;
}
public static void shakerSort(int[] arr){
int begin = 0;
int end = arr.length - 1;
while(maxToEnd(arr, begin, end--) && minToBegin(arr, begin++, end)){};
}
public static int binarySearch(int [] arr, int val){
if(arr.length == 0) return -1;
int left = 0;
int right = arr.length - 1;
if(arr[left] > val) return - 1;
if(arr[left] == val) return left;
if(arr[right] < val) return - arr.length - 1;
if(arr[right] == val) return right;
int mid = 0;
while (right - left > 1) {
mid = (right + left) / 2;
if(arr[mid] == val) return mid;
if (val > arr[mid]) left = mid;
else right = mid;
}
return -1-right;
}
public static void quickSortEntry(int[] arr){
int low = 0;
int high = arr.length-1;
quickSort(arr, low, high);
}
private static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int part = qsPart(arr, low, high);
if ( part > low) {
//while we have swaps from left
//we decrease range and call again
quickSort(arr, low, part-1);
}
if (part < high) {
//while we have swaps from right
//we decrease range and call again
quickSort(arr, part+1, high);
}
}
}
private static int qsPart(int arr[], int low, int high) {
int i = low;
int j = high+1;
int pivot = arr[low];
while(true) {
//finds first unsorted element from left;
while (arr[++i] < pivot) {
if(i==high) break;
}
//finds first unsorted element from right;
while (arr[--j] > pivot ) {
if(j==low) break;
}
//break if a full pass was made;
if(i >= j) break;
getSwap(arr, i, j);
}
getSwap(arr, low, j);
return j;
}
}
var arr = [3, 5, 6, 8, 4, 0, 2]
quickSortEntry(arr);
console.log(arr);
function quickSortEntry(arr) {
var low = 0;
var high = arr.length - 1;
quickSort(arr, low, high);
}
function quickSort(arr, low, high) {
var part;
if (low < high) {
part = qsPart(arr, low, high);
if (part > low) {
quickSort(arr, low, --part);
}
if (part < high) {
quickSort(arr, ++part, high);
}
}
}
function qsPart(arr, low, high) {
var i = low;
var j = high + 1;
var pivot = arr[low];
while (true) {
//finds first unsorted element from left;
while (arr[++i] < pivot) {
if (i == high) break;
}
//finds first unsorted element from right;
while (arr[--j] > pivot) {
if (j == low) break;
}
//break if a full pass was made;
if (i >= j) break;
swap(arr, i, j);
}
swap(arr, low, j);
return j;
}
function swap(arr, i, j) {
var pocket = arr[i];
arr[i] = arr[j];
arr[j] = pocket;
}
var arr = [3, 5, 6, 8, 4, 0, 2]
quickSortEntry(arr);
console.log(arr);
function quickSortEntry(arr) {
var low = 0;
var high = arr.length - 1;
quickSort(arr, low, high);
}
function quickSort(arr, low, high) {
if (low >= high) return;
var part;
var pivot = arr[Math.floor((high - low + 1) * Math.random())]
part = qsPart(arr, low, high, pivot);
if (part > low) {
quickSort(arr, low, --part, pivot);
}
if (part < high) {
quickSort(arr, ++part, high, pivot);
}
}
function qsPart(arr, low, high, pivot) {
var i = low;
var j = high + 1;
while (true) {
//finds first unsorted element from left;
while (arr[++i] < pivot) {
if (i == high) break;
}
//finds first unsorted element from right;
while (arr[--j] > pivot) {
if (j == low) break;
}
//break if a full pass was made;
if (i >= j) break;
swap(arr, i, j);
}
swap(arr, low, j);
return j;
}
function swap(arr, i, j) {
var pocket = arr[i];
arr[i] = arr[j];
arr[j] = pocket;
}