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

1607094103 李雅洁 #32

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

1607094103 李雅洁 #32

liyaj opened this issue Nov 25, 2017 · 0 comments

Comments

@liyaj
Copy link

liyaj commented Nov 25, 2017

public class Factory {
    private BaseSort Sort;
    public void setSort(BaseSort Sort){
        this.Sort = Sort;
    }
    public void doSort(int []a){
        Sort.Sort(a);
    }
}
public class BaseSort {
    public void Sort(int[] a){
        System.out.println("排序算法");
    }

}
public class SelectSort extends BaseSort {
    public void Sort(int[] a){
        int k;
        for(int i = 0;i < a.length;i++){
            for (int j = i+1; j < a.length ; j++) {
                if(a[i]>a[j]){
                    k = a[i];
                    a[i] = a[j];
                    a[j] = k;
                }

            }

        }
        for (int i = 0; i <a.length ; i++) {
            System.out.println(a[i]);

        }
    }
public class InsertSort extends BaseSort {
    public void Sort(int[] a){
        //直接插入法
        int k;
        for (int i = 1; i < a.length ; i++) {
            for (int l = i;l >= 1; l--) {
                if (a[l] < a[l-1]) {
                    k = a[l-1];
                    a[l-1] = a[l];
                    a[l] = k;
                }

            }
        }

        for ( int i = 0; i <a.length ; i++) {
            System.out.println(a[i]);

        }
    }
}
import java.util.Scanner;

public class QuickSort extends BaseSort {
    public void Sort(int[] a){
      sort(a,0,a.length-1);
        for (int i = 0; i <a.length ; i++) {
            System.out.println(a[i]);

        }
    }
    public void sort(int[] a,int left,int right){

        int i = left;//low
        int j = right;//high
        int k;//标志
        k = a[left];
        for(;i != j&&i<j;){
            System.out.println("222");
            while (i<j&&a[j]>k)
                j--;
            if (a[j] < k) {
                a[i] = a[j];
                i++;
            }
            while (i<j&&a[i]<k)
                i++;
           if (a[i]>k ){
                a[j] = a[i];
                j--;
            }
        }
        a[i]  = k;
        if(left<i-1)
            sort(a,left,i-1);
        if(i+1<a.length-1)
            sort(a,i+1,a.length-1);
    }
}
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
    
       int[] a = new int[10];     
   for (int i = 0; i < 10; i++) {
           a[i] = in.nextInt();
      }

       Factory factory = new Factory();
       BaseSort select_sort = new SelectSort();
       factory.setSort(select_sort);
       factory.doSort(a);
       select_sort.diaplay(a);
        QuickSort quickSort = new QuickSort();
        quickSort.Sort(a);
        InsertSort insertSort = new InsertSort();
        insertSort.Sort(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