求：一道关于vector 的算法,该怎么解决

www.MyException.Cn  网友分享于：2013-01-03  浏览：6次

------解决方案--------------------

/**
* 可排序类型
* @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++){
}
}

/**
*
* @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> ();