用Java实现的几种常见排序算法

冒泡排序

冒泡排序的基本思想是:通过对待排序序列从前往后(从小标较小的元素开始),依次比较相邻元素的值,若发现逆序则交换,使得值较大的元素逐渐从前往后移动,就像水底下的气泡一样逐渐向上冒。

优化:因为排序的过程中,各元素不断接近自己的位置,如果一趟比较下来没有进行过交换就说明序列有序。因此要在排序过程中设置一个flag判断元素是否进行过交换,从而减少不必要的比较
冒泡排序的特点:

  • 长度为n的数组需要(n-1)次循环
  • 每一趟排序的次数逐渐减少

    public class BubbleSort {

    1. public static void main(String[] args) {
    2. //定义一个数组
    3. int[] arr = { 6,12,9,-3,2};
    4. //变量用于交换数据
    5. int temp = 0;
    6. //外层循环控制比较次数
    7. for (int i = 0;i < arr.length-1;i++){
    8. //内层循环遍历要比较的元素
    9. for (int j = 0;j<arr.length-1-i;j++){
    10. if (arr[j] > arr[j+1]){
    11. temp = arr[j];
    12. arr[j] = arr[j+1];
    13. arr[j+1] = temp;
    14. }
    15. }
    16. System.out.println("第"+(i+1)+"趟排序后:");
    17. System.out.println(Arrays.toString(arr));
    18. }
    19. }

    }
    /输出结果 第1趟排序后: [6, 9, -3, 2, 12] 第2趟排序后: [6, -3, 2, 9, 12] 第3趟排序后: [-3, 2, 6, 9, 12] 第4趟排序后: [-3, 2, 6, 9, 12] /

代码优化

  1. public class BubbleSort {
  2. public static void main(String[] args) {
  3. //定义一个数组
  4. int[] arr = { 6,12,9,-3,2};
  5. //变量用于交换数据
  6. int temp = 0;
  7. //外层循环控制比较次数
  8. for (int i = 0;i < arr.length-1;i++){
  9. //标识变量
  10. boolean flag = false;
  11. //内层循环遍历要比较的元素
  12. for (int j = 0;j<arr.length-1-i;j++){
  13. if (arr[j] > arr[j+1]){
  14. //如果有交换,设为true
  15. flag = true;
  16. temp = arr[j];
  17. arr[j] = arr[j+1];
  18. arr[j+1] = temp;
  19. }
  20. }
  21. //flag为false,表明没有数据交换,排序已完成
  22. if (!flag)
  23. break;
  24. System.out.println("第"+(i+1)+"趟排序后:");
  25. System.out.println(Arrays.toString(arr));
  26. }
  27. }
  28. }
  29. /*输出结果 第1趟排序后: [6, 9, -3, 2, 12] 第2趟排序后: [6, -3, 2, 9, 12] 第3趟排序后: [-3, 2, 6, 9, 12] */

选择排序

基本思想:第一次从arr[0]-arr[n-1]中选取最小值,与arr[0]交换;第二次从arr[1]-arr[n-1]中选取最小值,与arr[1]交换;第三次从arr[2]-arr[n-1]中选取最小值,与arr[2]交换,…,第n-1次从arr[n-2]-arr[n-1]中选出最小值,与arr[n-2]交换,总共通过n-1次得到一个从小到大排序的有序序列。

  1. public class SelectSort {
  2. public static void main(String[] args) {
  3. int[] arr = { 8,7,23,14,2};
  4. //记录最小值的下标
  5. int minIndex;
  6. //记录最小值
  7. int min;
  8. //外层循环控制排序次数
  9. for (int i = 0;i < arr.length-1;i++){
  10. //把本趟排序的第一个数假设为最小值
  11. minIndex = i;
  12. min = arr[i];
  13. for (int j = i+1;j<arr.length;j++){
  14. if (min > arr[j]){
  15. min = arr[j];
  16. minIndex = j;
  17. }
  18. }
  19. //如果找到比假定值还小的数,交换
  20. if (minIndex != i){
  21. arr[minIndex] = arr[i];
  22. arr[i] = min;
  23. }
  24. System.out.println("第"+(i+1)+"趟排序后:");
  25. System.out.println(Arrays.toString(arr));
  26. }
  27. }
  28. }
  29. /* 第1趟排序后: [2, 7, 23, 14, 8] 第2趟排序后: [2, 7, 23, 14, 8] 第3趟排序后: [2, 7, 8, 14, 23] 第4趟排序后: [2, 7, 8, 14, 23] */

直接插入排序

直接插入排序属于内部排序,是对于欲排序的元素以插入的方式找寻该元素的适当位置以达到排序的目的。其基本思想是:
把n个待排序的元素看成为一个有序表和一个无序表,开始时有序表中只有一个元素,无序表中包含有n-1个元素,排序过程中每次从无序表中取出第一个元素,把它的排序码一次与有序表元素的排序码进行比较,将它插入到有序表中的适当位置,使之成为新的有序表。

  1. public class InsertSort {
  2. public static void main(String[] args) {
  3. int[] arr = { 32,56,12,24,5,15};
  4. insertSort(arr);
  5. }
  6. static void insertSort(int[] arr){
  7. int insertVal;
  8. int insertIndex;
  9. for (int i = 1;i<arr.length;i++){
  10. insertVal = arr[i];
  11. insertIndex = i -1;
  12. while (insertIndex >= 0&&insertVal < arr[insertIndex]){
  13. arr[insertIndex+1] = arr[insertIndex];
  14. insertIndex--;
  15. }
  16. //退出while循环,说明位置已经找到
  17. arr[insertIndex+1] = insertVal;
  18. System.out.println("第"+i+"轮排序:");
  19. System.out.println(Arrays.toString(arr));
  20. }
  21. }
  22. }
  23. /** 第1轮排序: [32, 56, 12, 24, 5, 15] 第2轮排序: [12, 32, 56, 24, 5, 15] 第3轮排序: [12, 24, 32, 56, 5, 15] 第4轮排序: [5, 12, 24, 32, 56, 15] 第5轮排序: [5, 12, 15, 24, 32, 56] */

n个数据要进行n-1次排序

希尔排序

希尔排序又叫做缩小增量排序,是直接插入排序的更高效版本。基本思想是:把记录按下标的一定增量分组,对每组使用直接插入排序的算法进行排序。随着增量的逐渐减少,每组包含的关键词越来越多,当增量减至1,整组数据就分为一组,算法终止。
在直接插入排序中,如果出现{2,3,4,5,6,1}这样的序列,要想从小到大排序,只需将1直接放到第一个位置即可,但是也要逐次比较到最后,效率很低,这时可用希尔排序替代。

  1. public class InsertDemo02 {
  2. public static void main(String[] args) {
  3. int[] arr = new int[] { 8,9,1,7,2,3,5,4,6,0};
  4. System.out.println("初始序列"+Arrays.toString(arr));
  5. shellSort(arr);
  6. }
  7. public static void shellSort(int[] arr){
  8. int cnt = 0;
  9. //遍历所有步长
  10. for(int d = arr.length/2;d > 0;d/=2) {
  11. //遍历所有元素
  12. for (int i = d; i < arr.length; i++) {
  13. //遍历本组中所有的元素
  14. for (int j = i - d; j >= 0; j -= d) {
  15. //如果当前元素大于加上步长的那个元素
  16. if (arr[j] > arr[j + d]) {
  17. int temp = arr[j];
  18. arr[j] = arr[j + d];
  19. arr[j + d] = temp;
  20. }
  21. }
  22. }
  23. System.out.println("第"+(++cnt)+"轮排序后:"+Arrays.toString(arr));
  24. }
  25. }
  26. }
  27. /** 初始序列[8, 9, 1, 7, 2, 3, 5, 4, 6, 0] 第1轮排序后:[3, 5, 1, 6, 0, 8, 9, 4, 7, 2] 第2轮排序后:[0, 2, 1, 4, 3, 5, 7, 6, 9, 8] 第3轮排序后:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] */

上面实现的希尔排序中对数据的处理采用交换的方式,整体的效率是不高的,当数据是几万个的时候,它的效果甚至不如直接插入排序。将交换的部分进行优化,将它改成直接插入排序的移位法:

  1. public class InsertDemo02 {
  2. public static void main(String[] args) {
  3. int[] arr = new int[] { 8,9,1,7,2,3,5,4,6,0};
  4. System.out.println("初始序列"+Arrays.toString(arr));
  5. shellSort1(arr);
  6. }
  7. public static void shellSort1(int[] arr){
  8. int cnt = 0;
  9. int insertIndex;
  10. int temp;
  11. //遍历所有步长
  12. for(int d = arr.length/2;d > 0;d/=2) {
  13. //遍历所有元素
  14. for (int i = d; i < arr.length; i++) {
  15. insertIndex = i;
  16. temp = arr[insertIndex];
  17. if (arr[insertIndex] < arr[insertIndex-d]) {
  18. while (insertIndex-d >= 0 && temp < arr[insertIndex - d]) {
  19. arr[insertIndex] = arr[insertIndex - d];
  20. insertIndex -= d;
  21. }
  22. arr[insertIndex] = temp;
  23. }
  24. }
  25. System.out.println("第"+(++cnt)+"轮排序后:"+Arrays.toString(arr));
  26. }
  27. }
  28. }
  29. /** 初始序列[8, 9, 1, 7, 2, 3, 5, 4, 6, 0] 第1轮排序后:[3, 5, 1, 6, 0, 8, 9, 4, 7, 2] 第2轮排序后:[0, 2, 1, 4, 3, 5, 7, 6, 9, 8] 第3轮排序后:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] */

快速排序

快速排序是对冒泡排序的一种改进,基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对两部分数据分别进行快速排序,整个排序过程可以用递归进行。

  1. public class QuickSort {
  2. public static void main(String[] args) {
  3. int[] arr={ -9,12,0,-32,78,1};
  4. quickSort(arr,0,arr.length-1);
  5. System.out.println("排序后:"+ Arrays.toString(arr));
  6. }
  7. public static void quickSort(int[] arr,int start,int end){
  8. //当start==end时递归结束
  9. if(start < end){
  10. //把数组中的第0个数作为标准数
  11. int standard = arr[start];
  12. //记录需要排序的下标
  13. int low = start;
  14. int high = end;
  15. //循环找标准数大的数和比标准数小的数
  16. while(low < high) {
  17. //右边的数字比标准数大
  18. while(low < high && arr[high] >= standard) {
  19. high--;
  20. }
  21. //使用右边的数字替换左边的数字
  22. arr[low] = arr[high];
  23. //左边的数字比标准数小
  24. while(low < high && arr[low] <= standard){
  25. low++;
  26. }
  27. //左边的数字替换右边的数字
  28. arr[high] = arr[low];
  29. //当low == high时
  30. arr[low] = standard;
  31. //处理所有小的数字
  32. quickSort(arr,start,low);
  33. //处理所有大的数字
  34. quickSort(arr,low+1,end);
  35. }
  36. }
  37. }
  38. }
  39. /** 排序后:[-32, -9, 0, 1, 12, 78] */

归并排序

归并排序是利用归并的思想实现的排序方法,该算法采用经典的分治策略(分治法将问题成一些小问题然后递归求解,而的阶段则将分的阶段得到的各答案“修补”在一起,即分而治之)。

  1. public static void main(String[] args) {
  2. int[] arr = new int[] { 3,6,1,9,3,2};
  3. System.out.println(Arrays.toString(arr));
  4. mergeSort(arr,0,arr.length-1);
  5. System.out.println(Arrays.toString(arr));
  6. }
  7. public static void mergeSort(int[] arr,int low,int high){
  8. int middle = (high + low)/2;
  9. if(low < high) {
  10. //处理左边
  11. mergeSort(arr, low, middle);
  12. //处理右边
  13. mergeSort(arr, middle + 1, high);
  14. //归并
  15. merge(arr, low, middle, high);
  16. }
  17. }
  18. public static void merge(int[] arr,int low,int middle,int high){
  19. //用于存储归并后的临时数组
  20. int[] temp = new int[high - low +1];
  21. //记录第一个数组中需要遍历的下标
  22. int i = low;
  23. //记录第二个数组中需要遍历的下标
  24. int j = middle + 1;
  25. //用于记录在临时数组中存放的下标
  26. int index = 0;
  27. //遍历两个数组取出小的数字,放入临时数组中
  28. while (i <= middle && j <= high){
  29. //第一个数组的数据更小
  30. if (arr[i] <= arr[j]){
  31. //把小的数据放入临时数组中
  32. temp[index] = arr[i];
  33. //让下标向后移一位
  34. i++;
  35. }else{
  36. temp[index] = arr[j];
  37. j++;
  38. }
  39. index++;
  40. }
  41. //处理多余的数据
  42. while(j <= high){
  43. temp[index] = arr[j];
  44. j++;
  45. index++;
  46. }
  47. while (i <= middle){
  48. temp[index] = arr[i];
  49. i++;
  50. index++;
  51. }
  52. //把临时数组中的数据重新存入原数组
  53. for(int k = 0;k < temp.length;k++){
  54. arr[k+low] = temp[k];
  55. }
  56. }

基数排序

基数排序属于“分配式排序”,又称“桶子法”,通过键值的各个位的值,将要排序的元素分配到某些桶中,达到排序的目的。

  1. public class RadixSortDemo1 {
  2. public static void main(String[] args) {
  3. int[] arr = { 32,34,12,3,21,15,413,124};
  4. radixSort(arr);
  5. }
  6. //基数排序方法
  7. public static void radixSort(int[] arr){
  8. //得到数组中最大的数
  9. int max = arr[0];
  10. for (int i = 1; i < arr.length; i++) {
  11. if (max < arr[i])
  12. max = arr[i];
  13. }
  14. //得到最大数是几位数
  15. int maxLength = (max + "").length();
  16. //定义一个二维数组,表示10个桶,每个桶就是一个一维数组
  17. //基数排序使用空间换时间
  18. int[][] bucket = new int[10][arr.length];
  19. //记录每个桶中,实际存放了多少个数据,我们定义一个一维数组来记录各个桶的每次放入的数据个数
  20. //比如bucketElementCounts[0],记录的是bucket[0]这个桶放入数据的个数
  21. int[] bucketElementCounts = new int[10];
  22. for (int i = 0,n=1;i < maxLength;i++,n*=10){
  23. //对每个元素的对应位进行排序处理,第一次是个位,第二位是十位...
  24. for (int j = 0;j < arr.length;j++){
  25. //取出每个元素的对应位的值
  26. int digitOfElement = arr[j]/n % 10;
  27. //放入到对应的桶中
  28. bucket[digitOfElement][bucketElementCounts[digitOfElement]] = arr[j];
  29. bucketElementCounts[digitOfElement]++;
  30. }
  31. //按照一维数组的下标依次取出数据,放入到原来的数组
  32. int index = 0;
  33. //遍历每一个桶,并将桶中的数据放入到原来数组
  34. for (int k = 0; k < bucketElementCounts.length; k++) {
  35. //如果桶中有数据
  36. if (bucketElementCounts[k] != 0){
  37. //循环该桶即第k个桶(第k个一维数组),放入
  38. for (int l = 0; l < bucketElementCounts[k]; l++) {
  39. arr[index++] = bucket[k][l];
  40. }
  41. }
  42. //第i+1轮处理后需要将每个bucketElementCounts[k]=0,即清空
  43. bucketElementCounts[k] = 0;
  44. }
  45. System.out.println("第"+(i+1)+"轮排序后:"+ Arrays.toString(arr));
  46. }
  47. }
  48. }
  49. /* 第1轮排序后:[21, 32, 12, 3, 413, 34, 124, 15] 第2轮排序后:[3, 12, 413, 15, 21, 124, 32, 34] 第3轮排序后:[3, 12, 15, 21, 32, 34, 124, 413] */

说明:基数排序是对传统桶排序的扩展,速度很快,可以比快速排序的速度快,是经典的空间换时间的方式,占用内存空间很大(有10个桶,每个桶的长度和 原来数组一样,也就是有11个和原来数组一样大的数组)。当对海量数据排序时容易造成OutOfMemoryError。
|排序算法|平均时间复杂度|最好情况|最坏情况|空间复杂度|稳定性






































































排序算法 平均时间复杂度 最好情况 最坏情况 空间复杂度 稳定性
冒泡排序 O(n2) O(n) O(n2) O(1) 稳定
选择排序 O(n2) O(n2) O(n2) O(1) 不稳定
直接插入排序 O(n2) O(n) O(n2) O(1) 稳定
希尔排序 O(nlogn) O(nlog2n) O(nlog2n) O(1) 不稳定
归并排序 O(nlogn) O(nlogn) O(nlogn) O(n) 稳定
快速排序 O(nlogn) O(nlogn) O(n2) Ologn) 不稳定
基数排序 O(n×k) O(n×k) O(n×k) O(n+k) 稳定

时间复杂度:一个算法执行所耗费的时间
空间复杂度:运行完一个程序需要的内存大小
n:数据规模
k:桶的个数

发表评论

表情:
评论列表 (有 0 条评论,89人围观)

还没有评论,来说两句吧...

相关阅读