android-nuc / 17_Java_Train

17级 Android 实验室 Java 培训
12 stars 2 forks source link

1607094158-王宇飞 #4

Open McFlyWYF opened 6 years ago

McFlyWYF commented 6 years ago

排序算法

public class BaseSort {

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

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

public class SelectSort extends BaseSort{

public void sort(int []a){

    int i,j,k;
    for(i = 0;i < a.length - 1;i++) {
        k = i;
        for (j = i+1;j < a.length;j++){
            if(a[j] < a[k]){
                k = j;
            }
            if(k!=i){
                int temp = a[k];
                a[k] = a[i];
                a[i] = temp;
            }
        }
    }
    super.sort(a);
    System.out.println("选择排序算法");
    System.out.println(Arrays.toString(a));
}

}

* 插入排序
```java
import java.util.Arrays;

public class InsertSort extends BaseSort{

    public void sort(int[] a){

        int i,j,k;
        for(i = 1;i < a.length;i++){
            j = 0;
            while (i > j && a[i] >= a[j]){
                j++;
            }
            if (i > j){
                   k = i;
                int temp = a[i];
                while (k > j){
                    a[k] = a[k-1];
                    k--;
                }
                a[k] = temp;
            }
        }
        super.sort(a);
        System.out.println("插入排序算法");
        System.out.println(Arrays.toString(a));
    }
}

public class QuickSort extends BaseSort {

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

    int start = left;
    int end = right;
    int key = a[left];

    while (end > start) {
        while (end > start && a[end] >= key)
            end--;
        if (a[end] <= key) {
            int temp = a[end];
            a[end] = a[start];
            a[start] = temp;
        }

        while (end > start && a[start] <= key)
            start++;
        if (a[start] >= key) {
            int temp = a[start];
            a[start] = a[end];
            a[end] = temp;
        }
    }
    if (start > left) sort(a, left, start - 1);
    if (end < right) sort(a, end + 1, right);

    super.sort(a);
    System.out.println("快速排序算法");
    System.out.println(Arrays.toString(a));
}

}

```java
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 left,int right){
        sort.sort(a,left,right);
    }
}

import java.util.Scanner;

public class Test {
    public static void main(String[] args){

        int[] a = new int[10];

        Scanner in = new Scanner(System.in);
        for (int i = 0;i < 10;i++) {
            a[i] = in.nextInt();
        }

        Factory factory = new Factory();

        BaseSort select_sort = new SelectSort();
        BaseSort insert_sort = new InsertSort();
        BaseSort quick_sort = new QuickSort();

        factory.setSort(insert_sort);//插入排序
        //factory.setSort(select_sort);//选择排序
        //factory.setSort(quick_sort);//快速排序

        //factory.doSort(a,0,9);
        factory.doSort(a);
    }
}
样例输入
4 3 234 89 67 43 09 78 56 55
样例输出
[3, 4, 9, 43, 55, 56, 67, 78, 89, 234]