数组两元素交换位置
public class ArrayUtil {
/**
* 交换数组中的两个元素
* @param array 数组
* @param ele1Idx 元素1的索引下标
* @param ele2Idx 元素1的索引下表
*/
public static void swap(int[] array, int ele1Idx, int ele2Idx) {
int tmp = array[ele1Idx];
array[ele1Idx] = array[ele2Idx];
array[ele2Idx] = tmp;
}
}
public class BubbleSort {
public static void main(String[] args) {
int[] nums = {12, 34, 11, 98, 56, 49, 21, 29, 19};
System.out.println("排序前: " + Arrays.toString(nums));
new BubbleSort().bubbleSort(nums);
System.out.println("排序后: " + Arrays.toString(nums));
}
private void bubbleSort(int[] nums) {
for (int i = 0; i < nums.length - 1; i++) {
for (int j = 0; j < nums.length - 1 - i; j++) {
if (nums[j] > nums[j + 1]) {
ArrayUtil.swap(nums, j, j + 1);
}
}
}
}
}
public class QuickSort {
public static void main(String[] args) {
int[] nums = {12, 34, 11, 98, 56, 49, 21, 29, 19};
System.out.println("排序前: " + Arrays.toString(nums));
new QuickSort().quickSort(nums, 0, nums.length - 1);
System.out.println("排序后: " + Arrays.toString(nums));
}
private void quickSort(int[] nums, int start, int end) {
if (start > end) return;
int i = start, j = end, base = nums[start];
while (i < j) {
while (j >= 0 && nums[j] >= base) j--;
while (i < j && nums[i] <= base) i++;
if (i < j) {
ArrayUtil.swap(nums, i, j);
}
}
ArrayUtil.swap(nums, start, i);
quickSort(nums, start, i - 1);
quickSort(nums, i + 1, end);
}
}
public class InsertionSort {
public static void main(String[] args) {
int[] nums = {12, 34, 11, 98, 56, 49, 21, 29, 19};
System.out.println("排序前: " + Arrays.toString(nums));
new InsertionSort().insertionSort(nums);
System.out.println("排序后: " + Arrays.toString(nums));
}
private void insertionSort(int[] nums) {
for (int i = 1; i < nums.length; i++) {
int j, temp = nums[i];
for (j = i; j > 0 && nums[j - 1] > temp; j--) {
nums[j] = nums[j - 1];
}
nums[j] = temp;
}
}
}
public class ShellSort {
public static void main(String[] args) {
int[] nums = {12, 34, 11, 98, 56, 49, 21, 29, 19};
System.out.println("排序前: " + Arrays.toString(nums));
new ShellSort().shellSort(nums);
System.out.println("排序后: " + Arrays.toString(nums));
}
private void shellSort(int[] nums) {
// 缩小增量排序
for (int gap = nums.length / 2; gap >= 1; gap /= 2) {
for (int i = gap; i < nums.length; i++) {
int j, temp = nums[i];
for (j = i; j >= gap && nums[j - gap] > temp; j-=gap) {
nums[j] = nums[j - gap];
}
nums[j] = temp;
}
}
}
}
public class SelectionSort {
public static void main(String[] args) {
int[] nums = {12, 34, 11, 98, 56, 49, 21, 29, 19};
System.out.println("排序前: " + Arrays.toString(nums));
new SelectionSort().selectionSort(nums);
System.out.println("排序后: " + Arrays.toString(nums));
}
private void selectionSort(int[] nums) {
for (int i = 0; i < nums.length - 1; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] > nums[j]) {
ArrayUtil.swap(nums, i, j);
}
}
}
}
}
public class HeapSort {
public static void main(String[] args) {
int[] nums = {12, 34, 11, 98, 56, 49, 21, 29, 19};
System.out.println("排序前: " + Arrays.toString(nums));
new HeapSort().headSort(nums);
System.out.println("排序后: " + Arrays.toString(nums));
}
private void headSort(int[] nums) {
int n = nums.length;
buildHeap(nums, n);
for (int i = n - 1; i >= 0 ; i--) {
swap(nums, i, 0);
heapify(nums, i, 0);
}
}
private void buildHeap(int[] tree, int n) {
int lastNodeIndex = n - 1;
int parentNodeIndex = (lastNodeIndex - 1) / 2;
for (int i = parentNodeIndex; i >= 0; i--) {
heapify(tree, n, i);
}
}
private void heapify(int[] tree, int n, int i) {
if (i > n) {
return;
}
int l = 2 * i + 1;
int r = 2 * i + 2;
int max = i;
if (l < n && tree[l] > tree[max]) {
max = l;
}
if (r < n && tree[r] > tree[max]) {
max = r;
}
if (max != i) {
swap(tree, max, i);
heapify(tree, n, max);
}
}
private void swap(int[] nums, int left, int right) {
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
}
}
public class MergeSort {
public static void main(String[] args) {
int[] nums = {6, 8, 10, 9, 4, 5, 2, 7};
new MergeSort().mergeSort(nums, 0, nums.length - 1);
System.out.println(Arrays.toString(nums));
}
private void mergeSort(int[] arr, int l, int r) {
if (l == r) return;
int m = (l + r) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m + 1, r);
}
private void merge(int[] arr, int l, int m, int r) {
int lSize = m - l;
int rSize = r - m + 1;
int[] lArr = new int[lSize];
int[] rArr = new int[rSize];
// 1.fill in the left sub array
if (m - l >= 0) System.arraycopy(arr, l, lArr, 0, lSize);
// 2.fill in the right sub array
if (r + 1 - m >= 0) System.arraycopy(arr, m, rArr, 0, rSize);
// 3.merge into the original array
int i = 0, j = 0, k = l;
while (i < lSize && j < rSize) {
if (lArr[i] < rArr[j]) {
arr[k++] = lArr[i++];
} else {
arr[k++] = rArr[j++];
}
}
while (i < lSize) {
arr[k++] = lArr[i++];
}
while (j < rSize) {
arr[k++] = rArr[j++];
}
}
}
public class ThreeWayMergeSort {
public static void main(String[] args) {
int[] nums = {6, 8, 10, 9, 4, 33, 11, 98, 11, 1, 5, 2, 7};
new ThreeWayMergeSort().threeWayMergeSort(nums);
System.out.println(Arrays.toString(nums));
}
private void threeWayMergeSort(int[] arr) {
threeWayMergeSort(arr, 0, arr.length - 1);
}
private void threeWayMergeSort(int[] arr, int l, int r) {
if (l >= r) return;
int m1 = l + (r - l) / 3;
int m2 = l + 2 * (r - l) / 3;
threeWayMergeSort(arr, l, m1);
threeWayMergeSort(arr, m1 + 1, m2);
threeWayMergeSort(arr, m2 + 1, r);
merge(arr, l, m1, m2, r);
}
private static void merge(int[] arr, int l, int m1, int m2, int r) {
int n1 = m1 - l + 1;
int n2 = m2 - m1;
int n3 = r - m2;
int[] lArr = new int[n1];
int[] mArr = new int[n2];
int[] rArr = new int[n3];
System.arraycopy(arr, l, lArr,0, n1);
System.arraycopy(arr, m1 + 1, mArr,0, n2);
System.arraycopy(arr, m2 + 1, rArr,0, n3);
int i = 0, j = 0, k = 0, idx = l;
while (i < n1 && j < n2 && k < n3) {
if (lArr[i] <= mArr[j] && lArr[i] <= rArr[k]) {
arr[idx++] = lArr[i++];
} else if (mArr[j] <= lArr[i] && mArr[j] <= rArr[k]) {
arr[idx++] = mArr[j++];
} else {
arr[idx++] = rArr[k++];
}
}
while (j < n2 && k < n3) arr[idx++] = mArr[j] <= rArr[k] ? mArr[j++] : rArr[k++];
while (i < n1 && k < n3) arr[idx++] = lArr[i] <= rArr[k] ? lArr[i++] : rArr[k++];
while (i < n1 && j < n2) arr[idx++] = lArr[i] <= mArr[j] ? lArr[i++] : mArr[j++];
while (i < n1) arr[idx++] = lArr[i++];
while (j < n2) arr[idx++] = mArr[j++];
while (k < n3) arr[idx++] = rArr[k++];
}
}
public class NormalMultiWayMergeSort {
public static void main(String[] args) {
int[] nums = {6, 8, 10, 9, 4, 33, 11, 98, 11, 1, 5, 2, 7};
new NormalMultiWayMergeSort().multiWayMergeSort(nums);
System.out.println(Arrays.toString(nums));
}
private void multiWayMergeSort(int[] arr) {
multiWayMergeSort(arr, 0, arr.length - 1, 3);
}
private void multiWayMergeSort(int[] arr, int l, int r, int n) {
if (l >= r) return;
int m1 = l + (r - l) / n;
multiWayMergeSort(arr, l, m1, n);
int mn = m1;
for (int i = 2; i < n; i++) {
int mm = l + i * (r - l) / n;
multiWayMergeSort(arr, m1 + 1, mm, n);
mn = mm;
}
multiWayMergeSort(arr, mn + 1, r, n);
merge(arr, l, m1, mn, r);
}
private static void merge(int[] arr, int l, int m1, int m2, int r) {
int n1 = m1 - l + 1;
int n2 = m2 - m1;
int n3 = r - m2;
int[] lArr = new int[n1];
int[] mArr = new int[n2];
int[] rArr = new int[n3];
System.arraycopy(arr, l, lArr,0, n1);
System.arraycopy(arr, m1 + 1, mArr,0, n2);
System.arraycopy(arr, m2 + 1, rArr,0, n3);
int i = 0, j = 0, k = 0, idx = l;
while (i < n1 && j < n2 && k < n3) {
if (lArr[i] <= mArr[j] && lArr[i] <= rArr[k]) {
arr[idx++] = lArr[i++];
} else if (mArr[j] <= lArr[i] && mArr[j] <= rArr[k]) {
arr[idx++] = mArr[j++];
} else {
arr[idx++] = rArr[k++];
}
}
while (j < n2 && k < n3) arr[idx++] = mArr[j] <= rArr[k] ? mArr[j++] : rArr[k++];
while (i < n1 && k < n3) arr[idx++] = lArr[i] <= rArr[k] ? lArr[i++] : rArr[k++];
while (i < n1 && j < n2) arr[idx++] = lArr[i] <= mArr[j] ? lArr[i++] : mArr[j++];
while (i < n1) arr[idx++] = lArr[i++];
while (j < n2) arr[idx++] = mArr[j++];
while (k < n3) arr[idx++] = rArr[k++];
}
}
public class QueueMultiWayMergeSort {
public static void main(String[] args) {
int[] nums = {6, 8, 10, 9, 4, 33, 11, 98, 11, 1, 5, 2, 7};
int[] sortedNums = new QueueMultiWayMergeSort().multiWayMergeSort(nums);
System.out.println(Arrays.toString(sortedNums));
}
private int[] multiWayMergeSort(int[] array) {
PriorityQueue<List<Integer>> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a.get(0)));
for (int num : array) {
List<Integer> sublist = new ArrayList<>();
sublist.add(num);
pq.offer(sublist);
}
while (pq.size() > 1) {
List<Integer> list1 = pq.poll();
List<Integer> list2 = pq.poll();
List<Integer> mergedList = merge(list1, list2);
pq.offer(mergedList);
}
return Objects.requireNonNull(pq.poll()).stream().mapToInt(i -> i).toArray();
}
private List<Integer> merge(List<Integer> list1, List<Integer> list2) {
List<Integer> mergedList = new ArrayList<>();
int i = 0, j = 0;
while (i < list1.size() && j < list2.size()) {
if (list1.get(i) < list2.get(j)) {
mergedList.add(list1.get(i++));
} else {
mergedList.add(list2.get(j++));
}
}
while (i < list1.size()) {
mergedList.add(list1.get(i++));
}
while (j < list2.size()) {
mergedList.add(list2.get(j++));
}
return mergedList;
}
}
public class CountingSort {
public static void main(String[] args) {
int[] nums = {12, 34, 11, 98, 56, 49, 21, 29, 19};
System.out.println("排序前: " + Arrays.toString(nums));
new CountingSort().countingSort(nums);
System.out.println("排序后: " + Arrays.toString(nums));
}
public void countingSort(int[] nums) {
int[] mm = getMaxMin(nums);
int[] cnt = new int[mm[1] - mm[0] + 1];
for (int num : nums) {
cnt[num - mm[0]] += 1;
}
int idx = 0;
for (int i = 0; i < cnt.length; i++) {
for (int j = 0; j < cnt[i]; j++) {
nums[idx++] = i + mm[0];
}
}
}
private static int[] getMaxMin(int[] nums) {
int max = nums[0], min = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] > max) max = nums[i];
if (nums[i] < min) min = nums[i];
}
return new int[]{min, max};
}
}
public class BucketSort {
public static void main(String[] args) {
int[] nums = {12, 34, 11, 98, 56, 49, 21, 29, 19};
System.out.println("排序前: " + Arrays.toString(nums));
new BucketSort().bucketSort(nums);
System.out.println("排序后: " + Arrays.toString(nums));
}
public void bucketSort(int[] nums) {
if (nums == null || nums.length == 0) {
return;
}
// 找出数组中的最大值和最小值
int[] mm = getMaxMin(nums);
// 初始化桶数组
int listSize = mm[1] - mm[0] + 1;
List<List<Integer>> buckets = new ArrayList<>(listSize);
for (int i = 0; i < listSize; i++) {
buckets.add(new ArrayList<>());
}
// 将数据分配到桶中
for (int num : nums) {
buckets.get(num - mm[0]).add(num);
}
// 对每个桶进行排序,这里使用了简单的Insertion Sort
for (List<Integer> numList : buckets) {
if (!numList.isEmpty()) {
Collections.sort(numList);
}
}
// 从桶中收集已排序的数据
int k = 0;
for (List<Integer> bucket : buckets) {
for (Integer value : bucket) {
nums[k++] = value;
}
}
}
private static int[] getMaxMin(int[] nums) {
int max = nums[0], min = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] > max) max = nums[i];
if (nums[i] < min) min = nums[i];
}
return new int[]{min, max};
}
}
public class RadixSort {
public static void main(String[] args) {
int[] nums = {12, 34, 11, 98, 56, 49, 21, 29, 19};
System.out.println("排序前: " + Arrays.toString(nums));
new RadixSort().radixSort(nums);
System.out.println("排序后: " + Arrays.toString(nums));
}
public void radixSort(int[] nums) {
int[] mm = getMaxMin(nums);
int len = String.valueOf(mm[1]).length();
@SuppressWarnings("unchecked")
LinkedList<Integer>[] digits = new LinkedList[19];
for (int powCount = 0; powCount < len; powCount++) {
for (int num : nums) {
int idx = (int) Math.abs(num / Math.pow(10, powCount) % 10);
idx = num >= 0 ? 9 + idx : 9 - idx;
if (digits[idx] == null) digits[idx] = new LinkedList<>();
digits[idx].add(num);
}
for (int i = 0, j = 0; j < digits.length; j++) {
while (digits[j] != null && !digits[j].isEmpty()) {
nums[i++] = digits[j].removeFirst();
}
}
}
}
private static int[] getMaxMin(int[] nums) {
int max = nums[0], min = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] > max) max = nums[i];
if (nums[i] < min) min = nums[i];
}
return new int[]{min, max};
}
}