快速排序

	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());
		}
	}
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值