温馨提示:距离2024年结束还剩18天,剩余约为4.92%...
一共八大数据结构分类
private static void shell(int[] a) {
int n = a.length;
for (int gap = n / 2; gap > 0; gap /= 2) {
// i 代表待插入元素的索引
for (int i = gap; i < n; i++) {
int t = a[i]; // 代表待插入的元素值
int j = i;
while (j >= gap) {
// 每次与上一个间隙为 gap 的元素进行插入排序
if (t < a[j - gap]) { // j-gap 是上一个元素索引,如果 > t,后移
a[j] = a[j - gap];
j -= gap;
} else { // 如果 j-1 已经 <= t, 则 j 就是插入位置
break;
}
}
a[j] = t;
System.out.println(Arrays.toString(a) + " gap:" + gap);
}
}
}
// 修改了代码与希尔排序一致
public static void insert(int[] a) {
// i 代表待插入元素的索引
for (int i = 1; i < a.length; i++) {
int t = a[i]; // 代表待插入的元素值
int j = i;
System.out.println(j);
while (j >= 1) {
if (t < a[j - 1]) { // j-1 是上一个元素索引,如果 > t,后移
a[j] = a[j - 1];
j--;
} else { // 如果 j-1 已经 <= t, 则 j 就是插入位置
break;
}
}
a[j] = t;
System.out.println(Arrays.toString(a) + " " + j);
}
}
算法实现:
public static void selection(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
// i 代表每轮选择最小元素要交换到的目标索引
int s = i; // 代表最小元素的索引
for (int j = s + 1; j < a.length; j++) {
if (a[s] > a[j]) { // j 元素比 s 元素还要小, 更新 s
s = j;
}
}
if (s != i) {
swap(a, s, i);
}
System.out.println(Arrays.toString(a));
}
}
public static void swap(int[] array, int i, int j) {
int t = array[i];
array[i] = array[j];
array[j] = t;
}
与冒泡排序比较
稳定排序与不稳定排序
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StableVsUnstable {
public static void main(String[] args) {
System.out.println("=================不稳定================");
Card[] cards = getStaticCards();
System.out.println(Arrays.toString(cards));
selection(cards, Comparator.comparingInt((Card a) -> a.sharpOrder).reversed());
System.out.println(Arrays.toString(cards));
selection(cards, Comparator.comparingInt((Card a) -> a.numberOrder).reversed());
System.out.println(Arrays.toString(cards));
System.out.println("=================稳定=================");
cards = getStaticCards();
System.out.println(Arrays.toString(cards));
bubble(cards, Comparator.comparingInt((Card a) -> a.sharpOrder).reversed());
System.out.println(Arrays.toString(cards));
bubble(cards, Comparator.comparingInt((Card a) -> a.numberOrder).reversed());
System.out.println(Arrays.toString(cards));
}
public static void bubble(Card[] a, Comparator<Card> comparator) {
int n = a.length - 1;
while (true) {
int last = 0; // 表示最后一次交换索引位置
for (int i = 0; i < n; i++) {
if (comparator.compare(a[i], a[i + 1]) > 0) {
swap(a, i, i + 1);
last = i;
}
}
n = last;
if (n == 0) {
break;
}
}
}
private static void selection(Card[] a, Comparator<Card> comparator) {
for (int i = 0; i < a.length - 1; i++) {
// i 代表每轮选择最小元素要交换到的目标索引
int s = i; // 代表最小元素的索引
for (int j = s + 1; j < a.length; j++) {
if (comparator.compare(a[s], a[j]) > 0) {
s = j;
}
}
if (s != i) {
swap(a, s, i);
}
}
}
public static void swap(Card[] a, int i, int j) {
Card t = a[i];
a[i] = a[j];
a[j] = t;
}
enum Sharp {
diamond, club, heart, spade, black, red
}
static Card[] getStaticCards() {
List<Card> list = new ArrayList<>();
Card[] copy = Arrays.copyOfRange(Card.cards, 2, 13 * 4 + 2);
list.add(copy[7]);
list.add(copy[12]);
list.add(copy[12+13]);
list.add(copy[10]);
list.add(copy[9]);
list.add(copy[9+13]);
return list.toArray(new Card[0]);
}
static Map<String, Integer> map = Map.ofEntries(
Map.entry("RJ", 16),
Map.entry("BJ", 15),
Map.entry("A", 14),
Map.entry("K", 13),
Map.entry("Q", 12),
Map.entry("J", 11),
Map.entry("10", 10),
Map.entry("9", 9),
Map.entry("8", 8),
Map.entry("7", 7),
Map.entry("6", 6),
Map.entry("5", 5),
Map.entry("4", 4),
Map.entry("3", 3),
Map.entry("2", 2)
);
static class Card {
private Sharp sharp;
private final String number;
private final int numberOrder;
private final int sharpOrder;
public Card(Sharp sharp, String number) {
this.sharp = sharp;
this.number = number;
this.numberOrder = map.get(number);
this.sharpOrder = sharp.ordinal();
}
private static final Card[] cards;
static {
cards = new Card[54];
Sharp[] sharps = {Sharp.spade, Sharp.heart, Sharp.club, Sharp.diamond};
String[] numbers = {"A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2"};
int idx = 2;
for (Sharp sharp : sharps) {
for (String number : numbers) {
cards[idx++] = new Card(sharp, number);
}
}
cards[0] = new Card(Sharp.red, "RJ");
cards[1] = new Card(Sharp.black, "BJ");
}
@Override
public String toString() {
return switch (sharp) {
case heart -> "[\033[31m" + "♥" + number + "\033[0m]";
case diamond -> "[\033[31m" + "♦" + number + "\033[0m]";
case spade -> "[\033[30m" + "♠" + number + "\033[0m]";
case club -> "[\033[30m" + "♣" + number + "\033[0m]";
case red -> "[\033[31m" + "\uD83C\uDFAD" + "\033[0m]";
case black -> "[\033[30m" + "\uD83C\uDFAD" + "\033[0m]";
};
}
}
}
都是先按照花色排序(♠♥♣♦),再按照数字排序(AKQJ...)
[[♠7], [♠2], [♠4], [♠5], [♥2], [♥5]]
[[♠7], [♠5], [♥5], [♠4], [♥2], [♠2]]
原来 ♠2 在前 ♥2 在后,按数字再排后,他俩的位置变了
[[♠7], [♠2], [♠4], [♠5], [♥2], [♥5]]
[[♠7], [♠5], [♥5], [♠4], [♠2], [♥2]]
算法实现:
//工具类
public class Utils {
public static void swap(int[] array, int i, int j) {
int t = array[i];
array[i] = array[j];
array[j] = t;
}
}
public static void bubble(int[] a) {
for (int j = 0; j < a.length - 1; j++) {
// 一轮冒泡
boolean swapped = false; // 是否发生了交换
for (int i = 0; i < a.length - 1 - j; i++) {
System.out.println("比较次数" + i);
if (a[i] > a[i + 1]) {
// 交换元素
Utils.swap(a, i, i + 1);
swapped = true;
}
}
System.out.println("第" + j + "轮冒泡"
+ Arrays.toString(a));
if (!swapped) {
break;
}
}
}
进一步优化
public static void bubble_v2(int[] a) {
int n = a.length - 1;
while (true) {
int last = 0; // 表示最后一次交换索引位置
for (int i = 0; i < n; i++) {
System.out.println("比较次数" + i);
if (a[i] > a[i + 1]) {
Utils.swap(a, i, i + 1);
last = i;
}
}
n = last;
System.out.println("第轮冒泡"
+ Arrays.toString(a));
if (n == 0) {
break;
}
}
}
算法实现:
public static int binarySearch(int[] a, int t) {
int l = 0, r = a.length - 1, m;
while (l <= r) {
m = (l + r) / 2;
if (a[m] == t) {
return m;
} else if (a[m] > t) {
r = m - 1;
} else {
l = m + 1;
}
}
return -1;
}
测试代码
public static void main(String[] args) {
int[] array = {1, 5, 8, 11, 19, 22, 31, 35, 40, 45, 48, 49, 50};
int target = 47;
int idx = binarySearch(array, target);
System.out.println(idx);
}
解决整数溢出问题
当 l 和 r 都较大时,l + r 有可能超过整数范围,造成运算错误,解决方法有两种:
int m = l + (r - l) / 2;
还有一种是:
int m = (l + r) >>> 1;