1、冒泡排序
public static void maopao(int[] arr){
for(int i = 0;i<arr.length;i++){
for (int j = i;j<arr.length;j++){
if(arr[i] > arr[j]){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
2、归并排序
public static int[] sort(int[] nums, int low, int high) {
int mid = (low + high) / 2;
if (low < high) {
// 左边
sort(nums, low, mid);
// 右边
sort(nums, mid + 1, high);
// 左右归并
merge(nums, low, mid, high);
}
return nums;
}
/**
* 将数组中low到high位置的数进行排序
* @param nums 待排序数组
* @param low 待排的开始位置
* @param mid 待排中间位置
* @param high 待排结束位置
*/
public static void merge(int[] nums, int low, int mid, int high) {
int[] temp = new int[high - low + 1];
int i = low;// 左指针
int j = mid + 1;// 右指针
int k = 0;
// 把较小的数先移到新数组中
while (i <= mid && j <= high) {
if (nums[i] < nums[j]) {
temp[k++] = nums[i++];
} else {
temp[k++] = nums[j++];
}
}
// 把左边剩余的数移入数组
while (i <= mid) {
temp[k++] = nums[i++];
}
// 把右边边剩余的数移入数组
while (j <= high) {
temp[k++] = nums[j++];
}
// 把新数组中的数覆盖nums数组
for (int k2 = 0; k2 < temp.length; k2++) {
nums[k2 + low] = temp[k2];
}
}
3、快速排序
public static int getM(int[] arr,int low,int hight){
int temp = arr[low];
while(low<hight){
while(low<hight && arr[hight]>=temp ){
hight--;
}
arr[low] = arr[hight];
while(low<hight && arr[low] <= temp){
low++;
}
arr[hight] = arr[low];
}
arr[low] = temp;
return low;
}
public static void quaisu(int[] arr,int low,int hight){
if(low<hight){
int m = getM(arr, low, hight);
quaisu(arr, low, m-1);
quaisu(arr, m+1, hight);
}
}
public static void kuaisu(int [] arr,int low,int hight){
Stack<Integer> stack = new Stack<>();
if(low<hight){
//每次入栈的都是先大的
stack.push(hight);
stack.push(low);
while(!stack.isEmpty()){
Integer l = stack.pop();
Integer h = stack.pop();
int m = getM(arr, l, h);
if(l <m-1){
stack.push(m-1);
stack.push(l);
}
if(hight>m+1){
stack.push(h);
stack.push(m+1);
}
}
}
}
4、二分查找
public static int erfen(int[] arr,int k){
int low = 0;
int hight = arr.length-1;
int m = 0;
while(low<=hight){
m = (low+hight)/2;
if(k > arr[m]){
low = m+1;
}else if(k<arr[m]){
hight = m-1;
}else {
return m;
}
}
return -1;
}
5、二叉树遍历
public class Node {
private int data;
private Node l;
private Node r;
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getL() {
return l;
}
public void setL(Node l) {
this.l = l;
}
public Node getR() {
return r;
}
public void setR(Node r) {
this.r = r;
}
public Node(int data, Node l, Node r) {
this.data = data;
this.l = l;
this.r = r;
}
public Node() {
}
}
public class bianli {
//先序遍历递归
public static void xianxu1(Node node){
if(node != null){
System.out.print(node.getData()+"\n");
xianxu1(node.getL());
xianxu1(node.getR());
}
}
//先序遍历非递归
public static void xianxu2(Node node){
Stack<Node> stack = new Stack<>();
while (true){
while(node!=null){
System.out.print(node.getData()+"\n");
stack.push(node);
node = node.getL();
}
if(stack.isEmpty()){break;}
node = stack.pop();
node = node.getR();
}
}
//中序遍历递归
public static void zhongxu(Node node ){
if(node != null){
zhongxu(node.getL());
System.out.print(node.getData()+"\n");
zhongxu(node.getR());
}
}
//中序遍历非递归
public static void zhongxu2(Node node){
Stack<Node> stack = new Stack<>();
while(true){
while(node!=null){
stack.push(node);
node = node.getL();
System.out.print(node.getData()+"\n");
}
if(stack.isEmpty()){
break;
}
node = stack.pop();
node.getR();
}
}
}
6、链表反转
public class Node {
private int data;
private Node next;
public Node(int data){
this.data = data;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
public class Node2 {
private int data;
private Node2 up;
private Node2 down;
public Node2(int data) {
this.data = data;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node2 getUp() {
return up;
}
public void setUp(Node2 up) {
this.up = up;
}
public Node2 getDown() {
return down;
}
public void setDown(Node2 down) {
this.down = down;
}
}
public class NodeRe {
public static void main(String[] args){
Node node1 = new Node(1);
Node node2 = new Node(2);
Node node3 = new Node(3);
Node node4 = new Node(4);
Node node5 = new Node(5);
node1.setNext(node2);
node2.setNext(node3);
node3.setNext(node4);
node4.setNext(node5);
Node re = re(node1);
while(re!=null){
System.out.println(re.getData());
re = re.getNext();
}
}
public static Node re(Node node){
if(node == null || node.getNext() == null){
return node;
}
Node re = re(node.getNext());
node.getNext().setNext(node);
node.setNext(null);
return re;
}
}
public static Node2 re(Node2 node){
if(node==null || node.getDown()==null){
node.setDown(node.getUp());
node.setUp(null);
return node;
}
Node2 re = re(node.getDown());
Node2 up = node.getUp();
node.setUp(node.getDown());
node.setDown(up);
return re;
}
}