几个排序算法

来源:互联网 发布:什么铁艺制品淘宝 编辑:程序博客网 时间:2024/05/16 11:21
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package suanFa;import util.SuanFaUtil;/** * 插入排序:直接插入排序+希尔排序 * * @author Administrator */public class TestCharuAndXiEr {    public static void main(String args[]) {        int[] a = SuanFaUtil.getRandomArrayByLength(                100);        System.out.println("排序前数组");        printA(a);        xiErSort(a, new int[]{10, 5, 1});        System.out.println("\n排序后数组");        printA(a);    }    public static void printA(int[] a) {        System.out.print("--------------------------------------------------------------------\n|");        for (int j = 1; j <= 20; j++) {            System.out.print("-" + String.format("% 3d", j) + "-|");        }        for (int i = 0; i < a.length; i++) {            if (i % 20 == 0) {                System.out.print("\n|");            }            System.out.print(String.format("% 5d", a[i]) + "|");        }    }    /**     * 直接插入排序 逐个加入元素,并放到适当大小处     *     * @param a     */    public static void chaRuSort(int[] a) {        for (int i = 1; i < a.length; i++) {//插入length-1次            int j = i;            int temp;//用于交换位置            while (j > 0 && a[j] <= a[j - 1]) {//这里其实是在不创建新的数组的前提下从左到由逐个插入元素使得有序,其实左边部分就是有序数组                temp = a[j - 1];                a[j - 1] = a[j];                a[j] = temp;                j--;            }        }    }    /**     * 希尔排序 d为增量数组     */    public static void xiErSort(int[] a, int[] d) {        for (int k = 0; k < d.length; k++) {//每个增量排序依次,最后依次排序必须是增量为1的            //其分组实际上是分成增量相同个数个组,其实每隔一定增量取一个就会得到这个结果(必定每个元素都在其中一组)            int curD = d[k];            for (int p = 0; p < curD; p++) {                //对每一组采取直接插入排序,不同之处只是取得该组数据的是根据增量的对应下标获取                for (int i = p + curD; i < a.length; i = i + curD) {                    int j = i;                    int temp;//用于交换位置                    while (j >= curD && a[j] <= a[j - curD]) {//这里其实是在不创建新的数组的前提下从左到由逐个插入元素使得有序,其实左边部分就是有序数组                        temp = a[j - curD];                        a[j - curD] = a[j];                        a[j] = temp;                        j = j - curD;                    }                }            }            if (k != d.length - 1) {                System.out.println("\n------------------------------      d=" + curD                        + "       -----------------------------------");                printA(a);            }        }    }}

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package suanFa;import util.SuanFaUtil;/** * 选择排序包括直接选择排序和堆排序 * * @author Administrator */public class TestSelectorSort {    public static void main(String args[]) {        int[] a = SuanFaUtil.getRandomArrayByLength(1000);        System.out.println("排序前数组");        SuanFaUtil.printArray(a);        selectSort(a);        System.out.println("\n排序后数组");        SuanFaUtil.printArray(a);    }    /**     * 直接选择排序     *     * @param a     */    public static void selectSort(int[] a) {        for (int i = 0; i < a.length - 1; i++) {//排序length-1次即可获得有序数组(n-1次选取最大值)            int temp;            for (int j = i + 1; j < a.length; j++) {                //对数组的无序部分,逐个比较以获得最小值(符合小于条件后交换),并放到对应位置,最后:a[0]<a[1]<a[2]..<a[n-1]                if (a[i] > a[j]) {//a[i]将是当前最小值                    temp = a[i];                    a[i] = a[j];                    a[j] = temp;                }            }        }    }}

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package suanFa;import util.SuanFaUtil;/** * 交换排序:包括冒泡排序和快速排序 * * @author Administrator */public class TestExchangeSort {    public static void main(String args[]) {        int[] a = SuanFaUtil.getRandomArrayByLength(1000);        System.out.println("排序前数组");        SuanFaUtil.printArray(a);        quickSort(a, 0, a.length - 1);        System.out.println("\n排序后数组");        SuanFaUtil.printArray(a);    }    /**     * 冒泡排序     *     * @param a     */    public static void bubbleSort(int[] a) {        for (int i = 0; i < a.length - 1; i++) {//排序length-1次即可获得有序数组(n-1次两两比较数组无序部分)            int temp;            for (int j = 0; j < a.length - 1; j++) {                //无序部分进行两两比较,将较少值前移                if (a[j + 1] < a[j]) {//a[i]将是当前最小值                    temp = a[j + 1];                    a[j + 1] = a[j];                    a[j] = temp;                }            }        }    }    /**     * 快速排序 选取一个元素,将目标数组的中小于该元素的元素放到该元素左边,大于的放到右边     * 为什么这算是交换排序?冒泡是两两比较交换位置,而快速则是一个比较其他元素,并根据结果交换该元素与其他元素的位置?     *     * @param a     * @param low 当前排序的部分在目标数组的下限     * @param high 上限     */    public static void quickSort(int[] a, int low, int high) {        if (low >= high) {//分别对应当前排序的数组部分元素个数为0或者一的情况            return;        }        int targetIndex = low;//记录用于作为标准的元素当前下标,默认使用下端作为标准元素        for (int i = low + 1; i <= high; i++) {//只需要比较n-1次即可            int tempTarget;            int tempNext;            if (a[i] < a[targetIndex]) {//当发现存在小于a[low]标准元素时左移,                //具体移动是,发现的较少元素一道原标准元素处,标准元素移到下一位,而原下一位元素移到较少元素原下标会发现即使较少元素同时又是标准元素的下一位也没问题                tempTarget = a[targetIndex];                tempNext = a[targetIndex + 1];                a[targetIndex] = a[i];                a[i] = tempNext;                a[targetIndex + 1] = tempTarget;                targetIndex++;            }        }        //递归对后面的数组部分操作        quickSort(a, low, targetIndex - 1);        quickSort(a, targetIndex + 1, high);    }}
package util;/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. *//** * * @author Djh */public class SuanFaUtil {    public static int[] getRandomArrayByLength(int length) {        int[] a = new int[length];        for (int i = 0; i < length; i++) {            a[i] = (int) (Math.random() * length) + 1;//生成的随机数是1到length之间        }        return a;    }    public static void printArray(int[] a) {        System.out.print("--------------------------------------------------------------------\n|");        for (int j = 1; j <= 20; j++) {            System.out.print("-" + String.format("% 3d", j) + "-|");        }        for (int i = 0; i < a.length; i++) {            if (i % 20 == 0) {                System.out.print("\n|");            }            System.out.print(String.format("% 5d", a[i]) + "|");        }    }}



0 0
原创粉丝点击