数据结构(三)之八种排序算法

.冒泡排序

package cn.liusong.Sort;

public class BubbleSort {
    public static void main(String[] args) {
        int[] arr = new int[]{35, 24, 78, 12, 34, 62, 54, 98, 71, 55, 32};
        BubbleSort bs = new BubbleSort();
        bs.bubbleSort(arr);
        bs.show(arr);
    }

    //冒泡排序----从小到大
    public void bubbleSort(int[] arr) {
        //控制比较多少轮
        for (int i = 0; i < arr.length - 1; i++) {
            //比较的次数
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }


    //打印排序后的数组元素
    public void show(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + "--->");
        }
    }
}

快速排序

package cn.liusong.Sort;

import java.lang.reflect.Array;
import java.util.Arrays;

public class QuickSort {
    public static void main(String[] args) {
        int[] arr = new int[]{8, 9, 7, 12, 5, 4, 10, 1, 3, 2};
        quickSort(arr, 0, arr.length - 1);
        System.out.println(Arrays.toString(arr));
    }

    //快速排序
    public static void quickSort(int[] arr, int start, int end) {
        if (start < end) {
            //记录下需要排序的下标
            int low = start;
            int high = end;
            //把数组中第0个元素作为标准数
            int stard = arr[start];
            //找出比标准数大的数和比标准数小的数
            while (low < high) {
                //右边的数比标准数大
                while (low < high && stard <= arr[high]) {
                    high--;
                }
                //使用右边的数字替换左边的数
                arr[low] = arr[high];
                //如果左边的数比标准数小
                while (low < high && arr[low] <= stard) {
                    low++;
                }
                //使用左边的数替换右边的数
                arr[high] = arr[low];
            }
            //把标准数赋给低所在的位置元素
            arr[low] = stard;
            //递归排列较小的一边
            quickSort(arr, start, low);
            //递归排列较大的一边
            quickSort(arr, low + 1, end);
        }
    }
}

插入排序

package cn.liusong.Sort;

import java.util.Arrays;

public class InsertSort {
    public static void main(String[] args) {
        int[] arr = new int[]{2, 5, 3, 1, 7, 6, 4, 9, 8};
        insertSort(arr);
        System.out.print(Arrays.toString(arr));
    }

    public static void insertSort(int[] arr) {
        //遍历所有的数字
        for (int i = 1; i < arr.length; i++) {
            //如果当前数字比前面数字小
            if (arr[i] < arr[i - 1]) {
                //j表示临时变量所在下标的前一个下标
                int j;
                //把当前数字赋给临时变量
                int temp = arr[i];
                //遍历当前数字前面的所有数字
                for (j = i - 1; j >= 0 && temp < arr[j]; j--) {
                    //把前一个数字赋给后一个数字
                    arr[j + 1] = arr[j];
                }
                //把临时变量(外层for循环的当前元素)赋给不满足条件的后一个值
                arr[j + 1] = temp;
            }
        }
    }
}

希尔排序

package cn.liusong.Sort;

import java.util.Arrays;

public class ShellSort {
    public static void main(String[] args) {
        int[] arr = new int[]{5, 6, 1, 7, 2, 6, 3, 5, 7, 9, 8, 0};
        shellSort(arr);
        System.out.print(Arrays.toString(arr));
    }

    public static void shellSort(int[] arr) {
        //遍历所有步长
        for (int d = arr.length / 2; d > 0; d = d / 2) {
            //遍历所有元素
            for (int i = d; i < arr.length; i++) {
                //遍历本组中所有元素
                for (int j = i - d; j >= 0; j -= d) {
                    //如果当前元素大于加上步长后的那个元素
                    if (arr[j] > arr[j + d]) {
                        int temp = arr[j];
                        arr[j] = arr[j + d];
                        arr[j + d] = temp;
                    }
                }
            }
        }
    }
}

选择排序

package cn.liusong.Sort;

import java.util.Arrays;

public class SelectSort {
    public static void main(String[] args) {
        int[] arr = new int[]{2, 4, 1, 9, 5, 7, 2, 4, 6, 7, 0};
        selectSort(arr);
        System.out.print(Arrays.toString(arr));
    }

    //选择排序
    public static void selectSort(int[] arr) {
        //遍历所有元素
        for (int i = 0; i < arr.length; i++) {
            int minIndex = i;
            //把当前最小的数与后面的是依次比较,并记录下最小的数的下标
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[minIndex] > arr[j]) {
                    //记录下最小的数的下标
                    minIndex = j;
                }
            }
            //如果最小的数和当前遍历是的下标不一致是,说明下标为minIndex的数比当前遍历的数更小
            if (i != minIndex) {
                int temp = arr[i];
                arr[i] = arr[minIndex];
                arr[minIndex] = temp;
            }
        }
    }
}

归并排序

package cn.liusong.Sort;

import java.util.Arrays;

public class MergeSort {
    public static void main(String[] args) {
        //int[] arr = new int[]{3,1} ;
        int[] arr = new int[]{1, 3, 5, 2, 4, 6, 8, 10};
        System.out.print(Arrays.toString(arr));
        mergeSort(arr, 0, arr.length - 1);
        System.out.print(Arrays.toString(arr));
    }


    public static void mergeSort(int[] arr, int low, int high) {
        int middle = (high + low) / 2;
        if (low < high) {
            //处理左边
            mergeSort(arr, low, middle);
            //处理右边
            mergeSort(arr, middle + 1, high);
            //归并
            merge(arr, low, middle, high);
        }
    }

    public static void merge(int[] arr, int low, int middle, int high) {
        int[] temp = new int[high - low + 1];
        //记录第一个需要遍历的数组的起始下标
        int i = low;
        //记录第二个需要遍历的数组的起始下标
        int j = middle + 1;
        //临时数组的下标
        int index = 0;
        while (i <= middle && j <= high) {
            if (arr[i] <= arr[j]) {
                temp[index] = arr[i];
                //让下标向后移一位
                i++;
            } else {
                temp[index] = arr[j];
                j++;
            }
            index++;
        }
        //处理多余数据
        while (j <= high) {
            temp[index] = arr[j];
            j++;
            index++;
        }
        while (i <= middle) {
            temp[index] = arr[i];
            i++;
            index++;
        }
        for (int k = 0; k < temp.length; k++) {
            arr[k + low] = temp[k];
        }
    }
}

基数排序

package cn.liusong.Sort;

import java.util.Arrays;

public class RadixSort {
    public static void main(String[] args) {
        int[] arr = new int[]{23, 6, 189, 45, 9, 287, 56, 1, 798, 34, 65, 652, 5};
        radixSort(arr);
        System.out.print(Arrays.toString(arr));
    }

    public static void radixSort(int[] arr) {
        //存数组中最大的数字
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        //最大数字数的位数
        int maxLength = (max + " ").length();
        //用于存储临时数组
        int[][] temp = new int[10][arr.length];
        //用于记录在temp中相应的数组中存放的数组的数量
        int[] counts = new int[10];
        //根据最大长度的数决定比较的次数
        for (int i = 0, n = 1; i < maxLength; i++, n *= 10) {
            //分别计算每一位数字的余数
            for (int j = 0; j < arr.length; j++) {
                //计算余数
                int ys = arr[j] / n % 10;
                //把当前遍历到的数据放入指定的数组中
                temp[ys][counts[ys]] = arr[j];
                //记录数量
                counts[ys]++;
            }
            //记录取出来的元素需要放的位置
            int index = 0;
            //把数字取出来
            for (int k = 0; k < counts.length; k++) {
                //记录数量的数组中当前余数记录的数量不为0
                if (counts[k] != 0) {
                    //循环取出元素
                    for (int l = 0; l < counts[k]; l++) {
                        arr[index] = temp[k][l];
                        index++;
                    }
                    //把数量置空为0
                    counts[k] = 0;

                }
            }
        }
    }
}

基数排序之队列实现

package cn.liusong.Sort;

import cn.liusong.Array.Queue;

import java.util.Arrays;

public class RadisQueueSort {
    public static void main(String[] args) {
        int[] arr = new int[]{23, 6, 189, 45, 9, 287, 56, 1, 798, 34, 65, 652, 5};
        radixSort(arr);
        System.out.print(Arrays.toString(arr));
    }

    public static void radixSort(int[] arr) {
        //存数组中最大的数字
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        //最大数字数的位数
        int maxLength = (max + " ").length();
        //用于临时存储数据队列的数组
        Queue[] temp = new Queue[10];
        //为队列数组赋值
        for (int i = 0; i < temp.length; i++) {
            temp[i] = new Queue();
        }
        //根据最大长度的数决定比较的次数
        for (int i = 0, n = 1; i < maxLength; i++, n *= 10) {
            //分别计算每一位数字的余数
            for (int j = 0; j < arr.length; j++) {
                //计算余数
                int ys = arr[j] / n % 10;
                //把当前遍历到的数据放入指定的队列中
                temp[ys].push(arr[j]);
                ;
            }
            //记录取出来的元素需要放的位置
            int index = 0;
            //把把所有队列中的数字取出来
            for (int k = 0; k < temp.length; k++) {
                //当前遍历的队列不为空
                while (!temp[k].isEmpty()) {
                    //循环取出元素
                    arr[index] = temp[k].pop();
                    index++;
                }
            }
        }
    }
}
原文地址:https://www.cnblogs.com/Lovis/p/10736944.html