我想对一个由整数组成的数组进行排序,这些整数是map的索引>,比较取决于Map中与整数索引相对应的字符串列表, 那么考虑到时间和内存,如果数组很大,那么哪种排序算法是最好的呢?
public class AuthorSorting {
public static void main(String args[]) throws Exception{
int index = 0;
int array[] = {0, 1 ,2 ,3 ,4 ,5 ,6};
Map<Integer,List<String>> myMap = new LinkedHashMap<>();
myMap.put(index++, Arrays.asList("Abhijit Bhaduri","Kewal Dheer","Hemant Shesh"));
myMap.put(index++, Arrays.asList("Kewal Dheer","Kiran Desai","Nandini Sahu"));
myMap.put(index++, Arrays.asList("Abhijit Bhaduri"));
myMap.put(index++, Arrays.asList("Kewal Dheer","Kiran Desai","Mohan Deep"));
myMap.put(index++, Arrays.asList("Kewal Dheer"));
myMap.put(index++, Arrays.asList("Kiran Desai","Mohan Deep","Pawan Karan"));
myMap.put(index++, Arrays.asList("Hemant Shesh"));
System.out.println("*****input*****\n");
for(Map.Entry<Integer, List<String>> entry: myMap.entrySet()) {
System.out.println(entry.getKey()+" "+entry.getValue().toString());
}
int temp;
for(int i = 1 ; i < array.length;i++) {
for(int j = 0; j < array.length-i;j++) {
if(compare(myMap.get(array[j]),myMap.get(array[j+1]))<0) {//swapping only if compare() returns -1
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
System.out.println("\n******result*******\n");
for(int number:array) {
System.out.println(number+" "+myMap.get(number).toString());
}
}
static int compare(List<String> listOne,List<String> listTwo) {
int i;
for(i = 0 ; i<listOne.size()&&i<listTwo.size();i++) {
if(listOne.get(i).compareToIgnoreCase(listTwo.get(i))>0) {
return -1;// listOne's string is lexically greater
}
else if(listOne.get(i).compareToIgnoreCase(listTwo.get(i))<0) {
return 1;// listTwo's string is lexically greater
}
}
if(listOne.size()==listTwo.size())
return 0;
if(i==listOne.size())
return 1;
if(i==listTwo.size())
return -1;
return 0;
}
}
*****input*****
0 [Abhijit Bhaduri, Kewal Dheer, Hemant Shesh]
1 [Kewal Dheer, Kiran Desai, Nandini Sahu]
2 [Abhijit Bhaduri]
3 [Kewal Dheer, Kiran Desai, Mohan Deep]
4 [Kewal Dheer]
5 [Kiran Desai, Mohan Deep, Pawan Karan]
6 [Hemant Shesh]
******result*******
2 [Abhijit Bhaduri]
0 [Abhijit Bhaduri, Kewal Dheer, Hemant Shesh]
6 [Hemant Shesh]
4 [Kewal Dheer]
3 [Kewal Dheer, Kiran Desai, Mohan Deep]
1 [Kewal Dheer, Kiran Desai, Nandini Sahu]
5 [Kiran Desai, Mohan Deep, Pawan Karan]
当前,我已经使用了冒泡排序,还不确定是否可以在大型数组中使用