算法的一些小栗子2(选择排序)

来源:互联网 发布:湖北软件行业协会 编辑:程序博客网 时间:2024/05/17 05:57

简单选择排序

//简单选择排序public class SelectSort {    public void sort(int[] a) {        System.out.println("排序之前:");        for (int i = 0; i < a.length; i++) {            System.out.print(a[i] + " ");        }        System.out.println();        System.out.println("==============================");        int min;        int temp;        for (int i = 0; i < a.length; i++) {            min = a[i];            for (int j = i; j < a.length; j++) {                if (min > a[j]) {                    min = a[j];                    temp = a[i];                    a[i] = min;                    a[j] = temp;                }            }        }        System.out.println("排序之后:");        for (int i = 0; i < a.length; i++) {            System.out.print(a[i] + " ");        }    }    public static void main(String[] args) {        int a[] = {43, 21, 55, 37, 8, 0, 67, 99, 81, 32, 48};        SelectSort selectSort = new SelectSort();        selectSort.sort(a);    }}

堆排序

//堆排序public class HeapSort {    public void sort(int[] a) {        System.out.println("排序之前:");        for (int i = 0; i < a.length; i++) {            System.out.print(a[i] + " ");        }        System.out.println();        System.out.println("===================================");        if (a == null || a.length <= 1) {            return;        }        //创建大堆        buildMaxHeap(a);        for (int i = a.length - 1; i >= 1; i--) {            //最大元素排在了下标为0的地方            exchangeElements(a, 0, i);//每交换一次,就沉淀一个大元素            maxHeap(a, i, 0);        }        System.out.println("排序之后:");        for (int i = 0; i < a.length; i++) {            System.out.print(a[i] + " ");        }    }    private void buildMaxHeap(int[] a) {        int half = (a.length - 1) / 2;//只需遍历数组长度一半        for (int i = half; i >= 0; i--) {            maxHeap(a, a.length, i);        }    }    //length表示用于构造大堆的数组长度    private void maxHeap(int[] a, int length, int i) {        int left = i * 2 + 1;        int right = i * 2 + 2;        int largest = i;        if (left < length && a[left] > a[i]) {            largest = left;        }        if (right < length && a[right] > a[largest]) {            largest = right;        }        if (i != largest) {            //进行数据交换            exchangeElements(a, i, largest);            maxHeap(a, length, largest);        }    }    private void exchangeElements(int[] a, int i, int largest) {        int temp = a[i];        a[i] = a[largest];        a[largest] = temp;    }    public static void main(String[] args) {        int[] a = {44, 63, 56, 78, 20, 17, 33, 8, 72, 81, 108, 73};        HeapSort heapSort = new HeapSort();        heapSort.sort(a);    }}