常用排序算法实现总结(Java)

1. 交换排序
import java.util.Arrays;
import java.util.Random;

//交换排序
public class Exchange {

   //冒泡排序
    public static void bubbleSort(int[] arr) {
        for(int i = 0;i < arr.length - 1;i++)
            for(int j = 0;j<arr.length - i - 1;j++)
                if(arr[j] > arr[j + 1]) {
                    int tmp = arr[j];arr[j] = arr[j+1];arr[j+1] = tmp;   
                }
    }

    //快速排序(递归)
    public static void quickSort(int[] arr, int left, int right) {
        if(left < right) {
            int privotLoc = partition(arr, left, right);
            quickSort(arr, left, privotLoc);
            quickSort(arr, privotLoc + 1, right);
        }
    }

    private static int partition(int[] arr, int left, int right) {
        int privotKey = arr[left];
        while(left < right) {
            while(left < right && arr[right] >= privotKey) --right;
            int tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
            while(left < right && arr[left] < privotKey) ++left;
            tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
        }
        return left;
    }

    public static void main(String[] args) {
        int[] arr = new Random().ints(12, 1, 30).toArray();
        System.out.println(Arrays.toString(arr));
        bubbleSort(arr);
        //quickSort(arr, 0, arr.length - 1);
        System.out.println(Arrays.toString(arr));
    }
}
2. 插入排序
public class Insert {

    //直接插入排序 -- 稳定
    public static void straightInsertion(int[] arr) {
        for(int i = 1;i < arr.length;i++) {
            if(arr[i] < arr[i - 1]) {
                int index = i - 1; //有序表界限下标
                int x = arr[i]; //取出待排序元素
                arr[i] = arr[i - 1]; //空出的位置留给哨兵
                while(index >= 0) {//有序表后移
                    if(x < arr[index])
                        arr[index + 1] = arr[index];
                    else
                        break;
                    index--;
                }
                arr[index + 1] = x;//插入待排序元素
            }
            System.out.println(i + ": " + Arrays.toString(arr));
        }
    }

    //希尔排序又叫缩小增量排序 -- 不稳定
    public static void shellInsertion(int[] arr) {
        int dis = arr.length/2;
        while(dis >= 1) {
            shellInsertion(arr, dis);
            System.out.println(dis + ": " + Arrays.toString(arr));
            dis--;
        }
    }

    private static void shellInsertion(int[] arr, int dis) {
        for(int i = dis;i < arr.length;i++) {
            if(arr[i] < arr[i - dis]) {
                int index = i - dis;
                int x = arr[i];
                while(index >= 0) {
                    if(x < arr[index])
                        arr[index + dis] = arr[index];
                    else
                        break;
                    index -= dis;
                }
                arr[index + dis] = x;
            }
        }
    }

    public static void main(String[] args) {
        //int[] arr = new Random().ints(10, 0, 20).toArray();
        int[] arr = new int[]{49,38,65,97,76,13,27};
        System.out.println(Arrays.toString(arr));
        //straightInsertion(arr);
        shellInsertion(arr);
        System.out.println(Arrays.toString(arr));
    }
}
3. 归并排序
import java.util.Arrays;
import java.util.Random;

public class Merge {

    public static void mergeSort(int[] arr, int low, int high) {
        int mid = (low + high)/2;
        if(low < high) {
            mergeSort(arr, low, mid);
            mergeSort(arr, mid + 1, high);
            merge(arr, low, mid, high);
        }
    }

    private static void merge(int[] arr, int low, int mid, int high) {
        int[] tmp = new int[high - low + 1];
        int i = low;
        int j = mid + 1;
        int k = 0;
        while(i <= mid && j <= high) {
            if(arr[i] < arr[j])
                tmp[k++] = arr[i++];
            else
                tmp[k++] = arr[j++];
        }

        // 把左边或右边剩余的数移入数组
        while(i<=mid) tmp[k++] = arr[i++];
        while(j<=high) tmp[k++] = arr[j++];


        for(int x = 0;x < tmp.length;x++)
            arr[x + low] = tmp[x];

    }

    public static void main(String[] args) {
        int[] arr = new Random().ints(12, 1, 30).toArray();
        System.out.println(Arrays.toString(arr));
        mergeSort(arr, 0, arr.length);
        System.out.println(Arrays.toString(arr));
    }
}

4. 选择排序
import java.util.Arrays;

public class Select {

    //简单选择排序
    public static void simpleSelection(int[] arr) {
        int index = -1;
        for(int i = 0;i<arr.length;i++) {
            index = minIndex(arr, i);
            if(index != i) {
                int tmp = arr[index];
                arr[index] = arr[i];
                arr[i] = tmp;
            }
            System.out.println(i + 1 + ":" + Arrays.toString(arr));
        }
    }

    //找到这个数字之后的最小数字的下标
    private static int minIndex(int[] arr, int start) {
        int res = start;
        for(int i = start + 1;i < arr.length;i++)
            if(arr[i] < arr[res])
                res = i;
        return res;
    }

    //堆排序
    public static void heapSelection(int[] arr) {
        initHeap(arr);
        for(int i = arr.length - 1;i>=0;i--) {
            int temp = arr[i]; arr[i] = arr[0]; arr[0] = temp;
            adjustHeap(arr, 0, i);
        }
    }

    private static void initHeap(int[] heap) {
        for(int i = (heap.length - 1)/2;i >= 0;i--)
            adjustHeap(heap, i, heap.length);
    }

    private static void adjustHeap(int[] heap, int i, int length) {
        int tmp = heap[i];
        int child = 2 * i + 1;//左子节点的位置
        while(child < length) {
            if(child + 1 < length && heap[child] < heap[child + 1])
                ++child;
            if(heap[i] < heap[child]) {
                heap[i] = heap[child];
                i = child;
                child = 2 * i + 1;
            } else
                break;
            heap[i] = tmp;
        }
    }

    public static void main(String[] args) {
        int[] arr = new int[]{49,38,65,97,76,13,27};
        System.out.println(Arrays.toString(arr));
        //simpleSelection(arr);
        heapSelection(arr);
        System.out.println(Arrays.toString(arr));
    }
}
5. 桶排序(LSD)
import java.util.Arrays;
import java.util.Random;

public class Radix {

    /**
     * 这是《算法》里的LSD
     * @param a
     */
    public static void radixSort(int[] a) {
        final int BITS = 32;
        final int R = 1 << 8;
        final int MASK = R - 1;
        final int w = BITS/8;
        int n = a.length;
        int[] aux = new int[n];
        for(int d = 0;d < w;d++) {
            int[] count = new int[R + 1];

            //compute frequency count
            for(int i = 0;i < n;i++) {
                int c = (a[i] >> 8*d) & MASK;
                count[c + 1]++;
            }
            //System.out.println("freq:" + Arrays.toString(count));

            // compute cumulates
            for (int r = 0; r < R; r++)
                count[r+1] += count[r];
            System.out.println("cumu:" + Arrays.toString(count));

            // for most significant byte, 0x80-0xFF comes before 0x00-0x7F
            if (d == w-1) {
                int shift1 = count[R] - count[R/2];
                int shift2 = count[R/2];
                for (int r = 0; r < R/2; r++)
                    count[r] += shift1;
                for (int r = R/2; r < R; r++)
                    count[r] -= shift2;
            }
            System.out.println("sign:" + Arrays.toString(count));

            // move data
            for (int i = 0; i < n; i++) {
                int c = (a[i] >> 8*d) & MASK;
                aux[count[c]++] = a[i];
            }
            System.out.println("move:" + Arrays.toString(aux));

            // copy back
            for (int i = 0; i < n; i++)
                a[i] = aux[i];
        }
    }

    public static void main(String[] args) {
        int[] arr = new Random().ints(10, 1, 20).toArray();
        System.out.println(Arrays.toString(arr));
        radixSort(arr);
        System.out.println(Arrays.toString(arr));
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值