java 排序算法

来源:互联网 发布:linux utf8转ansi 编辑:程序博客网 时间:2024/06/10 15:04

1.插入排序

(1)直接插入排序

package com.wenwen.insertsort;

/**
 * @author 张文辉
 *
 */
public class DirectInsert {
    /**
     *  直接插入排序: 先插入后移动
     * @param a
     */
    public static void sort(int[] a) {
        for (int i = 1; i < a.length; i++) {
            int temp = 0, index = 0;
            for (int j = 0; j < i; j++) {
                if (a[i] < a[j]) {
                    temp = a[j];
                    a[j] = a[i];
                    index = j + 1;
                    break;
                }
            }
            if (index > 0) {
                for (int m = i; m > index; m--) {
                    a[m] = a[m - 1];
                }
                a[index] = temp;
            }
        }
    }

    /**
     *  直接插入排序 先移动后插入
     * @param a
     */
    public static void sort2(int[] a) {
        for (int i = 0; i < a.length -1 ; i++) {
            int j = i;
            int temp = a[i + 1];
            while (j >= 0 && temp < a[j]) {
                a[j + 1] = a[j];
                j--;
            }
            a[j + 1] = temp;
        }
    }

    public static void main(String[] args) {
        int[] a = { 45, 23, 24, 13, 12, 35, 23, 45, 68, 10, 9, 40, 1 };
        DirectInsert.sort(a);
        System.out.println("直接插入排序后:");
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + "    ");
        }
        DirectInsert.sort2(a);
        System.out.println("直接插入排序后:");
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + "    ");
        }
    }
}

(2)希尔排序:

package com.wenwen.insertsort;

/**
 * @author 张文辉 希尔排序,分组插入排序
 *
 */
public class GroupInsert {

    /**
     * 希尔排序,即分组使用直接排序
     *
     * @param a
     *            待排序数组
     * @param d
     *            分组的增量数组,最后的增量值必须为1
     */
    public static void sort2(int[] a, int[] d) {
        for (int increment = 0; increment < d.length; increment++) {
            for (int k = 0; k < d[increment]; k++) {
                for (int i = k; i < a.length - d[increment]; i += d[increment]) {
                    int j = i;
                    int temp = a[i + d[increment]];
                    while (j >= 0 && temp < a[j]) {
                        a[j + d[increment]] = a[j];
                        j -= d[increment];
                    }
                    a[j + d[increment]] = temp;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[] a = { 45, 23, 24, 13, 12, 35, 23, 45, 68, 10, 9, 40, 1 };
        int[] d = { 6, 3, 1 };
        GroupInsert.sort2(a, d);
        System.out.println("希尔排序后:");
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + "    ");
        }
    }
}

2.选择排序

(1)直接选择排序:

package com.wenwen.choicesort;

/**
 * @author 张文辉
 *
 */
public class DirectChoice {
    /**
     *  直接选择排序
     * @param a
     */
    public static void sort(int[] a) {
        for(int i = 0; i < a.length; i++){
            int small = i;
            for(int j = i + 1; j < a.length; j++){
                if(a[j] < a[small]){
                    small = j;
                }
            }
            if(small != i){
                int temp = a[i];
                a[i] = a[small];
                a[small] = temp;
            }
        }
        System.out.println("直接选择排序后:");
    }

    public static void main(String[] args) {
        int[] a = { 45, 23, 24, 13, 12, 35, 23, 45, 68, 10, 9, 40, 1 };
        DirectChoice.sort(a);
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + "    ");
        }
    }
}

(2)堆排序:

package com.wenwen.choicesort;

/**
 * @author 张文辉
 *
 */
public class HeapSort {
    /**
     * 堆排序,选择排序,每次都选择堆顶元素
     *
     * @param a
     */

    /**
     * @param a
     * @param n
     *            需要创建的堆大小,元数个数
     * @param notLeaf
     */
    public static void createMaxHeap(int[] a, int n, int notLeaf) {
        int notLeafIndex = notLeaf; // 非叶子结点
        int leafData = a[notLeafIndex];

        int leftLeafIndex = notLeafIndex * 2 + 1; // 左子结点

        int flag = 0; // 如果没有移动过,则不需要对数据进行调整

        while (flag == 0 && leftLeafIndex < n) {
            if ((leftLeafIndex < n - 1)
                    && (a[leftLeafIndex] < a[leftLeafIndex + 1])) {
                leftLeafIndex++; // 左子结点小于右子节点,则和右子结点比较
            }
            if (a[leftLeafIndex] < leafData) {
                flag = 1; // 非叶子结点更大,则不需要移动
            } else {
                a[notLeafIndex] = a[leftLeafIndex];
                notLeafIndex = leftLeafIndex;
                leftLeafIndex = notLeafIndex * 2 + 1;
            }
        }
        a[notLeafIndex] = leafData;
    }

    public static void initCreateMaxHeap(int[] a) {
        for (int i = (a.length - 1) / 2 - 1; i >= 0; i--) {
            createMaxHeap(a, a.length, i);
        }
    }

    public static void sort(int[] a) {
        System.out.println("堆排序后:");
        initCreateMaxHeap(a);
        for (int i = a.length - 1; i > 0; i--) {
            int temp = a[i];
            a[i] = a[0];
            a[0] = temp;
            createMaxHeap(a, i, 0);
        }
    }

    public static void main(String[] args) {
        int[] a = { 45, 23, 24, 13, 12, 35, 23, 45, 68, 10, 9, 40, 1 };
        HeapSort.sort(a);
        // HeapSort.initCreateMaxHeap(a);
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + "    ");
        }
    }
}

3.交换排序

(1)冒泡排序:

package com.wenwen.changesort;

/**
 * @author 张文辉
 *
 */
public class BubbleSort {
    /**
     *  交换排序 : 冒泡排序
     * @param a
     */
    public static void sort(int[] a) {
        boolean flag = true;
        for(int i = 0; i < a.length && flag; i++){
            flag = false;
            for(int j = 0; j < a.length - 1 - i; j++){
                if(a[j] > a[j+1]){
                    int temp = a[j+1];
                    a[j+1] = a[j];
                    a[j] = temp;
                    flag = true;
                }
            }
        }
        System.out.println("冒泡排序后:");
    }

    public static void main(String[] args) {
        int[] a = { 45, 23, 24, 13, 12, 35, 23, 45, 68, 10, 9, 40, 1 };
        BubbleSort.sort(a);
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + "    ");
        }
    }
}

(2)快速排序

package com.wenwen.changesort;

/**
 * @author 张文辉
 *
 */
public class QuickSort {
    /**
     * 交换排序 : 快速排序
     *
     * @param a
     */
    public static void sort(int[] a, int low, int high) {

        int level = a[low];
        int i = low, j = high;
        while (i < j) {
            while (i < j && (a[j] >= level)) {
                j--;
            }
            if (i < j) {
                a[i] = a[j];
                i++;
            }

            while (i < j && (a[i] < level)) {
                i++;
            }
            if (i < j) {
                a[j] = a[i];
                j--;
            }
        }
        a[i] = level;

        if (low < i)
            sort(a, low, i - 1);
        if (i < high)
            sort(a, j + 1, high);

    }

    public static void main(String[] args) {
        int[] a = { 45, 23, 24, 13, 12, 35, 23, 45, 68, 10, 9, 40, 1 };
        System.out.println("快速排序后:");
        QuickSort.sort(a, 0, a.length - 1);
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + "    ");
        }
    }
}

4.归并排序:

(1)二路归并:

package com.wenwen.mergesort;

import java.util.Arrays;

/**
 * @author 张文辉
 *
 */
public class MergeSpaceSort {
    /**
     * 归并排序 : 二路归并排序,利用临时空间
     *
     * @param a
     */
    public static void twoMerge(int[] a, int width, int[] tempArray) {

        int low1Index = 0, up1Index;
        int low2Index, up2Index;
        int tempIndex = 0;
        while (low1Index + width < a.length) {
            up1Index = low1Index + width - 1;
            low2Index = up1Index + 1;
            up2Index = (low2Index + width - 1) <= (a.length - 1) ? (low2Index
                    + width - 1) : (a.length - 1);
            int i, j;
            for (i = low1Index, j = low2Index; (i <= up1Index && j <= up2Index); tempIndex++) {
                if (a[i] <= a[j]) {
                    tempArray[tempIndex] = a[i];
                    i++;
                } else {
                    tempArray[tempIndex] = a[j];
                    j++;
                }
            }
            while (i <= up1Index) {
                tempArray[tempIndex++] = a[i++];
            }
            while (j <= up2Index) {
                tempArray[tempIndex++] = a[j++];
            }

            low1Index = up2Index + 1;
        }

        while (low1Index < a.length && tempIndex < tempArray.length) {
            tempArray[tempIndex++] = a[low1Index++];
        }
    }

    static public int[] sort(int[] a) {
        System.out.println("归并排序后:");
        int[] tempArray = new int[a.length];
        for (int i = 1; i < a.length; i *= 2) {
            twoMerge(a, i, tempArray);
            for (int m = 0; m < tempArray.length; m++) {
                System.out.print(tempArray[m] + "    ");
            }
            // a = Arrays.copyOf(tempArray, tempArray.length);
            for (int j = 0; j < a.length; j++) {
                a[j] = tempArray[j];
            }
            System.out.println();
        }
        return a;
    }

    static public void main(String[] args) {
        int[] a = { 45, 23, 24, 13, 12, 35, 23, 45, 68, 10, 9, 40, 1 };
        // a = MergeSpaceSort.sort(a);
        MergeSpaceSort.sort(a);
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + "    ");
        }
    }
}

5.基数排序:

package com.wenwen.radixsort;

import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author 张文辉
 *
 */
public class RadixSort {
    /**
     * 基数排序
     *
     * @param a
     * @param count
     *            最大数值的位数 比如 123,则count为3
     * @param scale
     *            进制数(十进制、二进制等)
     */
    public static void radixSort(int[] a, int count, int scale) {
        Queue[] qList = new Queue[scale];
        for (int i = 1; i <= count; i++) {
            for (int j = 0; j < a.length; j++) {
                int value = (a[j] / ((int) Math.pow(scale, i - 1)))
                        - (scale * (a[j] / (int) Math.pow(scale, i)));
                if (qList[value] == null) {
                    qList[value] = new LinkedBlockingQueue<Integer>();
                }
                qList[value].add(a[j]);
            }
            int tempIndex = 0;
            for (int q = 0; q < qList.length; q++) {
                if (qList[q] == null)
                    continue;
                while (!qList[q].isEmpty()) {
                    a[tempIndex] = (Integer) qList[q].poll();
                    tempIndex++;
                }
            }
        }
        for (Queue q : qList) {
            q = null;
        }
        qList = null;
    }

    static public void main(String[] args) {
        int[] a = { 45, 1024, 24, 13, 12, 35, 23, 45, 68, 10, 9, 404, 132,10 };
        // a = MergeSpaceSort.sort(a);
        RadixSort.radixSort(a, 4, 10);
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + "    ");
        }
    }
}

代码思想参考:《数据结构》 一书。





0 0
原创粉丝点击