1.排序
public class sortUtils {
//冒泡
public void bubbleSort(int[] array){
int temp = 0;
for(int i=0;i<array.length;i++){
for(int j=0;j<array.length-i-1;j++){
if(array[j]>array[j+1]){
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
//插入
public void insertSort(int[] array){
for(int i = 1;i < array.length;i++){
//待插入的元素
int temp = array[i];
int j;
for(j = i-1;j >= 0;j--){
//元素后移
if(temp<array[j]){
array[j+1] = array[j];
}else{
break;
}
}
array[j+1] = temp;
}
}
//直接选择
public void selectSort(int[] array){
for(int i = 0;i < array.length;i++){
int min = array[i];//最小值
int minIndex = i;//最小值索引
int j;
for(j = i+1;j<array.length;j++){
if(min > array[j]){
min = array[j];
minIndex = j;
}
}
array[minIndex] = array[i];
array[i] = min;
}
}
//快速
public void quickSort(int[] array){
if(array.length>0){
quick_Sort(array,0,array.length-1);
}
}
public void quick_Sort(int[] array,int left,int right){
if(left<right){
int mid = getMiddle(array,left,right);
quick_Sort(array,0,mid-1);
quick_Sort(array,mid+1,right);
}
}
public int getMiddle(int[] array,int left,int right){
int middle = array[left];//基准元素
while(left<right){
while(left<right && array[right]>=middle){
right--;
}
array[left] = array[right];
while(left<right && array[left]<middle){
left++;
}
array[right] = array[left];
}
array[left] = middle;
return left;
}
@Test
public void test(){
int[] array = {1,2,3,8,6,4,7,0};
quickSort(array);
for(int temp : array){
System.out.print(temp+" ");
}
}
}
2.树的遍历
public class Traverse {
private Node a;
public Traverse() {
//创建树
a = new Node("a");
Node b = a.createChildNode("b");
Node c = a.createChildNode("c");
Node d = b.createChildNode("d");
Node e = c.createChildNode("e");
Node f = c.createChildNode("f");
Node g = e.createChildNode("g");
Node h = e.createChildNode("h");
}
/**
* 深度遍历
*/
public void testDFS() {
System.out.println("Testing DFS");
visitDFS(a);
}
public void visitDFS(Node nodeStart) {
for (Node child : nodeStart.children) {
visitDFS(child);
}
System.out.println("DFS: " + nodeStart.nodeName);
}
/**
* 广度遍历
*/
public void testBFS() {
System.out.println("Testing BFS");
visitBFS(a); // visit from the root
}
public void visitBFS(Node nodeStart) {
List<Node> pendingExplore = new ArrayList<Node>();
pendingExplore.add(nodeStart);
while (pendingExplore.size() > 0) {
//先把节点同级节点打出,子节点插入末尾
Node currentNode = pendingExplore.remove(0);
System.out.println("BFS: " + currentNode.nodeName);
pendingExplore.addAll(currentNode.children);
}
}
/**
* 树节点类
* @author Administrator
*
*/
public static class Node {
public Node parent;
public List<Node> children = new ArrayList<Node>();
public String nodeName;
public Node(String nodeName) {
this.nodeName = nodeName;
}
public Node createChildNode(String childNodeName) {
Node child = new Node(childNodeName);
child.parent = this;
children.add(child);
return child;
}
}
/**
* 测试
* @param args
*/
public static void main(String[] args) {
Traverse testTree = new Traverse();
testTree.testBFS();
testTree.testDFS();
}
}
3.查找
public static <T extends Comparable<T>> int binarySearch(T[] x, T key) {
return binarySearch(x, 0, x.length- 1, key);
}
// 使用循环实现的二分查找
public static <T> int binarySearch(T[] x, T key, Comparator<T> comp) {
int low = 0;
int high = x.length - 1;
while (low <= high) {
int mid = (low + high) >>> 1;
int cmp = comp.compare(x[mid], key);
if (cmp < 0) {
low= mid + 1;
}
else if (cmp > 0) {
high= mid - 1;
}
else {
return mid;
}
}
return -1;
}
// 使用递归实现的二分查找
private static<T extends Comparable<T>> int binarySearch(T[] x, int low, int high, T key) {
if(low <= high) {
int mid = low + ((high -low) >> 1);
if(key.compareTo(x[mid])== 0) {
return mid;
}
else if(key.compareTo(x[mid])< 0) {
return binarySearch(x,low, mid - 1, key);
}
else {
return binarySearch(x,mid + 1, high, key);
}
}
return -1;
}
4.单链表反转
package javatest1;
public class javatest1 {
public static void main(String[] args) {
Node head = new Node(0);
Node node1 = new Node(1);
Node node2 = new Node(2);
Node node3 = new Node(3);
head.setNext(node1);
node1.setNext(node2);
node2.setNext(node3);
// 打印反转前的链表
Node h = head;
while (null != h) {
System.out.print(h.getData() + " ");
h = h.getNext();
}
// 调用反转方法
head = Reverse1(head);
System.out.println("\n**************************");
// 打印反转后的结果
while (null != head) {
System.out.print(head.getData() + " ");
head = head.getNext();
}
}
/**
* 递归实现
*/
public static Node Reverse1(Node head) {
// head看作是前一结点,head.getNext()是当前结点,reHead是反转后新链表的头结点
if (head == null || head.getNext() == null) {
return head;// 若为空链或者当前结点在尾结点,则直接还回
}
Node reHead = Reverse1(head.getNext());// 先反转后续节点head.getNext()
head.getNext().setNext(head);// 将当前结点的指针域指向前一结点
head.setNext(null);// 前一结点的指针域令为null;
return reHead;// 反转后新链表的头结点
}
/**
*循环实现
*/
public static Node reverse2(Node head) {
if (head == null)
return head;
Node pre = head;// 上一结点
Node cur = head.getNext();// 当前结点
Node tmp;// 临时结点,用于保存当前结点的指针域(即下一结点)
while (cur != null) {// 当前结点为null,说明位于尾结点
tmp = cur.getNext();
cur.setNext(pre);// 反转指针域的指向
// 指针往下移动
pre = cur;
cur = tmp;
}
// 最后将原链表的头节点的指针域置为null,还回新链表的头结点,即原链表的尾结点
head.setNext(null);
return pre;
}
} class Node { private int Data;// 数据域 private Node Next;// 指针域 public Node(int Data) { // super(); 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; } }