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

1707004426冯小涛 #30

Open
xiaoshanzha opened this issue Nov 25, 2017 · 0 comments
Open

1707004426冯小涛 #30

xiaoshanzha opened this issue Nov 25, 2017 · 0 comments

Comments

@xiaoshanzha
Copy link

public class BaseSort {
    public void sort (int []a){
        System.out.println("排序算法");
    }
}

public class SelectSort extends BaseSort {
    @Override
    public void sort(int[] a) {
        for (int i = 0; i <a.length - 1; i++) {
            for (int j = 1+i; j <a.length ; j++) {
                if(a[i]>a[j])
                {
                    int temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
        System.out.print("选择排序:");
        for (int i = 0; i < a.length ; i++) {
            System.out.print(a[i]+" ");
        }
        System.out.println(" ");
    }
}
public class InsertSort extends BaseSort {
    public void sort(int[] a){
        for (int i = 1; i <a.length - 1; i++)
        {
            int temp = a[i],j;
            for( j = i - 1;j >= 0&&temp<a[j];j--)
            {
                a[j+1] = a[j];
            }
            a[j+1] = temp;
        }
        System.out.print("插入排序:");
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]+" ");
        }
        System.out.println(" ");
    }
}
public class QuivkSort extends BaseSort {
    public void sort(int[] a) {
        sort1(a,0,a.length-1);
        System.out.print("快速排序:");
        for(int i = 0;i < a.length;i++)
        {
            System.out.print(a[i]+" ");
        }
    }
        void sort1(int []arr,int start ,int end)
        {
            int key = arr[start];
            int low = start;
            int high = end;
            while(low < high)
            {
                while(low < high && arr[high] >= key)
                {
                    high--;
                }
                if(low < high)
                {
                    arr[low++] = arr[high];
                }
                while(low < high && arr[low] <= key)
                {
                    low  ++;
                }
                if(low < high)
                {
                    arr[high--] = arr[low];
                }
                arr[low] = key;
            }
            int m = low;
            if(start < m-1)
            {
                sort1(arr,start,m-1);
            }
            if(low+1 < end)
                sort1(arr,m+1,end);
        }
}
import java.util.Scanner;

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){
            Scanner input = new Scanner(System.in);
            int []a = new int [5];
            for (int i = 0;i < 5; i++){
                a[i] = input.nextInt();
            }
           //选择排序
            Factory factory = new Factory();
            BaseSort select_sort = new SelectSort();
            factory.setSort(select_sort);
            factory.doSort(a);
            //插入排序
            Factory factory2 = new Factory();
            BaseSort insert_sort = new InsertSort();
            factory2.setSort(insert_sort);
            factory2.doSort(a);
            //快速排序
            Factory factory3 = new Factory();
            BaseSort quick_sort = new QuivkSort();
            factory3.setSort(quick_sort);
            factory3.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