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

1607094156蒋昱葳 #35

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

1607094156蒋昱葳 #35

jiangyuwei666 opened this issue Nov 25, 2017 · 0 comments

Comments

@jiangyuwei666
Copy link

Day1 Work


BaseSort

public class BaseSort {

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

    public void sort ( int [] a , int start , int end ) {
        System.out.println("重载排序算法");
    }

}

InsertSort

public class InsertSort extends BaseSort{

    @Override
    public void sort(int[] a) {
        super.sort(a);
        for ( int i = 0 ; i < a.length ; i ++ ) {
            for ( int j = i + 1 ; j < a.length ; j ++) {
                if ( a[i] > a[j] ) {
                    int t = a[i] ;
                    a[i] = a[j] ;
                    a[j] = t ;
                }
            }
            System.out.println("冒泡排序");
        }
    }

}

QuickSort

public class QuickSort extends BaseSort {

    @Override
    public void sort(int[] a, int start, int end) {
        super.sort(a, start, end);
        int i , j , t , p ;//p放基准数,t用于交换,ij 控制循环

        if ( start > end ) {
            return ;
        }//判断是否还需要再排

        i = start ;
        j = end ;
        p = a[start] ;

        while ( i != j ) {

            while (a[j] >= p && i < j  ) {
                j -- ;
            }
            while ( a[i] <= p && i < j ) {
                i ++ ;
            }

            if ( i < j ) {
                t = a[i] ;
                a[i] = a[j] ;
                a[j] = t ;
            }

        }
        a[start] = a[i] ;
        a[i] = p ;
        sort( a , start , i - 1 );//递归,再对左边进行排序
        sort( a , i +1 , end );//递归,对右边进行排序
        System.out.println("快速排序");
    }

}

SelectSort

public class SelectSort extends BaseSort {

    @Override
    public void sort(int[] a) {
        super.sort(a);
        int k , t  ;
        for ( int i = 0 ; i <= 9 ; i ++ ) {
            k = i ;
            for ( int j = i + 1 ; j <= 9 ; j ++ ) {
                if ( a[j] < a[k] ) {
                    k = j ;
                }
                t = a[k] ;
                a[k] = a[i] ;
                a[i] = t ;
            }
        }
        System.out.println("选择排序");
    }
}

Factory

public class Factory {

    private BaseSort sort;

    //依赖注入
    public void setSort(BaseSort sort){
        this.sort = sort;
    }

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

    public void doSort ( int [] a , int start , int end ) {
        sort.sort( a , start , end ) ;
    }

}

homework

public class homework {

    public static void main(String[] args) {
        Scanner scanner = new Scanner( System.in ) ;
        int a[] ;
        a = new int[10]  ;
        for ( int i = 0 ; i < 10 ; i ++ ) {
            a[i] = scanner.nextInt() ;
        }
        Factory factory = new Factory();
        BaseSort quick_sort = new InsertSort();
        factory.setSort(quick_sort);
        factory.doSort(a);
        //factory.doSort(a ,0 , a.length - 1) ;
        for ( int i:a
                ) {
            System.out.println( i );
        }
    }

}
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