经典排序算法:冒泡排序、选择排序、插入排序

21 篇文章 4 订阅

冒泡排序

它的基本思路是:每一次遍历数组中两两相邻的元素,把最大的那个元素“沉底”,下一次遍历的时候,只遍历第一个元素到倒数第二个元素,依次类推。经过多次轮询,最终使得数组有序。

待排序的数据:{8,5,2,7,4,9,1,6,3}

每一次轮询的结果:

[5, 2, 7, 4, 8, 1, 6, 3, 9]
[2, 5, 4, 7, 1, 6, 3, 8, 9]
[2, 4, 5, 1, 6, 3, 7, 8, 9]
[2, 4, 1, 5, 3, 6, 7, 8, 9]
[2, 1, 4, 3, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Java实现:

//冒泡排序
public void bubble_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]){
                array[j] = array[j] ^ array[j + 1];
                array[j + 1] = array[j] ^ array[j + 1];
                array[j] = array[j] ^ array[j + 1];
            }
        }
    }
}
public static void main(String[] args) {
    int[] arr = new int[]{8,5,2,7,4,9,1,6,3};
    Array_Sort as = new Array_Sort();
    as.bubble_sort(arr);
    System.out.println(Arrays.toString(arr));
}

python实现:

# 冒泡排序
def bubble_sort(array):
    for i in range(len(array) - 1):
        for j in range(len(array) - i - 1):
            if array[j]>array[j + 1]:
                array[j] = array[j] ^ array[j + 1]
                array[j + 1] = array[j] ^ array[j + 1]
                array[j] = array[j] ^ array[j + 1]
list1 = [8,5,2,7,4,9,1,6,3]
bubble_sort(list1)
print(list1)#[1, 2, 3, 4, 5, 6, 7, 8, 9]

选择排序

它的基本思路是:每一次轮询,选择出数组中的最小值,将最小值按照索引顺序放置在相应的位置上。

待排序的数据:{8,5,2,7,4,9,1,6,3}

每一次轮询的结果:

[1, 5, 2, 7, 4, 9, 8, 6, 3]
[1, 2, 5, 7, 4, 9, 8, 6, 3]
[1, 2, 3, 7, 4, 9, 8, 6, 5]
[1, 2, 3, 4, 7, 9, 8, 6, 5]
[1, 2, 3, 4, 5, 9, 8, 6, 7]
[1, 2, 3, 4, 5, 6, 8, 9, 7]
[1, 2, 3, 4, 5, 6, 7, 9, 8]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Java实现:

//选择排序
public void select_sort(int[] array){
    int min_index,should_index;
    for (int i = 0;i<array.length-1;i++){
        min_index = should_index = i;
        for (int j = i+1;j<array.length;j++){
            if(array[j] < array[min_index])
                min_index = j;
        }
        if(should_index != min_index) {
            array[should_index] = array[should_index] ^ array[min_index];
            array[min_index] = array[should_index] ^ array[min_index];
            array[should_index] = array[should_index] ^ array[min_index];
        }
    }
}
public static void main(String[] args) {
    int[] arr = new int[]{8,5,2,7,4,9,1,6,3};
    Array_Sort as = new Array_Sort();
    as.select_sort(arr);
    System.out.println(Arrays.toString(arr));
}

python实现:

# 选择排序
def select_sort(array):
    for i in range(len(array)-1):
        min_index = i
        should_index = i
        for j in range(i+1, len(array)):
            if array[j] < array[min_index]:
                min_index = j
        if should_index != min_index:
            array[should_index] = array[should_index] ^ array[min_index]
            array[min_index] = array[should_index] ^ array[min_index]
            array[should_index] = array[should_index] ^ array[min_index]

list1 = [8,5,2,7,4,9,1,6,3]
select_sort(list1)
print(list1)#[1, 2, 3, 4, 5, 6, 7, 8, 9]

插入排序

它的基本思路是:先假定第一个元素是有序的,其他元素全部是无序的,首先遍历无序元素中的第一个,把它“插入”到有序部分的相应位置,使得有序部分的元素越来越多,最终全部元素有序。

待排序的数据:{8,5,2,7,4,9,1,6,3}

每一次轮询的结果:

[5, 8, 2, 7, 4, 9, 1, 6, 3]
[2, 5, 8, 7, 4, 9, 1, 6, 3]
[2, 5, 7, 8, 4, 9, 1, 6, 3]
[2, 4, 5, 7, 8, 9, 1, 6, 3]
[2, 4, 5, 7, 8, 9, 1, 6, 3]
[1, 2, 4, 5, 7, 8, 9, 6, 3]
[1, 2, 4, 5, 6, 7, 8, 9, 3]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Java实现:

//插入排序
public void insert_sort(int[] array){
    int currentValue;
    int insertPosition;
    for (int i = 1;i<array.length;i++){
        currentValue = array[i];
        insertPosition = i;
        for (int j = i-1;j>=0;j--){
            if (currentValue<array[j]){
                array[j+1] = array[j];//往后移动一个位置,相当于覆盖
                insertPosition = j;
            }else {
                break;//提高了效率
            }
        }
        if (insertPosition != i){
            array[insertPosition] = currentValue;
        }
    }
}
public static void main(String[] args) {
    int[] arr = new int[]{8,5,2,7,4,9,1,6,3};
    Array_Sort as = new Array_Sort();
    as.insert_sort(arr);
    System.out.println(Arrays.toString(arr));
}

python实现:

# 插入排序
def insert_sort(array):
    for i in range(1,len(array)):
        currentValue = array[i]
        insertPositon = i
        j = i-1
        while j>=0:
            if currentValue<array[j]:
                array[j+1] = array[j]
                insertPositon = j
                j-=1
            else:
                break
        if insertPositon != i:
            array[insertPositon] = currentValue
list1 = [8,5,2,7,4,9,1,6,3]
insert_sort(list1)
print(list1)#[1, 2, 3, 4, 5, 6, 7, 8, 9]
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值