选择排序,插入排序,冒泡排序,二分法复习

pull/3/head
Leo 5 years ago
parent ac7636d030
commit df1601835e

@ -4,7 +4,7 @@ package leo.class01;
* @author Leo
* @ClassName BSAwesome
* @DATE 2020/11/12 5:09
* @Description ,,.>value<
* @Description ,,.>value<
*/
public class BSAwesome {

@ -43,6 +43,50 @@ public class BSExist {
return arr[L] == value;
}
public static boolean exist2(int[] arr, int value) {
if (arr.length == 0) {
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) {
return true;
} else if (arr[mid] > value) {
R = mid - 1;
} else if (arr[mid] < value) {
L = mid + 1;
}
}
return arr[L] == value;
}
public static boolean exist3(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) {
return true;
} else if (arr[mid] > value) {
R = mid - 1;
} else if (arr[mid] < value) {
L = mid + 1;
}
}
return arr[L] == value;
}
/**
* : test
* @author Leo
@ -65,13 +109,13 @@ public class BSExist {
int maxSize = 50;
int range = 500;
int testTime = 100;
int testTime = 10;
boolean succeed = true;
for (int i = 0; i < testTime; i++) {
int[] sortArr = ArrayUtil.randomSortArray(maxSize, range);
int value = (int) ((range + 1) * Math.random() - (range + 1) * Math.random());
if (exist(sortArr, value) != forExist(sortArr, value)) {
if (exist3(sortArr, value) != exist(sortArr, value)) {
succeed = false;
ArrayUtil.printArr(sortArr);
break;

@ -40,6 +40,51 @@ public class BSNear {
return index;
}
public static int BSNearLeft2(int[] arr, int value) {
int index = -1;
if (arr.length == 0 || arr == null) {
return index;
}
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 BSNearLeft3(int[] arr, int value) {
int index = -1;
if (arr.length == 0 || arr == null) {
return index;
}
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;
}
/**
* : >=(for test)
* @author Leo
@ -94,6 +139,27 @@ public class BSNear {
return index;
}
public static int BSNearRight2(int[] arr, int value) {
int index = -1;
if (arr.length == 0 || arr == null) {
return index;
}
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;
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) {
@ -116,18 +182,17 @@ public class BSNear {
for (int i = 0; i < testTime; i++) {
int[] sortArr = ArrayUtil.randomSortArray(maxSize, range);
int value = (int) ((range + 1) * Math.random() - (range + 1) * Math.random());
final int res1 = BSNearLeft(sortArr, value);
/*final int res1 = BSNearLeft3(sortArr, value);
final int res2 = forTestBSNearLeft(sortArr, value);
final int res3 = BSNearRight(sortArr, value);
final int res4 = forTestBSNearRight(sortArr, value);
if (res1 != res2) {
success = false;
ArrayUtil.printArr(sortArr);
System.out.println("BSNearLeft=" + res1);
System.out.println("forTestBSNearLeft=" + res2);
break;
}
}*/
final int res3 = BSNearRight(sortArr, value);
final int res4 = forTestBSNearRight(sortArr, value);
if (res3 != res4) {
success = false;
ArrayUtil.printArr(sortArr);

@ -47,7 +47,31 @@ public class BubbleSort {
}
public static void bubbleSort3(int[] arr) {
if (arr.length < 2 || 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);
}
}
}
}
public static void bubbleSort4(int[] arr) {
if (arr.length < 2 || 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);
}
}
}
}
/**
* :
@ -72,14 +96,14 @@ public class BubbleSort {
public static void main(String[] args){
int maxSize = 500;
int maxSize = 50;
int range = 20;
int testOfTime = 100000;
int testOfTime = 1000;
boolean succeed = true;
for (int i = 0; i < testOfTime; i++) {
int[] arr = ArrayUtil.randomArray(maxSize, range);
int[] anotherArr = ArrayUtil.copyArray(arr);
bubbleSort2(arr);
bubbleSort4(arr);
Arrays.sort(anotherArr);
if (!ArrayUtil.isEqual(arr, anotherArr)) {
succeed = false;

@ -3,8 +3,6 @@ package leo.class01;
import leo.util.ArrayUtil;
import java.util.Arrays;
import java.util.jar.JarEntry;
/**
* @author Leo
* @ClassName InsertionSort
@ -33,6 +31,41 @@ public class InsertionSort {
}
}
public static void insertionSort1(int[] arr) {
if (arr == null || arr.length < 2) {
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 insertionSort2(int[] arr) {
if (arr.length < 2 || 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 insertionSort3(int[] arr) {
if (arr.length < 2 || 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);
}
}
}
private static void swap(int[] arr, int i, int j) {
if (arr == null
@ -58,7 +91,7 @@ public class InsertionSort {
for (int i = 0; i < testOfTime; i++) {
int[] arr = ArrayUtil.randomArray(maxSize, range);
int[] anotherArr = ArrayUtil.copyArray(arr);
insertionSort(arr);
insertionSort3(arr);
Arrays.sort(anotherArr);
if (!ArrayUtil.isEqual(arr, anotherArr)) {
succeed = false;

@ -81,6 +81,20 @@ public class SelectionSort {
}
}
public static void selectionSort5(int[] arr) {
if (arr.length < 2 || 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);
}
}
}
}
/**
* :
@ -107,12 +121,12 @@ public class SelectionSort {
public static void main(String[] args){
int maxSize = 100;
int range = 50;
int testOfTime = 100000;
int testOfTime = 20;
boolean succeed = true;
for (int i = 0; i < testOfTime; i++) {
int[] arr = ArrayUtil.randomArray(maxSize, range);
int[] anotherArr = ArrayUtil.copyArray(arr);
selectionSort4(arr);
selectionSort5(arr);
Arrays.sort(anotherArr);
if (!ArrayUtil.isEqual(arr, anotherArr)) {
succeed = false;

Loading…
Cancel
Save