冒泡
public class BubbleSort {
public static int[] bubbleSort(int[] array){
int len=array.length;
if(len==0||array==null)
return null;
int temp;
for(int i=0;i<len-1;i++){
for(int j=0;j<len-i-1;j++){
if(array[j]>array[j+1]){
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
return array;
}
}
插入
public class InsertionSort {
public static int[] insertionSort(int[] array) {
int len = array.length;
if (len == 0 || array == null)
return null;
int temp, i, j;
for (i = 1; i < len; i++) {
if (array[i] < array[i - 1]) {
temp = array[i];
for (j = i-1; j >= 0 && array[j] > temp; --j) {
array[j+1] = array[j];
}
array[j+1] = temp;
}
}
return array;
}
归并
public class MergeSort {
public static void merge(int[] arr, int low, int mid, int high) {
int[] B = new int[arr.length];
int i, j, k;
for (k = low; k <= high; k++) {
B[k] = arr[k];
}
for (i = low, j = mid + 1, k = i; i <= mid && j <= high; k++) {
if (B[i] < B[j]) {
arr[k] = B[i++];
} else {
arr[k] = B[j++];
}
}
while (i <= mid) arr[k++] = B[i++];
while (j <= high) arr[k++] = B[j++];
}
public static void mergeSort(int[] arr, int low, int high) {
if (low < high) {
int mid = (low + high) / 2;
mergeSort(arr, low, mid);
mergeSort(arr, mid + 1, high);
merge(arr, low, mid, high);
}
}
快排
public static int partition(int[] arr, int low, int high) {
int pivot = arr[low];
while (low < high) {
while (low < high && arr[high] >= pivot)
high--;
arr[low] = arr[high];
while (low < high && arr[low] <= pivot)
low++;
arr[high] = arr[low];
}
arr[low] = pivot;
return low;
}
public static void quickSort(int[] arr, int low, int high) {
if(low<high){
int index=partition(arr, low, high);
quickSort(arr, low, index-1);
quickSort(arr, index+1,high);
}
}
堆排序
public static void heapSort(int[] a) {
int temp;
buildMaxHeap(a);
for (int i = 0; i < a.length; i++) {
temp = a[0];
a[0] = a[a.length - 1 - i];
a[a.length - 1 - i] = temp;
headAdjust(a, 0, a.length - 1 - i);
}
}
public static void buildMaxHeap(int[] a) {
int len = a.length;
for (int i = (len - 1) / 2; i >= 0; i--) {
headAdjust(a, i, len);
}
}
static void headAdjust(int[] a, int k, int len) {
int temp = a[k];
int s = 2 * k + 1;
while (s < len) {
if (s + 1 < len && a[s] < a[s + 1]) {
s = s + 1;
}
if (a[s] > a[k]) {
a[k] = a[s];
k = s;
s = 2 * k + 1;
} else {
break;
}
a[k] = temp;
}
}
死锁
public static void main(String[] args) {
CreatThread creatThread = new CreatThread();
Thread t1 = new Thread(creatThread, "窗口1");
Thread t2 = new Thread(creatThread, "窗口2");
Thread t3 = new Thread(creatThread, "窗口3");
t1.start();
t2.start();
t3.start();
}
static class CreatThread implements Runnable {
private int ticket = 10;
Object lock = new Object();
@Override
public void run() {
String name = Thread.currentThread().getName();
while (true) {
if ("窗口1".equals(name)) {
synchronized (lock) {
sell(name);
}
}
sell(name);
if (ticket <= 0)
break;
}
}
private synchronized void sell(String name) {
try {
Thread.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
synchronized (lock) {
if (ticket > 0) {
System.out.println(name + ":" + ticket);
ticket--;
}
}
}
}
}
结束进程
private static boolean flag=true;
public static void main(String[] args)throws InterruptedException{
Thread t1=new Thread(new Runnable() {
@Override
public void run() {
while(flag){
System.out.println("线程开始执行");
try {
Thread.sleep(1001);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t1.start();
Thread.sleep(10001);
flag=false;
System.out.println("线程结束");
}
BFS
public class BFS {
public void BFSWithQueue(TreeNode node){
Queue<TreeNode> queue=new LinkedList<>();
if (node!=null)
queue.offer(node);
while (!queue.isEmpty()){
TreeNode treeNode = queue.poll();
System.out.println(treeNode.val);
if(treeNode.left!=null)
queue.offer(treeNode.left);
if(treeNode.right!=null)
queue.offer(treeNode.right);
}
}
}
链表环
public class EntryNodeOfLoop {
public ListNode entryNodeOfLoop(ListNode pNode) {
ListNode low = pNode;
ListNode fast = pNode;
while (fast != null && fast.next != null) {
low = low.next;
fast = fast.next.next;
if (low == fast)
break;
}
if (fast == null || fast.next == null)
return null;
low = pNode;
while (low != fast) {
low = low.next;
fast = fast.next;
}
return low;
}
}