问题是:
假设我添加一个数,相应的给Vector也添加一个标志数。
例如:我先添加1,3两个数,vector元素的顺序也是1,3
然后我在1,3中间添加2,vector元素的顺序是1,3,2
我怎么去调整这个顺序啊,使vector的顺序为1,2,3
假设我添加一个数,相应的给Vector也添加一个标志数。
例如:我先添加1,3两个数,vector元素的顺序也是1,3
然后我在1,3中间添加2,vector元素的顺序是1,3,2
我怎么去调整这个顺序啊,使vector的顺序为1,2,3
/**
* 可排序类型
* @author jiangxu
* @version 1.0, Jan 25, 2007
* @since 1.5
*/
public interface SortableType {
/**
* 与st比较
*
* @param st
* @return 如果相等返回0; 如果大于st,返回大于0的值;如果小于st返回小于0的值
*/
int compareTo(SortableType st);
}/**
* 排序
* @author jiangxu
* @version 1.0, Jan 25, 2007
* @since 1.5
*/
public class Sort {
/**
* 快速排序链表中内容
*
* @param <T>
* @param list
*/
@SuppressWarnings(value={"unchecked"})
public static <T extends SortableType> void quickSort(Vector<T> list){
if(list.size() <= 1){
return;
}
Object[] contentArray = list.toArray();
quickSort(contentArray, 0, contentArray.length-1);
list.clear();
for(int i=0; i<contentArray.length; i++){
list.add((T)contentArray[i]);
}
}
/**
*
* @param contentArray
* @param low
* @param high
*/
private static void quickSort(Object[] contentArray, int iLow, int iHigh){
int low = iLow;
int high = iHigh;
SortableType mid = (SortableType)contentArray[(low + high)/2];
do{
while(((SortableType)contentArray[low]).compareTo(mid) < 0){
low ++;
}
while(((SortableType)contentArray[high]).compareTo(mid) > 0){
high--;
}
if(low <= high){
Object tmp = contentArray[low];
contentArray[low] = contentArray[high];
contentArray[high] = tmp;
low++;
high--;
}
if(high > iLow){
quickSort(contentArray, iLow, high);
}
if(low < iHigh){
quickSort(contentArray, low, iHigh);
}
}while(low <= high);
}
public static void main(String[] args){
///////////////////////////////////
//测试对向量中元素的快速排序
class Data implements SortableType{
private int value;
public Data(int value){
this.value = value;
}
public String toString(){
return "("+value+")";
}
public int compareTo(SortableType st){
return this.value - ((Data)st).value;
}
}
Vector<Data> list = new Vector<Data>();
list.add(new Data(28));
list.add(new Data(3));
list.add(new Data(5));
list.add(new Data(21));
list.add(new Data(17));
list.add(new Data(6));
list.add(new Data(18));
list.add(new Data(4));
list.add(new Data(6));
list.add(new Data(9));
list.add(new Data(10));
list.add(new Data(16));
list.add(new Data(30));
System.out.println("======快速排序前======");
for(int i=0; i<list.size(); i++){
System.out.println(list.get(i));
}
quickSort(list);
System.out.println("======快速排序后======");
for(int i=0; i<list.size(); i++){
System.out.println(list.get(i));
}
}
}
public interface SortableType {
int compareTo(SortableType st);
}
你可以看看我main函数里的例子
public int compareTo(SortableType st){
}
}
BTW:你在弄工作流?
谢谢,insert插入时需要一个int 参数的
Comparator<? super T> c)Sorts the specified list according to the order induced by the specified comparator. All elements in the list must be mutually comparable using the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the list).
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n log(n) performance. The specified list must be modifiable, but need not be resizable. This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. This avoids the n2 log(n) performance that would result from attempting to sort a linked list in place.
Parameters:
list - the list to be sorted.
c - the comparator to determine the order of the list. A null value indicates that the elements' natural ordering should be used.
Throws:
ClassCastException - if the list contains elements that are not mutually comparable using the specified comparator.
UnsupportedOperationException - if the specified list's list-iterator does not support the set operation.
See Also:
Comparator
sort(v.begin(), v.end());
这一行就完事儿了。
我的贴子第一次上头条,呵呵,改日开贴散分喽