[Sorting Algorithms] Comparison of Bubble, Selection, and Insertion Sort Algorithms

Bubble Sort

Ideas:
1. Compare two elements in an orderly manner, and put the larger element at the end of the array.
2. The loop compares to the last element. At this time, the largest number obtained in this round of comparison is at the end of the array.
3. Repeat the above steps until there are no elements to compare.

code:

public int[] BubbleSort(int[] array)
    {
        for (int i = 1; i < array.Length; i++)
        {
            bool _change = false;
            for (int j = 0; j < array.Length - i; j++)
            {
                if (array[j] > array[j + 1])
                {
                    int _num = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = _num;
                    _change = true;
                }
            }
            if (!_change)
            {
                break;
            }
        }
        return array;
    }


Performance analysis:
the time complexity is O(N²), assuming that the data is random, the average number of exchanges is N²/4. If the initial data is in reverse order, then each comparison must exchange positions, and the number of exchanges is N².

selection sort

Ideas:
1. Select the smallest element from all the elements, and put the smallest element in the first place in the array.
2. Find the smallest element from the remaining elements and place it in the second position of the array.
3. Cycle selection until the end of sorting.

code:

public int[] ChoiceSort(int[] array)
    {
        for (int i = 0; i < array.Length - 1; i++)
        {
            int _min = i;
            for (int j = i + 1; j < array.Length; j++)
            {
                if (array[j] < array[_min])//查找最小值的index
                {
                    _min = j;
                }
            }
            if (i != _min)//替换最小值
            {
                int _temp = array[i];
                array[i] = array[_min];
                array[_min] = _temp;
            }
        }
        return array;
    }


Performance analysis:
The time complexity is O(N²), and at most N exchanges are performed.

insertion sort

Ideas:
1. Divide the array into sorted and unsorted parts.
2. Loop the part to be sorted, and insert the retrieved elements into the previously arranged part.

code:

 public int[] InsertSort(int[] array)
    {
        int j;
        for (int i = 1; i < array.Length; i++)
        {
            int _temp = array[i];
            j = i;
            while (j > 0 && _temp < array[j - 1])
            {
                array[j] = array[j - 1];
                j--;
            }
            array[j] = _temp;
        }
        return array;
    }


Performance analysis:
The time complexity is O(N²), and the number of assignments is roughly equal to the number of comparisons, but the time consumption of an assignment is different from that of an exchange, so compared to random data, insertion sort is twice as fast as bubbling, faster than selection Sorting is slightly faster.

Summarize:

- The time complexity of bubbling, selection, and insertion is O(N²). Bubble sorting is generally not selected. Although bubble sorting is the easiest to write, the average performance is not as good as selection sorting and insertion sorting.
- Selection sort reduces the number of exchanges to a minimum, but the number of comparisons is still quite large. Selection sort can be applied when the amount of data is small and exchanging data is more time-consuming than comparing data.
- In most cases, insertion sort is the best choice among the three algorithms, assuming that the amount of data is relatively small or basically ordered.

Guess you like

Origin blog.csdn.net/qq_33461689/article/details/125636683