在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:iluwatar/30-seconds-of-java开源软件地址:https://github.com/iluwatar/30-seconds-of-java开源编程语言:Java 100.0%开源软件介绍:30 Seconds of JavaInspired by 30 seconds of code, this is a collection of reusable tested copy-pasteable Java 11 compatible code snippets that you can understand in 30 seconds or less. If you're interested in contributing to this library, please see the instructions. Table of ContentsAlgorithmArrayEncodingFileMathMediaNetworkingStringClassI/OThreadAlgorithmQuicksort public static void quickSort(int[] arr, int left, int right) {
var pivotIndex = left + (right - left) / 2;
var pivotValue = arr[pivotIndex];
var i = left;
var j = right;
while (i <= j) {
while (arr[i] < pivotValue) {
i++;
}
while (arr[j] > pivotValue) {
j--;
}
if (i <= j) {
var tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
if (left < i) {
quickSort(arr, left, j);
}
if (right > i) {
quickSort(arr, i, right);
}
}
} Bubblesort public static void bubbleSort(int[] arr) {
var lastIndex = arr.length - 1;
for(var j = 0; j < lastIndex; j++) {
for(var i = 0; i < lastIndex - j; i++) {
if(arr[i] > arr[i + 1]) {
var tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
}
}
}
} Selectionsort public static void selectionSort(int[] arr) {
var len = arr.length;
for (var i = 0; i < len - 1; i++) {
var minIndex = i;
for (var j = i + 1; j < len; j++) {
if(arr[j] < arr[minIndex])
minIndex = j;
}
var tmp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = tmp;
}
} InsertionSort public static void insertionSort(int[] arr) {
for (var i = 1; i < arr.length; i++) {
var tmp = arr[i];
var j = i - 1;
while (j >= 0 && arr[j] > tmp) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = tmp;
}
} CountingSort public static void countingSort(int[] arr) {
var max = Arrays.stream(arr).max().getAsInt();
var count = new int[max + 1];
for (var num : arr) {
count[num]++;
}
for (var i = 1; i <= max; i++) {
count[i] += count[i - 1];
}
var sorted = new int[arr.length];
for (var i = arr.length - 1; i >= 0; i--) {
var cur = arr[i];
sorted[count[cur] - 1] = cur;
count[cur]--;
}
var index = 0;
for (var num : sorted) {
arr[index++] = num;
}
} ArrayGeneric two array concatenation public static <T> T[] arrayConcat(T[] first, T[] second) {
var result = Arrays.copyOf(first, first.length + second.length);
System.arraycopy(second, 0, result, first.length, se |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论