排序算法

This commit is contained in:
sheep
2025-10-02 13:48:50 +08:00
commit c4da9d7c4b
5 changed files with 205 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
package org.sheep;
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
public static void main(String[] args) {
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
// to see how IntelliJ IDEA suggests fixing it.
System.out.printf("Hello and welcome!");
for (int i = 1; i <= 5; i++) {
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
System.out.println("i = " + i);
}
}
}

View File

@@ -0,0 +1,35 @@
package org.sheep;
import java.util.Stack;
public class MinStack {
private Stack<Integer> stack;
private Stack<Integer> minStack;
public MinStack() {
stack = new Stack<>();
minStack = new Stack<>();
}
public void push(int x) {
stack.push(x);
if (minStack.empty() || x < minStack.peek()) {
minStack.push(x);
} else {
minStack.push(minStack.peek());
}
}
public void pop() {
stack.pop();
minStack.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return minStack.peek();
}
}

View File

@@ -0,0 +1,38 @@
package org.sheep.alg;
/**
* 进制转换
* @author sheep
*/
public class ConversionOfNumber {
public static void main(String[] args) {
// 定义二进制数
int a = -0b1001;
System.out.println(a);
// 定义16进制数
int b = 0x10;
System.out.println(b);
// 取反
System.out.println(~a + 1);
// | & ^
int c = 0b0001010;
int d = 0b0000110;
printBinary(c | d);
printBinary(c & d);
printBinary(c ^ d);
int e = -3;
System.out.println(Integer.toBinaryString(e >> 2));
System.out.println(Integer.toBinaryString(e >>> 2));
}
/**
* 十进制转换二进制
* @param num
*/
public static void printBinary(int num) {
for (int i = 31; i >= 0; i--) {
System.out.print((num & (1 << i)) == 0 ? "0" : "1" );
}
System.out.println();
}
}

View File

@@ -0,0 +1,71 @@
package org.sheep.alg;
import java.util.Arrays;
/**
* 排序
* @author sheep
*/
public class Sort {
int[] arr = {5,3,4,1,2};
public static void main(String[] args) {
Sort sort = new Sort();
sort.insertSort(sort.arr);
System.out.println(Arrays.toString(sort.arr));
}
/**
* 交换数组中指定位置的两个数
* @param arr
* @param i
* @param j
*/
private void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/**
* 选择排序
*/
public void selectSort(int[] arr) {
if (arr == null || arr.length < 2) { return; }
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
swap(arr, i, j);
}
}
}
}
/**
* 冒泡排序
*/
public void bubbleSort(int[] arr) {
if (arr == null || arr.length < 2) { return; }
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1);
}
}
}
}
/**
* 插入排序
*/
public void insertSort(int[] arr) {
if (arr == null || arr.length < 2) { return; }
for (int i = 1; i < arr.length; i++) {
for (int j = i; j > 0; j--) {
if (arr[j] < arr[j - 1]) {
swap(arr, j, j - 1);
}
}
}
}
}

View File

@@ -0,0 +1,44 @@
package org.sheep.alg;
import java.util.Arrays;
/**
* 测试
* @author sheep
*/
public class Test {
private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String[] args) {
int[] arr = {5,3,4,1,2};
// for (int i = 0; i < arr.length; i++) {
// for (int j = 0; j < arr.length - i -1; j++) {
// if (arr[j] > arr[j + 1]) {
// swap(arr, j, j + 1);
// }
// }
// }
// for (int i = 0; i < arr.length; i++) {
// for (int j = i + 1; j < arr.length; j++) {
// if (arr[j] < arr[i]) {
// swap(arr, i, j);
// }
// }
// }
// for (int i = 1; i < arr.length; i++) {
// for (int j = i; j > 0; j--) {
// if (arr[j] < arr[j - 1]) {
// swap(arr, j, j - 1);
// }
// }
// }
System.out.println(Arrays.toString(arr));
}
}