public interface SortStrategy {
List<Integer> sort(List<Integer> list);
}
public class QuickSort implements SortStrategy{
private void partion(Integer[] array, int left, int right){
if(left>=right){
return;
}
Integer baseNum=array[left];
int leftIndex=left,rightIndex=right;
while(leftIndex<rightIndex){
while(leftIndex<rightIndex && array[rightIndex]>=baseNum){
rightIndex--;
}
if(leftIndex<rightIndex){
array[leftIndex++]=array[rightIndex];
}
while(leftIndex<rightIndex && array[leftIndex]<baseNum){
leftIndex++;
}
if(leftIndex<rightIndex){
array[rightIndex--]=array[leftIndex];
}
}
array[leftIndex]=baseNum;
partion(array,left,leftIndex-1);
partion(array,leftIndex+1,right);
}
@Override
public List<Integer> sort(List<Integer> list) {
Integer[] arrays=Iterables.toArray(list,Integer.class);
partion(arrays,0,list.size()-1);
return Lists.newArrayList(arrays);
}
}
public class StupidSort implements SortStrategy{
@Override
public List<Integer> sort(List<Integer> list) {
Integer[] arrays=Iterables.toArray(list,Integer.class);
for(int i=0;i<arrays.length-1;i++){
for(int j=i+1;j<arrays.length;j++){
if (arrays[i]>arrays[j]){
int t=arrays[i];
arrays[i]=arrays[j];
arrays[j]=t;
}
}
}
return Lists.newArrayList(arrays);
}
}
public class SortUtil {
private SortStrategy sortStrategy;
public SortUtil(SortStrategy sortStrategy){
this.sortStrategy=sortStrategy;
}
public List<Integer> sort(List<Integer> list){
return sortStrategy.sort(list);
}
}
public class App {
public static void main(String[] args) {
List<Integer> list= Lists.newArrayList(6,5,4,3,2,1);
SortUtil util=new SortUtil(new QuickSort());
list=util.sort(list);
System.out.println(Iterables.toString(list));
}
}