【Data Structures】 7. Simple Sorting—Bubble Sort, Selection Sort, and Insertion Sort

本文深入探讨了三种内部排序算法——冒泡排序、选择排序和插入排序的工作原理及性能表现。通过详细的步骤说明和示例跟踪,读者可以清晰地理解每种算法如何操作数据,并对比它们的时间复杂度。

Bubble sort

1. Compare two values at a time.

2. If the one on the left is bigger, swap them to BUBBLE UP the bigger value to the right.

3. Move one position to the right.


public static void bubbleSort(int[] data) {
    for (int out = data.length - 1; out >= 1; out--) {
        for (int in = 0; in < out; in++) {
            if (data[in] > data[in + 1]) {
                swap(data, in, in + 1);
            }
        }
    }
}
// a helper method that swaps two values in an int array
private static void swap(int[] data, int one, int two) {
    int temp = data[one];
    data[one] = data[two];
    data[two] = temp;
}


Result trace: int data[] = { 4, 7, 2, 5, 3 };

First round (out: 4)

4, 7, 2, 5, 3 (in: 0, in+1: 1, swap? N)

4, 2, 7, 5, 3 (in: 1, in+1: 2, swap? Y)

4, 2, 5, 7, 3 (in: 2, in+1: 3, swap? Y)

4, 2, 5, 3, 7 (in: 3, in+1: 4, swap? Y)


Second round (out: 3)

4, 2, 5, 3, 7 (in: 0, in+1: 1, swap? Y)

2, 4, 5, 3, 7 (in: 1, in+1: 2, swap? N)

2, 4, 3, 5, 7 (in: 2, in+1: 3, swap? Y)


Third round (out: 2)

2, 4, 3, 5, 7 (in: 0, in+1: 1, swap? N)

2, 3, 4, 5, 7 (in: 1, in+1: 2, swap? Y)


Fourth round (out: 1)

2, 3, 4, 5, 7 (in: 0, in+1: 1, swap? N)


Time complexity

Comparisons: (n - 1) + (n - 2) + ... + 1 = n * (n - 1) / 2 = n ^ 2 / 2

Swaps: n ^ 2 / 4

Because constants don't count in Big O notation, we can conclude that bubble sort runs in O(n^2) time.


Selection Sort: Faster than Bubble Sort but still not enough

1. Pick or SELECT the minimum value.

2. Swap it with the element on the left end.


int min;  // min variable
for (int out = 0; out < data.length - 1; out++) {
    min = out; // set initial min values' s index

    // select a new minimum value' s index
    for (int in = out + 1; in < data.length; in++) {
        if (data[in] < data[min]) {
            min = in;  // reset the new min index
        }
    }
    swap(data, out, min);  // time to put min value
}


Result trace:

First round (out: 0, min: 0)

4, 7, 2, 5, 3 (in: 1, min: 0)

4, 7, 2, 5, 3 (in: 2, min: 2)

4, 7, 2, 5, 3 (in: 3, min: 2)

4, 7, 2, 5, 3 (in: 4, min: 2)

swap (0, 2)

2, 7, 4, 5, 3


Second round (out: 1, min: 1)

2, 7, 4, 5, 3 (in: 2, min: 2)

2, 7, 4, 5, 3 (in: 3, min: 2)

2, 7, 4, 5, 3 (in: 4, min: 4)

swap (1, 4)

2, 3, 4, 5, 7


Third round (out: 2, min: 2)

2, 3, 4, 5, 7 (in: 3, min: 2)

2, 3, 4, 5, 7 (in: 4, min: 2)

no swap

2, 3, 4, 5, 7


Fourth round (out: 3, min: 3)

2, 3, 4, 5, 7 (in: 4, min: 3)

no swap

2, 3, 4, 5, 7


Insertion Sort

Most important thing in the insertion sort is that there is an imaginary dividing line.

Left hand side of the line is sorted among themselves.

The first element of the right hand side of the line should be inserted into the left hand side in a proper position

1. First, we keep the value of the first element into a temp place.

2. Shift the items of the left hand side to the right so that there can be a space for the value that is stored in the temp place.

3. When the position is found, INSERT the value into that position.


public static void insertionSort(int[] data) {
    // set and increase the dividing line
    for (int out = 1; out < data.length; out++) {
        int temp = data[out];
        int in = out;

        // go backward in the left side of the imaginary line to find a place to insert temp value
        while (in > 0 && data[in - 1] >= tmp) {
            data[in] = data[in - 1];
            in--;
        }

        data[in] = temp;  // INSERT the temp value
    }
}

Result trace:

First round (out: 1, temp: 7)

4,  , 2, 5, 3 (in: 1, in-1: 0, shift?: N)

insert

4, 7, 2, 5, 3


Second round (out: 2, temp: 2)

4, 7,  , 5, 3 (in: 2, in-1: 1, shift? Y)

4,  , 7, 5, 3 (in: 1, in-1: 0, shift? Y)

 , 4, 7, 5, 3

insert

2, 4, 7, 5, 3


Third round (out: 3, temp: 5)

2, 4, 7,  , 3 (in: 3, in-1: 2, shift? Y)

2, 4,  , 7, 3 (in: 2, in-1: 1, shift? N)

insert

2, 4, 5, 7, 3


Fourth round (out: 4, temp: 3)

2, 4, 5, 7,   (in: 4, in-1: 3, shift? Y)

2, 4, 5,  , 7 (in: 3, in-1: 2, shift? Y)

2, 4,  , 5, 7 (in: 2, in-1: 1, shift? Y)

2,  , 4, 5, 7 (in: 1, in-1: 0, shift? N)

insert

2, 3, 4, 5, 7


Time Complexity

These three internal sorting algorithms, bubble sort, selection sort and insertion sort, all run in O(n^2) time in the worst case.

However, often, insertion sort performs better than the other two because it may requires less number of comparisons depending on the input values and uses copying instead of swapping. Copy or shift is more efficient than swap.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值