对于大批量英文单词的排序。
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秒。
考虑换成并发处理。效率会更高。