pull/3/head
Leo 5 years ago
parent 63701c9917
commit a15fc31e72

@ -161,6 +161,33 @@ public class BSAwesome {
}
public static int BSAwesome4(int[] arr) {
if (arr.length <= 0 || arr == null) {
return -1;
}
if (arr.length == 1 || arr[0] < arr[1]) {
return 0;
}
if (arr[arr.length - 1] < arr[arr.length - 2]) {
return arr.length - 1;
}
int L = 0;
int R = arr.length - 1;
int mid = 0;
while (L <= R) {
mid = L + ((R - L) >> 1);
if (arr[mid] > arr[mid + 1]) {
L = mid + 1;
} else if (arr[mid] > arr[mid - 1]) {
R = mid - 1;
}else{
return mid;
}
}
return -1;
}
public static int verifyBSAwesome(int[] arr, int index) {
if (arr.length == 0 || index == -1) {
return -1;
@ -198,7 +225,7 @@ public class BSAwesome {
boolean succeed = true;
for (int i = 0; i < testTime; i++) {
int[] arr = ArrayUtil.randomAdjacentNotEqualArray(maxSize, range);
final int index = BSAwesome3(arr);
final int index = BSAwesome4(arr);
final int verifyIndex = verifyBSAwesome(arr,index);
if (index != verifyIndex) {
succeed = false;

@ -129,6 +129,27 @@ public class BSExist {
return arr[L] == value;
}
public static boolean exist6(int[] arr, int value) {
if (arr.length <= 0 || arr == null) {
return false;
}
int L = 0;
int R = arr.length - 1;
int mid = 0;
while (L < R) {
mid = L + ((R - L) >> 1);
if (arr[mid] > value) {
R = mid - 1;
} else if (arr[mid] < value) {
L = mid + 1;
}else {
return true;
}
}
return arr[L] == value;
}
/**
* : test
@ -158,7 +179,7 @@ public class BSExist {
int[] sortArr = ArrayUtil.randomSortArray(maxSize, range);
int value = (int) ((range + 1) * Math.random() - (range + 1) * Math.random());
if (exist5(sortArr, value) != exist(sortArr, value)) {
if (exist6(sortArr, value) != exist(sortArr, value)) {
succeed = false;
ArrayUtil.printArr(sortArr);
break;

@ -2,6 +2,8 @@ package leo.class01;
import leo.util.ArrayUtil;
import java.util.Arrays;
/**
* @author Leo
* @ClassName BSNear
@ -125,6 +127,46 @@ public class BSNear {
return index;
}
public static int BSNearLeft6(int[] arr, int value) {
if (arr.length <= 0 || arr == null) {
return -1;
}
int index = -1;
int L = 0;
int R = arr.length - 1;
int mid = 0;
while (L <= R) {
mid = L + ((R - L) >> 1);
if (arr[mid] >= value) {
index = mid;
R = mid - 1;
}else{
L = mid + 1;
}
}
return index;
}
public static int BSNearLeft7(int[] arr, int value) {
if (arr.length <= 0 || arr == null) {
return -1;
}
int L = 0;
int R = arr.length - 1;
int index = -1;
int mid = 0;
while (L <= R) {
mid = L + ((R - L) >>1 );
if (arr[mid] >= value) {
index = mid;
R = mid - 1;
}else{
L = mid + 1;
}
}
return index;
}
/**
* : >=(for test)
@ -244,6 +286,28 @@ public class BSNear {
return index;
}
public static int BSNearRight5(int[] arr, int value) {
if (arr.length <= 0 || arr == null) {
return -1;
}
int L = 0;
int R = arr.length - 1;
int mid = 0;
int index = -1;
while (L <= R) {
mid = L + ((R - L) >> 1);
if (arr[mid] <= value) {
index = mid;
L = mid + 1;
}else{
R = mid - 1;
}
}
return index;
}
public static int forTestBSNearRight(int[] arr, int value) {
int index = -1;
if (arr == null || arr.length == 0) {
@ -260,13 +324,13 @@ public class BSNear {
public static void main(String[] args){
int maxSize = 80;
int range = 800;
int range = 80;
int testTime = 10000;
boolean success = true;
for (int i = 0; i < testTime; i++) {
int[] sortArr = ArrayUtil.randomSortArray(maxSize, range);
int[] sortArr = randomArray(maxSize, range);
int value = (int) ((range + 1) * Math.random() - (range + 1) * Math.random());
/* final int res1 = BSNearLeft5(sortArr, value);
/* final int res1 = BSNearLeft7(sortArr, value);
final int res2 = forTestBSNearLeft(sortArr, value);
if (res1 != res2) {
success = false;
@ -275,7 +339,7 @@ public class BSNear {
System.out.println("forTestBSNearLeft=" + res2);
break;
}*/
final int res3 = BSNearRight4(sortArr, value);
final int res3 = BSNearRight5(sortArr, value);
final int res4 = forTestBSNearRight(sortArr, value);
if (res3 != res4) {
success = false;
@ -289,6 +353,16 @@ public class BSNear {
}
public static int[] randomArray(int sizeMax, int range) {
int[] arr = new int[(int) (sizeMax * Math.random() + 1)];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) ((range * Math.random() + 1) - (range * Math.random() + 1));
}
Arrays.sort(arr);
return arr;
}
}

@ -127,6 +127,20 @@ public class BubbleSort {
}
}
public static void bubbleSort9(int[] arr) {
if (arr.length <= 0 || arr == null) {
return;
}
for (int i = arr.length - 1; i >= 0; i--) {
for (int j = 0; j < i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1);
}
}
}
}
/**
* :
* @author Leo
@ -157,7 +171,7 @@ public class BubbleSort {
for (int i = 0; i < testOfTime; i++) {
int[] arr = ArrayUtil.randomArray(maxSize, range);
int[] anotherArr = ArrayUtil.copyArray(arr);
bubbleSort8(arr);
bubbleSort9(arr);
Arrays.sort(anotherArr);
if (!ArrayUtil.isEqual(arr, anotherArr)) {
succeed = false;

@ -40,6 +40,15 @@ public class EvenTimesOddTimes {
System.out.println(eor);
}
public static void printOdd3(int[] arr) {
int eor = 0;
for (int i = 0; i < arr.length; i++) {
eor ^= arr[i];
}
System.out.println(eor);
}
/**
* : ,
@ -97,7 +106,23 @@ public class EvenTimesOddTimes {
eorOther ^= arr[i];
}
}
System.out.println(eor + " " + (eor ^ eorOther));
System.out.println(eorOther + " " + (eor ^ eorOther));
}
public static void printOddTwo4(int[] arr) {
int eor = 0;
for (int i = 0; i < arr.length; i++) {
eor ^= arr[i];
}
int rightOne = eor & (-eor);
int eorOther = 0;
for (int i = 0; i < arr.length; i++) {
if ((rightOne & arr[i]) != 0) {
eorOther ^= arr[i];
}
}
System.out.println(eorOther + " " + (eor ^ eorOther));
}

@ -114,6 +114,31 @@ public class InsertionSort {
}
}
public static void insertionSort8(int[] arr) {
if (arr.length < 0 || arr == null) {
return;
}
for (int i = 1; i < arr.length; i++) {
for (int j = i - 1; j >= 0 && arr[j] > arr[j +1]; j--) {
swap(arr, j, j + 1);
}
}
}
public static void insertionSort9(int[] arr) {
if (arr == null || arr.length <= 0) {
return;
}
for (int i = 1; i < arr.length; i++) {
for (int j = i - 1; j >= 0 && arr[j] > arr[j + 1]; j--) {
swap(arr, j, j + 1);
}
}
}
private static void swap(int[] arr, int i, int j) {
if (arr == null
|| arr.length < 2
@ -138,7 +163,7 @@ public class InsertionSort {
for (int i = 0; i < testOfTime; i++) {
int[] arr = ArrayUtil.randomArray(maxSize, range);
int[] anotherArr = ArrayUtil.copyArray(arr);
insertionSort7(arr);
insertionSort9(arr);
Arrays.sort(anotherArr);
if (!ArrayUtil.isEqual(arr, anotherArr)) {
succeed = false;

@ -52,6 +52,39 @@ public class KM {
}
public static int onlyKTimes1(int[] arr, int k, int m) {
int[] t = new int[32];
for (int num : arr) {
for (int i = 0; i < t.length; i++) {
t[i] += (num >> i) & 1;
}
}
int ans = 0;
for (int i = 0; i < t.length; i++) {
if (t[i] % m != 0) {
ans |= 1 << i;
}
}
return ans;
}
public static int onlyKTimes2(int[] arr, int k, int m) {
int[] t = new int[32];
for(int num:arr){
for (int i = 0; i < t.length; i++) {
t[i] += (num >> i) & 1;
}
}
int ans = 0;
for (int i = 0; i < t.length; i++) {
if (t[i] % m != 0) {
ans |= 1 << i;
}
}
return ans;
}
public static void main(String[] args) {
int maxKinds = 8;
int range = 50;
@ -66,11 +99,13 @@ public class KM {
if (k == m) {
m++;
}
int[] arr = randomArray(maxKinds, range, k, m);
int[] arr = randomArray1(maxKinds, range, k, m);
int ans = onlyKTimes(arr, k, m);
int ans = onlyKTimes2(arr, k, m);
int ans2 = testForOnlyKTimes(arr, k, m);
if (ans != ans2) {
System.out.println(ans);
System.out.println(ans2);
System.out.println("出错了!!");
break;
}
@ -78,6 +113,35 @@ public class KM {
System.out.println("结束");
}
private static int[] randomArray1(int maxKinds, int range, int k, int m) {
//k
int kNumber = randomInt(range);
//随机生成共有几种数
int kinds = (int) (maxKinds * Math.random() + 1);
int arrLength = k + (kinds - 1) * m;
int[] arr = new int[arrLength];
int index = 0;
HashSet<Integer> set = new HashSet<>();
for (; index < k; index++) {
arr[index] = kNumber;
}
kinds--;
set.add(kNumber);
while (kinds != 0) {
int curNum = 0;
do {
curNum = randomInt(range);
} while (set.contains(curNum));
for (int i = 0; i < m; i++) {
arr[index++] = curNum;
}
kinds--;
}
return arr;
}
/**
* :
* @author Leo

@ -149,6 +149,19 @@ public class SelectionSort {
}
}
public static void selectionSort10(int[] arr){
if (arr.length <= 0 || arr == null) {
return;
}
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
swap(arr, i, j);
}
}
}
}
/**
* :
@ -180,7 +193,7 @@ public class SelectionSort {
for (int i = 0; i < testOfTime; i++) {
int[] arr = ArrayUtil.randomArray(maxSize, range);
int[] anotherArr = ArrayUtil.copyArray(arr);
selectionSort9(arr);
selectionSort10(arr);
Arrays.sort(anotherArr);
if (!ArrayUtil.isEqual(arr, anotherArr)) {
succeed = false;

Loading…
Cancel
Save