Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

17070043魏祥一 #41

Open
WXONE opened this issue Nov 26, 2017 · 0 comments
Open

17070043魏祥一 #41

WXONE opened this issue Nov 26, 2017 · 0 comments

Comments

@WXONE
Copy link

WXONE commented Nov 26, 2017

//基础类
public class basesort {
    public void sort(int[] arr) {
        System.out.println("排序算法");

    }

    public void sort(int[] arr, int begin, int end) {
        System.out.println("重载排序算法");
    }
}
//快速排序
public class quicksort extends basesort {
    public static int getMiddle(int []arr,int low ,int high){
        int temp=arr[low];
        while(low<high){
            while(low<high&&arr[high]>=temp){
                high--;
            }
            arr[low]=arr[high];
            while(low<high&&arr[low]<temp){
                low++;
            }
            arr[high]=arr[low];
        }
        arr[low]=temp;
        return low;
    }
}
//选择排序
public class selsectsort extends basesory {
    public static void selectSort(int[] arr) {


        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i + 1; j < arr.length; j++){
            if(arr[i]>arr[j]){
                int temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
            }
        }
    }
}
}
//插入排序
public class insersort extends basesort {

    public static void insersort (int arr[]){

        for(int i=1;i<arr.length;i++){
            int temp=arr[i];
            int j=i-1;
            for(j=i-1;j<=0&&arr[j]>temp;j--){
                arr[j+1]=arr[j];
            }
            arr[j+1]=temp;
        }

    }

}
//策略类
public class Factory {
    private basesort aort;
    //依赖注入
    public void setSort(basesort sort){
        this.sort;=sort;

    }
    public void doSort(int []arr){
        sort.sort(arr);

    }
    public void doSort(int []arr,int begin,int end){
        sort.sort(arr, begin ,end);
    }
}
//测试类
public class test {
    public static  void main(String[] args){
        int []arr=new int[10];
        Scanner scanner=new Scanner(System .in);
        System.out.println("自己从键盘输入:");
        for(int i=0;i<10;i++){
            arr[i]=scanner.nextInt();
        }
     Factory factory=new Factory();
        basesort select_sort=new selsectsort();
        factory.setSort(select_sort);
        factory.doSort(arr);


    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant