算法学习(3)序列的划分

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

public class Division_sequence {

	public static void main(String[] args) {
		Integer a[]= {1,2,5,8,9,0,3,4,6,7};
		String b[]= {"AoMen","BeiJing","ChongQing","TianJin","XiangGang","ShangHai"};
		Double c[]= {0.5,3.7,6.3,8.5,9.2,1.7,2.3,4.1,5.9,7.4};
		
		ArrayList<Integer>  A=new ArrayList<Integer>();
		for (int i = 0;  i< a.length; i++) {
			A.add(a[i]);
		}
		
		Vector<String> B=new Vector<String>();
		for (int i = 0;  i< b.length; i++) {
			B.add(b[i]);
		}
		
		LinkedList<Double> C=new LinkedList<Double>();
		for (int i = 0;  i< c.length; i++) {
			C.add(c[i]);
		}
		
		Integer j=LinearList.partition((List)A,0,a.length-1,new Greater());
		System.out.println(A);
		System.out.println(j);
		
		 j=LinearList.partition((List)B,0,b.length-1,new Less());
		System.out.println(B);
		System.out.println(j);
		
		j=LinearList.partition((List)C,0,c.length-1,new Greater());
		System.out.println(C);
		System.out.println(j);
		
	}

}
class LinearList{
	public static int partition(List<Comparable> a,int p,int r,Comparator comp) {
		Comparable x= a.get(r);
		int i=p-1;
		for(int j=p;j<r;j++) {
			if(comp.compare(a.get(j), x)<=0) {
				i++;
				Collections.swap(a, i, j);
			}
			
		}
		Collections.swap(a, i+1, r);
		return i+1;
		
	}

}
class Greater implements Comparator<Comparable>{

	@Override
	public int compare(Comparable x, Comparable y) {
		// TODO Auto-generated method stub
		return x.compareTo(y);
	}
	
}
class Less implements Comparator<Comparable>{

	@Override
	public int compare(Comparable x, Comparable y) {
		// TODO Auto-generated method stub
		return y.compareTo(x);
	}
	
}                                                                                             ————许

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值