英文单词排序。

对于大批量英文单词的排序。
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class ESort {
	
	static List<String> list = new ArrayList<String>();
	
	static{
		File f = new File("E:/sowpods.txt");
		try {
			InputStreamReader input = new InputStreamReader(new FileInputStream(f));
			BufferedReader read = new BufferedReader(input);
			String s;
			while((s = read.readLine())!=null){
				list.add(s);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*list.add("cabpeece");
		list.add("lavation");
		list.add("caaysome");
		list.add("zygobranchs");
		list.add("hierarchizing");
		list.add("teamworks");*/
	}
	
	public static void main(String[] args) {
		ESort e = new ESort();
		long s = System.currentTimeMillis();
		e.sort(list, 0, list.size()-1);
		long en = System.currentTimeMillis();
		System.out.println(en-s);
//		for (int i = 0; i < list.size(); i++) {
//			System.out.println(list.get(i));
//		}
	}
	
	public void sort(List<String> mlist,int low,int height){
		if(low < height){
			int s = adj(mlist,low,height);
			sort(mlist,low,s-1);
			sort(mlist,s+1,height);
		}
	}
	
	public int adj(List<String> mlist,int low,int height){
		String p = mlist.get(low);
		//一次排序保证左面所有数肯定比右面小
		while(low < height){
			while(low < height && (equals(p,mlist.get(height)) <= 0)){
				height--;
			}
			mlist.set(low, mlist.get(height));
			while(low < height && (equals(p,mlist.get(low)) >= 0)){
				low++;
			}
			mlist.set(height, mlist.get(low));
			mlist.set(low, p);
		}
		return low;
	}
	/**
	 * a>b 1
	 * @param a
	 * @param b
	 * @return
	 */
	public int equals(String a,String b){
		char[] achar = a.toCharArray();
		char[] bchar = b.toCharArray();
		int n = 0;
		int rs = 1;
		while(n < achar.length && n < bchar.length){
			rs = comp(achar[n],bchar[n]);
			if(rs == 0){
				n++;
				continue;
			}
			break;
		}
		return rs;
	}
	
	public int comp(char a, char b){
		if(a == b){
			return 0;
		}else if(a > b){
			return 1;
		}else{
			return -1;
		}
	}
}

sowpods.txt数据为2.6万条。排序执行效率为1.4秒。

考虑换成并发处理。效率会更高。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值