Java8种排序算法学习
阅读原文时间:2023年07月09日阅读:1

冒泡排序

public class test {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int numbers[] = { 6, 2, 4, 1, 5, 9 };
        BubbleSort(numbers);
    }  

    public static void BubbleSort(int [] numbers){
        for (int i = 0; i < numbers.length - 1; i++) { // 最多做n-1趟排序
            for (int j = 0; j < numbers.length - i - 1; j++) { // 对当前无序区间numbers[0......n-i-1]进行排序(j的范围很关键,这个范围是在逐步缩小的)
                if (numbers[j] > numbers[j + 1]) { // 把大的值交换到后面
                    int temp = numbers[j];
                    numbers[j] = numbers[j + 1];
                    numbers[j + 1] = temp;
                }
            }
        }
        System.out.print("最终排序结果:"+" ");
        for (int i = 0; i < numbers.length; i++) {
            System.out.print(numbers[i]+" ");
        }
    }
}  

选择排序

public class test {
    public static void main(String[] numbersrgs) {
        int [] numbers={6,2,4,1,9,8,3};
        SelectionSort(numbers);
        System.out.print("选择排序结果为:  ");
        for (int i : numbers)
            System.out.print(i + " ");
    }  

    private static void SelectionSort(int [] numbers){
         int n = numbers.length;
            for (int i = 0; i < n; i++) {
                int k = i;
                // 找出最小值的小标,找到之后赋值给K,numbers[k]即为最小值
                for (int j = i + 1; j < n; j++) {
                    if (numbers[j] < numbers[k]) {
                        k = j;
                    }
                }
                // 将最小值放到排序序列末尾
                if (k > i) {
                    int tmp = numbers[i];
                    numbers[i] = numbers[k];
                    numbers[k] = tmp;
                }
            }
    }
}  

插入排序

public class test2{
    public static void main(String[] args) {
        int [] nums={8,6,10,5,7,9,11};
        sortCore(nums);
    }  

    private static void sortCore(int[] array) {
        int arraySize = array.length;  

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

            int waitInsert = array[i];
            while(j > 0 && waitInsert < array[j - 1]) {
                array[j] = array[j - 1];
                j--;
            }
            array[j] = waitInsert;
        }  

        System.out.print("最终的排序为:");
        for(int i:array)
            System.out.print(i+" ");
    }
}  

归并排序

public class test_cow {
    public static void main(String[] args) {
        int [] numbers={6,2,4,1,9,8,3,15};
        sort(numbers);
        System.out.print("归并排序的结果为:");
        for(int i:numbers){
            System.out.print(i+" ");
        }  

    }  

    public static void sort(int[] data) {
        int[] temp = new int[data.length];
        mergeSort(data, temp, 0, data.length - 1);
    }  

    private static void mergeSort(int[] data, int[] temp, int left, int right) {
        int mid = (left + right) / 2;
        if (left == right)
            return;
        mergeSort(data, temp, left, mid);
        mergeSort(data, temp, mid + 1, right);  

        for (int i = left; i <= right; i++) {
            temp[i] = data[i];
        }
        int i1 = left;
        int i2 = mid + 1;
        for (int cur = left; cur <= right; cur++) {
            if (i1 == mid + 1)
                data[cur] = temp[i2++];
            else if (i2 > right)
                data[cur] = temp[i1++];
            else if (temp[i1] < temp[i2])
                data[cur] = temp[i1++];
            else
                data[cur] = temp[i2++];
        }
    }
}  

堆排序

public class test_sort {  

    public static void main(String[] args) {
        int[] nums = new int[] { 5, 3, 6, 2, 1, 9, 4, 8, 7 };
        print(nums);
        heapSort(nums);
        System.out.println("排序后的数组:");
        print(nums);
    }  

    public static void swap(int[] data, int i, int j) {
        if (i == j) {
            return;
        }
        data[i] = data[i] + data[j];
        data[j] = data[i] - data[j];
        data[i] = data[i] - data[j];
    }  

    public static void heapSort(int[] data) {
        for (int i = 0; i < data.length; i++) {
            createMaxdHeap(data, data.length - 1 - i);
            swap(data, 0, data.length - 1 - i);
            print(data);
        }
    }  

    public static void createMaxdHeap(int[] data, int lastIndex) {
        for (int i = (lastIndex - 1) / 2; i >= 0; i--) {
            // 保存当前正在判断的节点
            int k = i;
            // 若当前节点的子节点存在
            while (2 * k + 1 <= lastIndex) {
                // biggerIndex总是记录较大节点的值,先赋值为当前判断节点的左子节点
                int biggerIndex = 2 * k + 1;
                if (biggerIndex < lastIndex) {
                    // 若右子节点存在,否则此时biggerIndex应该等于 lastIndex
                    if (data[biggerIndex] < data[biggerIndex + 1]) {
                        // 若右子节点值比左子节点值大,则biggerIndex记录的是右子节点的值
                        biggerIndex++;
                    }
                }
                if (data[k] < data[biggerIndex]) {
                    // 若当前节点值比子节点最大值小,则交换2者得值,交换后将biggerIndex值赋值给k
                    swap(data, k, biggerIndex);
                    k = biggerIndex;
                } else {
                    break;
                }
            }
        }
    }  

    public static void print(int[] data) {
        for (int i = 0; i < data.length; i++) {
            System.out.print(data[i] + " ");
        }
        System.out.println();
    }
}  

希尔排序

public class test_sort {
    public static int count = 0;
    public static void main(String[] args) {  

        int[] nums = new int[] { 5, 3, 6, 2, 1, 9, 4, 8, 7 };
        print(nums);
        shellSort(nums);
        System.out.println("希尔排序最终结果为:");
        print(nums);
    }  

    public static void shellSort(int[] data) {
        // 计算出最大的h值
        int h = 1;
        while (h <= data.length / 3) {
            h = h * 3 + 1;
        }
        while (h > 0) {
            for (int i = h; i < data.length; i += h) {
                if (data[i] < data[i - h]) {
                    int tmp = data[i];
                    int j = i - h;
                    while (j >= 0 && data[j] > tmp) {
                        data[j + h] = data[j];
                        j -= h;
                    }
                    data[j + h] = tmp;
                    print(data);
                }
            }
            // 计算出下一个h值
            h = (h - 1) / 3;
        }
    }  

    public static void print(int[] data) {
        for (int i = 0; i < data.length; i++) {
            System.out.print(data[i] + "\t");
        }
        System.out.println();
    }  

}  

基数排序

import java.util.Arrays;  

public class test_sort {  

    public static void main(String[] args) {
        int[] nums = new int[] { 102, 52, 21, 12, 23 ,1,50,63,5,98};
        print(nums);
        radixSort(nums, 10, 4);
        System.out.println("基数排序后的数组:");
        print(nums);
    }  

    public static void radixSort(int[] data, int radix, int d) {
        // 缓存数组
        int[] tmp = new int[data.length];
        // buckets用于记录待排序元素的信息
        // buckets数组定义了max-min个桶
        int[] buckets = new int[radix];  

        for (int i = 0, rate = 1; i < d; i++) {  

            // 重置count数组,开始统计下一个关键字
            Arrays.fill(buckets, 0);
            // 将data中的元素完全复制到tmp数组中
            System.arraycopy(data, 0, tmp, 0, data.length);  

            // 计算每个待排序数据的子关键字
            for (int j = 0; j < data.length; j++) {
                int subKey = (tmp[j] / rate) % radix;
                buckets[subKey]++;
            }  

            for (int j = 1; j < radix; j++) {
                buckets[j] = buckets[j] + buckets[j - 1];
            }  

            // 按子关键字对指定的数据进行排序
            for (int m = data.length - 1; m >= 0; m--) {
                int subKey = (tmp[m] / rate) % radix;
                data[--buckets[subKey]] = tmp[m];
            }
            rate *= radix;
        }  

    }  

    public static void print(int[] data) {
        for (int i = 0; i < data.length; i++) {
            System.out.print(data[i] + "\t");
        }
        System.out.println();
    }  

}  

快速排序

/* 基于分治的思想,是冒泡排序的改进型
 * 快速排序和归并排序都使用分治法来设计算法,区别在于归并排序把数组分为两个基本等长的子数组,
 * 分别排好序之后还要进行归并(Merge)操作,而快速排序拆分子数组的时候显得更有艺术,取一个基准元素,
 * 拆分之后基准元素左边的元素都比基准元素小,右边的元素都不小于基准元素,这样只需要分别对两个子数组排序即可,
 * 不再像归并排序一样需要归并操作。
 * */
public class test_cow{  

    public static void main(String []args){
       int[] nums = {6,2,4,1,9,8,3,15};
       int start = 0;
       int end = nums.length-1;
       sort(nums,start,end);
       System.out.print("快速排序后的结果为:");
       for(int i:nums){
            System.out.print(i+" ");
        }  

    }  

    private static void sort(int[] a,int low,int high){
        int start = low;
        int end = high;
        int key = a[low];  

        while(end>start){                    //从后往前比较
            while(end>start&&a[end]>=key)   //如果没有比关键值小的,比较下一个,直到有比关键值小的交换位置,然后又从前往后比较
                end--;
            if(a[end]<=key){
                int temp = a[end];
                a[end] = a[start];
                a[start] = temp;
            }
            //从前往后比较
            while(end>start&&a[start]<=key)//如果没有比关键值大的,比较下一个,直到有比关键值大的交换位置
               start++;
            if(a[start]>=key){
                int temp = a[start];
                a[start] = a[end];
                a[end] = temp;
            }
        //  此时第一次循环比较结束,关键值的位置已经确定了。左边的值都比关键值小,右边的值都比关键值大,
        //  但是两边的顺序还有可能是不一样的,进行下面的递归调用
        }  

        if(start>low) sort(a,low,start-1);//左边序列。第一个索引位置到关键值索引-1
        if(end<high) sort(a,end+1,high);//右边序列。从关键值索引+1到最后一个
    }
}  

原文地址:

http://blog.csdn.net/sinat_22797429/article/details/64444248