static int[] arr={49, 38, 65, 97, 76, 13, 27, 55, 24, 35,38};
public static void sort(int i,int j){
int begin=i,end=j;
if(end<=begin){
return;
}
int key=arr[begin];
while(end>begin){
while(end>begin){
if(arr[end]<key){
arr[begin] = arr[end];
begin++;
break;
}
end--;
}
while(end>begin){
if(arr[begin]>key){
arr[end]=arr[begin];
end--;
break;
}
begin++;
}
}
arr[begin]=key;
if(begin>i){
sort(i,begin-1);
}
if(j>begin){
sort(begin+1,j);
}
}
自己实现栈(先进后出):
package com.tch.test;
public class MyStack<T> {
/**
* 节点类
*/
class Node<E>{
private T ele;//节点保存的数据
private Node<E> next;//下一个节点
public Node(T ele,Node<E> next){
this.ele = ele;
this.next = next;
}
public T getEle() {
return ele;
}
public Node<E> getNext() {
return next;
}
}
private Node<T> top;
public void push(T ele){
top = new Node<T>(ele,top);//添加元素的时候,将新添加的元素作为top元素,其next元素指向之前的top元素
}
public T pop(){
if(top==null){
return null;
}
T result = top.getEle();
top = top.getNext();
return result;
}
/**
* 是否还有元素
*/
public boolean hasElement(){
return top != null;
}
public static void main(String[] args) {
MyStack<String> stack = new MyStack<String>();
for(String s:"this is a string".split(" ")){
stack.push(s);
}
while(stack.hasElement()){
System.out.println("element : "+stack.pop());
}
}
}