您的位置:首页 > 编程语言 > Java开发

[Java]各种经典排序

2016-06-17 18:13 483 查看
package sort;  

  

import java.util.ArrayList;  

import java.util.List;  

  

public class SortMethods {  

    public static void main(String args[]) {  

        int a[] = { 5, 8, 1, 4, 3, 1, 2, 3, 7, 6 };  

        int size = a.length;  

  

        // BubbleSort(a, size);  

        // InsertSort(a, size);  

        SelectSort(a, size);  

        // QuickSort(a, size);  

        // HeapSort(a, size);  

        // ShellSort(a, size);  

        // MergeSort(a, size);  

        // CountSort(a, size);  

        print(a, size);  

    }  

  

    public static void print(int[] a, int size) {  

        for (int i = 0; i < size; i++) {  

            System.out.print(a[i] + " ");  

        }  

    }  

  

    // 冒泡排序  

    public static void BubbleSort(int[] a, int size) {  

        for (int i = 0; i < size; i++) {  

            for (int j = size - 1; j > i; j--) {  

                if (a[j] < a[j - 1]) {  

                    Swap(a, j - 1, j);  

                }  

            }  

        }  

    }  

  

    // 选择排序  

    public static void SelectSort(int[] a, int size) {  

        int min; // min指向最小数的下标  

        for (int i = 0; i < size; i++) {  

            min = i;  

            for (int j = i + 1; j < size; j++) {  

                if (a[min] > a[j]) {  

                    min = j;  

                }  

            }  

            if (min != i)  

                Swap(a, i, min);  

        }  

    }  

  

    // 插入排序  

    public static void InsertSort(int[] a, int size) {  

        int temp, j;  

        for (int i = 1; i < size; i++) {  

            temp = a[i];  

            j = i - 1;  

            while (j >= 0 && temp < a[j]) {  

                a[j + 1] = a[j];  

                j--;  

            }  

            a[j + 1] = temp;  

        }  

    }  

  

    // 快速排序  

    public static void QuickSort(int[] a, int size) {  

        RecQuickSort(a, 0, size - 1);  

    }  

  

    public static void RecQuickSort(int[] a, int low, int high) {  

        int p;  

        if (low < high) {  

            p = Partition(a, low, high);  

            RecQuickSort(a, low, p - 1);  

            RecQuickSort(a, p + 1, high);  

        }  

    }  

  

    public static int Partition(int[] a, int low, int high) {  

        int p = a[low];  

        while (low < high) {  

            while (low < high && a[high] >= p) {  

                high--;  

            }  

            a[low] = a[high];  

            while (low < high && a[low] <= p) {  

                low++;  

            }  

            a[high] = a[low];  

        }  

        a[low] = p;  

        return low;  

    }  

  

    // 堆排序  

    public static void HeapSort(int[] a, int size) {  

        BuildHeap(a, size);  

        for (int i = size - 1; i >= 0; i--) {  

            Swap(a, 0, i);  

            HeapAdjust(a, 0, i);  

        }  

    }  

  

    public static void BuildHeap(int[] a, int size) {  

        for (int i = size / 2 - 1; i >= 0; i--) {  

            HeapAdjust(a, i, size);  

        }  

    }  

  

    public static void HeapAdjust(int[] a, int i, int size) {  

        int temp = a[i];  

        int j = 2 * i + 1;  

        while (j < size) {  

            if (j + 1 < size && a[j + 1] > a[j])  

                j++;  

  

            if (a[j] < temp)  

                break;  

  

            a[i] = a[j];  

            i = j;  

            j = 2 * i + 1;  

        }  

        a[i] = temp;  

    }  

  

    // 希尔排序  

    public static void ShellSort(int[] a, int size) {  

        int temp, k;  

        for (int i = size / 2; i > 0; i /= 2) {  

            for (int j = i; j < size; j = j + i) {  

                temp = a[j];  

                k = j;  

                while (k - i >= 0 && temp < a[k - i]) {  

                    a[k] = a[k - i];  

                    k = k - i;  

                }  

                a[k] = temp;  

            }  

        }  

    }  

  

    // 归并排序  

    public static void MergeSort(int[] a, int size) {  

        RecMerge(a, 0, size - 1);  

    }  

  

    public static void RecMerge(int[] a, int low, int high) {  

        if (low < high) {  

            int middle = (low + high) / 2;  

            RecMerge(a, low, middle);  

            RecMerge(a, middle + 1, high);  

            Merge(a, low, middle, high);  

        }  

    }  

  

    public static void Merge(int[] a, int low, int middle, int high) {  

        int temp1[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };  

        int temp2[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };  

        int n1 = middle - low + 1;  

        int n2 = high - middle;  

  

        for (int i = 0; i < n1; i++) {  

            temp1[i] = a[low + i];  

        }  

        for (int i = 0; i < n2; i++) {  

            temp2[i] = a[middle + i + 1];  

        }  

        temp1[n1] = 100;  

        temp2[n2] = 100;  

        int j = 0, k = 0;  

        for (int i = low; i <= high; i++) {  

            if (temp1[j] < temp2[k]) {  

                a[i] = temp1[j];  

                j++;  

            } else {  

                a[i] = temp2[k];  

                k++;  

            }  

        }  

    }  

  

    // 计数排序  

    public static void CountSort(int a[], int size) {  

        int MAX = 0;  

        for (int i = 0; i < size; i++) {  

            if (a[i] > MAX)  

                MAX = a[i];  

        }  

        MAX++;  

        List<Integer> temp = new ArrayList<Integer>();  

        for (int i = 0; i < MAX; i++) {  

            temp.add(i, 0);  

        }  

  

        for (int i = 0; i < size; i++) {  

            temp.set(a[i], (Integer) (temp.get(a[i])) + 1);  

        }  

  

        int j, k = 0;  

        for (int i = 0; i < MAX; i++) {  

            j = temp.get(i);  

            while (j != 0) {  

                a[k] = i;  

                k++;  

                j--;  

            }  

        }  

    }  

  

    public static void Swap(int a[], int i, int j) {  

        int temp;  

        temp = a[j];  

        a[j] = a[i];  

        a[i] = temp;  

    }  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  面试题