算法/数据结构

就用java来搞基础算法吧。。

第1发,插入排序:

public void insertSort(){
  long[] array=new long[100];            //初始化数据数组
  int in,out;
  for(out=1;out<array.length;out++){
     long temp=a[out];
     in=out;
     while(in>0&& a[in-1]>=temp){
        a[in]=a[in-1];
        --in;
     }
     a[in]=temp;
  }
}

第2发,递归循环:(把一个数组里面的数组合全部列出)

import java.util.*;
import java.io.*;

public class Test{
	public static void main(String[] args){
		String[] array=new String[]{"1","2","3","4"};
		listAll(Arrays.asList(array),"");
	}
	public static void listAll(List candidate, String prefix){
//		if(candidate.isEmpty()){
			System.out.println(candidate.size()+" | "+prefix);
//		}
		for(int i=0;i<candidate.size();i++){
			List temp=new LinkedList(candidate);
			listAll(temp, prefix+temp.remove(i));
		}
	}
}

第3发,归并排序

public class Merge {
     //递归排列
	public void recMergeSort(long[] workSpace,int lowwerBound,int upperBound){
		if(lowwerBound==upperBound){	return;	}
		else{
			int mid=(lowwerBound+upperBound)/2;        //分割数组
 			recMergeSort(workSpace,lowwerBound,mid);
			recMergeSort(workSpace,mid+1,upperBound);
			merge(workSpace,lowwerBound,mid+1,upperBound);
		}
	}
	//归并算法实现
	public void merge(long[] workSpace,int lowPtr,int higPtr,int upperBound){
		int j=0;
		int lowwerBound=lowPtr;
		int mid=higPtr-1;
		int n=upperBound-lowwerBound+1;    //数组长度 lowwerBound数组首部 upperBound数组尾部
		
		while(lowPtr<=mid && higPtr<=upperBound)
			if(theArray[lowPtr] < theArray[higPtr]){
				workSpace[j++]=theArray[lowPtr++];
			}else{
				workSpace[j++]=theArray[higPtr++];
			}
		
		while(lowPtr <= mid){
			workSpace[j++]=theArray[lowPtr++];
		}
		while(higPtr <= upperBound){
			workSpace[j++]=theArray[higPtr++];
		}
		for(j=0;j<n;j++){
			theArray[lowwerBound+j]=workSpace[j];
		}	
	}
}

第4发,栈:

public class StackX{
	public int maxSize;
	public char[] stackArray;
	public int top;

	public Stackx(int s){
              maxSize=s;
		stackArray=new char[maxSize];
		top=-1;
	}

	public void push(char temp){
		stackArray[++top]=temp;
	}

	public char peek(){
		return stackArray[top];
	}

	public char pop(){
		return stackArray[top--];
	}

	public boolean isEmpty(){
		return (top==-1);
	}

	public boolean isFull(){
		return (top==maxSize-1);
	}
}

     栈实例1. 单词反转:   

public class Reverse{
	public String input;
	public int maxSize=10;
	public StackX Stackx=new StackX(maxSize);

        public Reverse(String in){
           input=in;
        }

	public String doRev(){
		for(int i=0;i<input.length();i++){
			Stackx.push(input.charAt(i));
		}
		String output="";
		while(!Stackx.isEmpty()){
			output=output+Stackx.pop();
		}
		return output;
	}
}

     栈实例2. 分割符匹配:

public class BracketChecker {

	private String input;
	
	public BracketChecker(String in){
		input=in;
	}
	
	public void check(){
		int stackSize=input.length();
		Stack theStack=new Stack(stackSize);
		
		for(int j=0;j<input.length();j++){
			char ch=input.charAt(j);
			switch(ch){
			case '{':
			case '[':
			case '(':theStack.push(ch);break;
			case '}':
			case ']':
			case ')':
				if(!theStack.isEmpty()){
					char chx=theStack.pop();
					if((ch=='}'&&chx!='{')||(ch==']'&&chx!='[')||(ch==')'&&chx!='(')) {
					      System.out.println("Error "+ch+" at "+j);
					}
				}else{
					System.out.println("no Matched '"+ch+"' at "+j);
				}break;
			default:break;
			}
		}
		if(!theStack.isEmpty()){
			System.out.println("Errors missing");
		}
	}
}

    栈实例3. 中缀表达式转换成后缀表达式

public class InToPost {

	private Stack theStack;
	private String input;
	private String output="";
	
	public InToPost(String in){
		input=in; int stackSize=input.length(); theStack=new StackX(stackSize);
	}
	
	public void gotOper(char opThis, int prec1){
		while(!theStack.isEmpty()){
			char opTop=theStack.pop();
			if(opTop=='('){
				theStack.push(opTop);break;
			}else{
				int prec2;
				if(opTop=='+' || opTop=='-')
					prec2=1;
				else 
					prec2=2;
				if(prec2 < prec1){
					theStack.push(opTop);break;
				}else {
					output=output+opTop;
				}
			}
		}
		theStack.push(opThis);
	}
	
	public void gotParen(char ch){
		while(!theStack.isEmpty()){
			char chx=theStack.pop();
			if(chx=='(')
				break;
			else 
				output=output+chx;			
		}
	}
	
	public String doTrans(){
		for(int j=0;j<input.length();j++){
			char ch=input.charAt(j);
			switch(ch){
			case '+':
			case '-':
				gotOper(ch,1);break;
			case '*':
			case '/':
				gotOper(ch, 2);break;
			case '(':
				theStack.push(ch);break;
			case ')':
				gotParen(ch);break;
			default:
				output=output+ch;break;
			}
		}
		while(!theStack.isEmpty()){
			output=output+theStack.pop();
		}
		return output;
	}
	
}

第5发,队列:

优先级队列(数据按一定顺序排列插入队列)

public class PriorityQueue {

	private int maxSize;
	private long[] queArray;
	private int nElems;
	
	public PriorityQueue(int s) {
		maxSize=s;
		queArray=new long[maxSize];
		nElems=0;
	}
	
	public void insert(long item){
		int j;
		if(nElems==0){
			queArray[nElems++]=item;
		}else{
			for(j=nElems-1;j>=0;j--){                 //从队列尾部开始
				if(item>queArray[j]){              //如果temp大于已有数据,则移动队列数据元素
					queArray[j+1]=queArray[j];
				}else{                             //如果不于,则结束移动队列数据
					break;
				}
			}
			queArray[j+1]=item;                        //插入数据
			nElems++;
		}
	}
	
	public long remove(){ return queArray[--nElems]; }
	
	public long peekMin(){ return queArray[nElems-1]; }

	public boolean isEmpty(){ return (nElems==0); }
	
	public boolean isFull(){ return (nElems==maxSize); }
}

第六发,二叉树:

二叉树生成,查询,删除.


import java.util.Stack;

class Node{
        public int iData;
	public double dData;
	public Node leftChild;
	public Node rightChild;
	
	public void displayNode(){
		System.out.print("{"+iData+", "+dData+"} ");
	}
}
public class SimpleTree {

	private Node root;
	
	public SimpleTree(){	root=null;	}
	
	public Node find(int key){
		Node current=root;
		while(current.iData != key){
			if(key<current.iData)
				current=current.leftChild; 
			else	
				current=current.rightChild;
			if(current==null)
				return null;
		}
		return current;
	}
	
	public void insert(int id,double dd){
		Node newNode=new Node();
		newNode.iData=id;
		newNode.dData=dd;
		if(root==null)	
			root=newNode;
		else{
			Node current = root;
			Node parent;
			while(true){
				parent = current;
				if(id < current.iData){
					current = current.leftChild;
					if(current == null){
						parent.leftChild=newNode;
						return;
					}
				}else{
					current=current.rightChild;
					if(current == null){
						parent.rightChild = newNode;
						return;
					}
				}
			}
		}
	}
	
	public boolean delete(int key){
		Node current = root;
		Node parent = root;
		boolean isLeftChild = true;
		
		while(current.iData != key){
			parent = current;
			if(key < current.iData){
				isLeftChild=true;
				current=current.leftChild;
			}else{
				isLeftChild=false;
				current=current.rightChild;
			}
			if(current == null){
				return false;
			}
		}
	
		if(current.leftChild == null && current.rightChild == null){	                        //no children 
			if(current == root)	
				root=null;
			else if(isLeftChild) 
				parent.leftChild = null;
			else 
				parent.rightChild = null;
		}else if(current.rightChild == null){							//no right children
			if(current == root)	
				root = current.leftChild;
			else if(isLeftChild)	
				parent.leftChild = current.leftChild;
			else	
				parent.rightChild = current.leftChild;
		}else if(current.leftChild == null){							//no left children
			if(current == root)	
				root=current.rightChild;
			else if(isLeftChild)	
				parent.leftChild=current.rightChild;
			else	
				parent.rightChild=current.rightChild;
		}else{											//two children, replace with inorder successor
			Node successor=getSuccessor(current);
			if(current==root)	
				root=successor;
			else if(isLeftChild)	
				parent.leftChild=successor;
			else 
				parent.rightChild=successor;
			successor.leftChild=current.leftChild;
		}
		return true;
	}
	
	public Node getSuccessor(Node delNode){
		Node successorParent=delNode;
		Node successor=delNode;
		Node current=delNode.rightChild;
		while(current != null){
			successorParent=successor;
			successor=current;
			current=current.leftChild;
		}
		if(successor != delNode.rightChild){
			successorParent.leftChild=successor.rightChild;
			successor.rightChild=delNode.rightChild;
		}
		return successor;
	}
	
	public void traverse(int traverseType){
		switch(traverseType){
			case 1:System.out.print("\nPreOrder: ");preOrder(root);break;
			case 2:System.out.print("\nInrder: ");inOrder(root);break;
			case 3:System.out.print("\nPostOrder: ");postOrder(root);break;
		}
	}
	//前序遍历
	public void preOrder(Node localRoot){
		if(localRoot!=null){
			System.out.print(localRoot.iData+" ");
			preOrder(localRoot.leftChild);
			preOrder(localRoot.rightChild);
		}
	}
	//中序遍历
	public void inOrder(Node localRoot){
		if(localRoot!=null){
			inOrder(localRoot.leftChild);
			System.out.print(localRoot.iData+" ");
			inOrder(localRoot.rightChild);
		}
	}
	//后序遍历
	public void postOrder(Node localRoot){
		if(localRoot!=null){
			postOrder(localRoot.leftChild);
			postOrder(localRoot.rightChild);
			System.out.print(localRoot.iData+" ");
		}
	}
}



转载于:https://my.oschina.net/igeeker/blog/163537

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值