1. 快速排序
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr = {5, 4, 3, 2, 1, 10, 11, 15, 14, 12};
quickSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
public static void quickSort(int[] data, int left, int right) {
int start = left;
int end = right;
if (start > end) {
return;
}
int key = data[left];
while (end > start) {
while (end > start && data[end] >= key) {
end--;
}
while (end > start && data[start] <= key) {
start++;
}
if (start < end) {
swap(data, start, end);
}
}
key = data[start];
swap(data, left, start);
quickSort(data, left, start - 1);
quickSort(data, start + 1, right);
}
private static void swap(int[] data, int i, int j) {
int temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
2. 全排列
import java.util.ArrayList;
import java.util.List;
public class Solution {
List<List<Integer>> result = new ArrayList<List<Integer>>();
public List<List<Integer>> permute(int[] nums) {
fullSort(nums, 0, nums.length - 1);
return result;
}
public void fullSort(int[] nums, int start, int end) {
if (start == end) {
List<Integer> list = new ArrayList<Integer>();
for (int num : nums) {
list.add(num);
}
result.add(list);
return;
}
for (int i = start; i <= end; i++) {
swap(nums, i, start);
fullSort(nums, start + 1, end);
swap(nums, i, start);
}
}
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
private void sort(int[] nums,int start,int end){
if (start==end){
System.out.println();
}
}
}
3. 堆排序
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr={1,2,5,4,7,6,10,56,20,34,23,9};
heapSort(arr);
System.out.println(Arrays.toString(arr));
}
public static void heapSort(int[] arr){
int n=arr.length-1;
for (int i=(n-1)/2;i>=0;i--){
heapBuild(arr,i,n);
}
for (int i = n; i >0 ; i--) {
swap(arr,i);
heapBuild(arr,0,i-1);
}
}
private static void heapBuild(int[] arr, int parent, int length) {
int temp=arr[parent];
for (int i = parent*2+1; i <=length; i=i*2+1) {
if(i<length&&arr[i]<arr[i+1]){
i++;
}
if(temp>=arr[i]){
break;
}
arr[parent]=arr[i];
parent=i;
}
arr[parent]=temp;
}
private static void swap(int[] arr,int i){
int temp=arr[0];
arr[0]=arr[i];
arr[i]=temp;
}
}
4. 二叉搜索树转换为双向链表
public class Solution {
private TreeNode head=null;
private TreeNode realHead=null;
public TreeNode Convert(TreeNode pRootOfTree) {
inorder(pRootOfTree);
return realHead;
}
public void inorder(TreeNode p){
if (p==null){
return;
}
inorder(p.left);
if (head==null){
head=p;
realHead=p;
}else{
head.right=p;
p.left=head;
head=p;
}
inorder(p.right);
}
}
class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
5. 非递归前序、中序遍历二叉树
import org.junit.Test;
import java.util.Stack;
public class BinaryTree {
private TreeNode root=null;
public void visited(TreeNode subTree){
subTree.isVisted=true;
System.out.println("key:"+subTree.key+"--name:"+subTree.data);;
}
public void nonRecPreOrder(TreeNode p){
Stack<TreeNode> stack=new Stack<>();
TreeNode node=p;
while (node!=null||stack.size()>0){
while(node!=null){
visited(node);
stack.push(node);
node=node.leftChild;
}
if (stack.size()>0){
node=stack.pop();
node=node.rightChild;
}
}
}
public void nonRecInOrder(TreeNode p){
Stack<TreeNode> stack=new Stack<>();
TreeNode node=p;
while(node!=null||stack.size()>0){
while(node!=null){
stack.push(node);
node=node.leftChild;
}
if (stack.size()>0){
node=stack.pop();
visited(node);
node=node.rightChild;
}
}
}
public void nonRecPostOrder(TreeNode p){
Stack<TreeNode> stack=new Stack<>();
TreeNode node=p;
while (p!=null){
while (p.leftChild!=null){
stack.push(p);
p=p.leftChild;
}
while (p!=null&&(p.rightChild==null||p.rightChild==node)){
visited(p);
node=p;
if (stack.empty()){
return;
}
p=stack.pop();
}
stack.push(p);
p=p.rightChild;
}
}
}
6. 反转链表
public ListNode ReverseList(ListNode head) {
if (head==null){
return null;
}
ListNode pre=null;
ListNode next=null;
while(head!=null){
next=head.next;
head.next=pre;
pre=head;
head=next;
}
return pre;
}
7. 输入前序遍历和中序遍历重建二叉树
public class Solution {
public TreeNode reConstructBinaryTree(int[] pre, int[] in) {
TreeNode root = constructTree(pre, 0, pre.length - 1, in, 0, in.length - 1);
return root;
}
private TreeNode constructTree(int[] pre, int startPre, int endPre, int[] in, int startIn, int endIn) {
if (startIn>endIn||startPre>endPre){
return null;
}
TreeNode root=new TreeNode(pre[startPre]);
for (int i = startIn; i <= endIn; i++) {
if (in[i] ==pre[startPre]){
root.left=constructTree(pre,startPre+1,startPre+i-startIn,in,startIn,i-1);
root.right=constructTree(pre,i-startIn+startPre+1,endPre,in,i+1,endIn);
break;
}
}
return root;
}
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}