算法排序代码(高级排序)

1、快速排序

 

public class FastSort {

    /**
     * 交换
     * @param arr
     * @param a
     * @param b
     */
    public static void swap(int[] arr, int a, int b) {
        int temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }

    /**
     * 快速排序
     *
     * @param arr: 数组
     */
    public static void Fastsort(int arr[], int left, int right) {
        if (left > right) {
            return;
        } else {
            int i = left;
            int j = right;
            int temp;
            int point = arr[left];
            while (i < j) {
                while (arr[j] >= point && i < j) {
                    j--;
                }
                while (arr[i] <= point && i < j) {
                    i++;
                }
                swap(arr, i, j);
            }
            arr[left] = arr[i];
            arr[i] = point;
            Fastsort(arr, left, i - 1);
            Fastsort(arr, i + 1, right);
        }

    }

    /**
     * 打印输出
     * @param arr
     */
    public static void print(int arr[]) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }

    public static void main(String[] args) {
        int arr[] = {1, 4, 7, 0, 5, 4, 9, 7, 0, 4, 11};
        Fastsort(arr, 0, arr.length - 1);
        print(arr);
    }
}
View Code

 

2、堆排序

 

public class HeapSort {
    public static void swap(int[] arr, int a, int b) {
        int temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }

    public static void CreatHeap(int arr[], int i, int length) {
        for (i = length / 2 - 1; i >= 0; i--) {
            int temp = arr[i];
            for (int j = 2 * i + 1; j < length; j = j * 2 + 1) {
                if (j + 1 < length && arr[j] < arr[j + 1]) {
                    j++;
                }
                if (temp < arr[j]) {
                    arr[i] = arr[j];
                    i = j;
                }else {
                    break;
                }
            }
            arr[i] = temp;
        }
    }

    public static void SortHeap(int[] arr) {
        for (int i = arr.length-1; i >0 ; i--) {
            swap(arr,0,i);
            CreatHeap(arr,0,i);
        }
    }

    public static void main(String[] args) {
        int arr[] = {1, 5, 6, 7, 8, 4, 9, 1, 0, 5};
        //初次弄成初始堆
        CreatHeap(arr, 0,arr.length);

        //进行最大
        SortHeap(arr);

        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
View Code

 

3、希尔排序

 

public class SheelSort {
    public static void swap(int[] arr, int a, int b) {
        int temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }

    public static void sheel_sort(int arr[]) {
        for (int i = arr.length/5; i >=1 ; i = i/2) {
            for (int j = i; j < arr.length; j++) {
                for (int k = j; k >= i; k-=i) {
                    if (arr[k-i]>arr[k])
                    {
                        swap(arr,k-i,k);
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        int arr[] = {1, 5, 6, 7, 8, 4, 9, 1, 0, 5};
        sheel_sort(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
View Code

 

posted @ 2019-05-11 23:24  编程小白1024  阅读(306)  评论(0编辑  收藏  举报