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

1607094220 李书豪 #20

Open
codejeremy opened this issue Nov 23, 2017 · 0 comments
Open

1607094220 李书豪 #20

codejeremy opened this issue Nov 23, 2017 · 0 comments

Comments

@codejeremy
Copy link

public class BaseSort {
    public void sort(int []a){
        System.out.println("排序算法");
    }
    }
//插入排序
public class InsertSort extends BaseSort{
    @Override
    public void sort(int[] a) {
        super.sort(a);
        int j;
        for(int i=1;i<a.length;i++){  //从第二个数开始插入
            j=i-1;
            while(j<i && a[j]<=a[i]) //寻找插入位置
                j++;
            if(j<i){    //在i位置之前如果有比a[i]大的数,将其后移并进行插入
                int k=i;
                int temp=a[i];
                while(k>j){   //后移
                    a[k]=a[k-1];
                    k--;
                }
                a[k]=temp;  //插入
            }
        }
        System.out.print("插入排序结果为:");
        for (int num:
             a) {
            System.out.print(num + " ");
        }
    }
}
//快速排序
public class QuickSort extends BaseSort {
    @Override
    public void sort(int[] a) {
        super.sort(a);
        sort(a,0,a.length-1);
        for (int num:
             a) {
            System.out.print("快速排序结果为:");
            System.out.print(num + " ");
        }
    }
    public void sort(int []a,int low,int high){
        int i,j;
        int key=a[low];
        i=low;j=high;
        while(i<j){
            while(i <j && a[j] >= key){
                j--;
            }
            if(i<j){
             a[i++]=a[j];
            }
            while(i <j && a[i] < key){
                i++;
            }
            if(i<j){
                a[j--]=a[i];
            }
        }
        a[i]=key;
        sort(a,low,i-1);
        sort(a,i+1,high);
    }
}
//选择排序
public class SelectSort extends BaseSort{
    @Override
    public void sort(int[] a) {
        super.sort(a);
        int temp;
        for (int i = 0; i < a.length; i++) {
            int k=i;
            for (int j = i+1; j < a.length; j++) {
                if(a[k]>a[j])
                    k=j;
            }
            if(k!=i){
                temp=a[i];
                a[i]=a[k];
                a[k]=temp;
            }
        }
        System.out.print("选择排序结果为:");
        for (int num:
             a) {
            System.out.print(num+" ");
        }
    }
}
//策略类
public class Factory {
    private BaseSort sort;//依赖注入
    public void setSort(BaseSort sort){
        this.sort = sort;
    }
    public void doSort(int []a){
        sort.sort(a);
    }
}
//测试
import java.util.Scanner;

public class Test {
    public static void main(String []args){
        int []a=new int[10];
        Scanner input =new Scanner(System.in);
        for (int i = 0; i <10 ; i++) {
            a[i] = input.nextInt();
        }
        Factory factory = new Factory();
       // BaseSort sort1 = new InsertSort();
        BaseSort sort2 = new QuickSort();
        BaseSort sort3 = new SelectSort();
        //插入排序
        //factory.setSort(sort1);
        //factory.doSort(a);
        //快排
        //factory.setSort(sort2);
        //factory.doSort(a);
        //选择排序
        factory.setSort(sort3);
        factory.doSort(a);
    }
}
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