冒泡排序
public static void main(String[] args) {
int n = 0;
int[] ns = new int[10];
for (int i = 0; i < ns.length; i++) {
ns[i] = (int) (Math.random() * 100);
}
System.out.println(Arrays.toString(ns));
for (int j = 0; j < ns.length - 1; j++) {
for (int i = 0; i < ns.length - 1 - j; i++) {
if (ns[i] > ns[i + 1]) {
ns[i] = ns[i] ^ ns[i + 1];
ns[i + 1] = ns[i] ^ ns[i + 1];
ns[i] = ns[i] ^ ns[i + 1];
n++;
}
}
}
System.out.println(Arrays.toString(ns) + n);
}
选择排序
public static void main(String[] args) {
int[] ns = new int[10];
for (int i = 0; i < ns.length; i++) {
ns[i] = (int) (Math.random() * 100);
}
System.out.println(Arrays.toString(ns));
for (int j = 0; j < ns.length - 1; j++) {
int minIndex = j;
for (int i = j + 1; i < ns.length; i++) {
if (ns[minIndex] > ns[i]) {
minIndex = i;
}
}
if (minIndex != j) {
ns[minIndex] = ns[minIndex] ^ ns[j];
ns[j] = ns[minIndex] ^ ns[j];
ns[minIndex] = ns[minIndex] ^ ns[j];
}
}
System.out.println(Arrays.toString(ns));
}
快速排序
public static void main(String[] args) {
int[] ns = new int[10];
for (int i = 0; i < ns.length; i++) {
ns[i] = (int) (Math.random() * 100);
}
System.out.println(Arrays.toString(ns));
quickSort(ns);
System.out.println(Arrays.toString(ns));
}
public static void quickSort(int[] values) {
quickSort(values, 0, values.length - 1);
}
private static void quickSort(int[] values, int sta, int end) {
int i = sta;
int j = end;
while (i < j) {
while (i < j) {
if (values[j] < values[i]) {
values[i] = values[i] ^ values[j];
values[j] = values[i] ^ values[j];
values[i] = values[i] ^ values[j];
break;
} else {
j--;
}
}
while (i < j) {
if (values[i] > values[j]) {
values[i] = values[i] ^ values[j];
values[j] = values[i] ^ values[j];
values[i] = values[i] ^ values[j];
break;
} else {
i++;
}
}
}
if (i - sta > 1) {
quickSort(values, sta, i - 1);
}
if (end - i > 1) {
quickSort(values, i + 1, end);
}
}
常用类
String, Math, Random
StringBuffer str1 = new StringBuffer();
str1.append("ads123");
str1.append("456");
str1.append("fgh");
str1.reverse();
System.out.println(str1);
str1.delete(2, str1.length());
System.out.println(str1);
String str = new String();
str = "Hello World";
String str2 = "hello world";
String str3 = " Hello world Hello world ";
System.out.println(str.codePointAt(3));
System.out.println(str.charAt(3));
for (int i = 0; i < str.length(); i++) {
System.out.print(str.charAt(i));
}
System.out.println(str.equals(str2));
System.out.println(str.equalsIgnoreCase(str2));
System.out.println(str.length());
System.out.println(str3);
System.out.println(str3.trim());
System.out.println(str.endsWith("ld"));
System.out.println(str.startsWith("he"));
System.out.println(str3.indexOf("ll"));
System.out.println(str3.lastIndexOf("ll"));
System.out.println(str3.substring(5));
System.out.println(str3.substring(6, 11));
String str4 = "12,22,33,44,52,63,72,83";
String[] str5 = str4.split(",");
for (String s : str5) {
System.out.print(s);
System.out.println(str3.replace("wo", "cc"));
String str6 = "abcABC";
String str7 = "abcb2";
System.out.println(str6.compareTo(str7));
System.out.println(str.toLowerCase());
System.out.println(str.toUpperCase());
System.out.println(Math.E);
System.out.println(Math.PI);
System.out.println(Math.sin(Math.PI/3));
System.out.println(Math.pow(4, 2));
System.out.println(Math.round(1.5));
System.out.println(Math.sqrt(2));
Random random = new Random();
System.out.println(random.nextBoolean());
System.out.println(random.nextInt());
System.out.println(random.nextFloat());
System.out.println(System.currentTimeMillis());