Java【数据结构】—— 冒泡排序、选择排序、直接插入排序

一、排序的概念及稳定性

- 概念: 所谓排序,就是使一串记录,按照其中的某个或某些关键字的大小,递增或递减的排列起来的操作。

- 稳定性: 简单来说就是在一组还未排序的数据中,两个关键字具有相同属性(相当于两个数字相等,即5 = 5)再经过排序后两个关键字的顺序不变,如以下示例。

在这里插入图片描述

二、冒泡排序

1.基本思想

冒泡排序是一种交换排序,它的基本思想是: 两两比较相邻记录的关键字,如果反序则交换,知道没有反序的记录为止。

在这里插入图片描述
以上为一趟冒泡排序的结果,此时我们已经把一个数字放到了指定排好的位置,我们下一趟排序的时候就不对这个已排好的关键字进行操作,依次类推久能把剩下的数据排序。

2.代码实现

import java.util.Arrays;

public class Bubble {
    public void sort (int[] array) {
        for (int i = 0;i < array.length - 1;i++) {
            for (int j = 0;j < array.length - i - 1;j++) {
                if (array[j] > array[j+1]) {
                    swap(array,j,j+1);
                }
            }
        }
    }

    private void swap(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    public void print (int[] array) {
        System.out.println(Arrays.toString(array));
    }

}

//测试类
public class BubbleTest {
    public static void main(String[] args) {
        Bubble bubble = new Bubble();
        int[] array = {23, 51, 24, 82, 7, 31, 21, 36, 63};
        bubble.sort(array);
        bubble.print(array);
    }
}

三、选择排序

1.基本思想

  • 在数组中选择关键字最小(大)元素的下标
  • 将找到的下标与指定位置进行交换(第一次是0下标,依次类推)
  • 指定位置排序好后不在进行操作

在这里插入图片描述

  • 再进反复的操作知道数组有序

2.代码实现

public class Selection {
    public void sort (int[] array) {
        for (int i = 0;i < array.length;i++) {
            int minIndex = i;
            for (int j = i + 1;j < array.length;j++) {
                if (array[minIndex] > array[j]) {
                    minIndex = j;
                }
            }
            swap(array,minIndex,i);
        }
    }

    private void swap(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}

import java.util.Arrays;

public class SelectionTest {
    public static void main(String[] args) {
        Selection selection = new Selection();
        int[] array = {23, 51, 24, 82, 7, 31, 21, 36, 63};
        selection.sort(array);
        System.out.println(Arrays.toString(array));
    }
}

四、直接插入排序

1.基本思想

  • 把待排序的记录按其关键码值的大小逐个插入到一个已经排好序的有序序列中,直到所有的记录插入完为止,得到一个新的有序序列 。实际中我们玩扑克牌时,就用了插入排序的思想。

在这里插入图片描述

在这里插入图片描述

2.代码实现

public class Insertion {
    public void sort (int[] array) {
        for (int i = 1; i < array.length; i++) {
            for (int j = i - 1;j >= 0;j--) {
                if (array[j+1] < array[j] ) {
                    swap(array,j+1,j);
                } else {
                    break;
                }
            }
        }
    }

    private void swap(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}

public class InsertionTest {
    public static void main(String[] args) {
        Insertion insertion = new Insertion();
        int[] array = {23, 51, 24, 82, 7, 31, 21, 36, 63};
        insertion.sort(array);
        System.out.println(Arrays.toString(array));
    }
}

五、复杂度及稳定性分析

1.冒泡排序

  • 时间复杂度:O(N^2)
  • 空间复杂度:O(1)
  • 稳定性:稳定

2.选择排序

  • 时间复杂度:O(N^2)
  • 空间复杂度:O(1)
  • 稳定性:不稳定

3.直接插入排序

  • 时间复杂度:O(N^2)
  • 空间复杂度:O(1)
  • 稳定性:稳定
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值