[Data Structure] Sorting Algorithm Complexity and Stability Analysis [Detailed Graphical Explanation]



Preface

I have previously told you about the principles, ideas, implementation steps, and code sources of the major sorting algorithms. Now let us compare the algorithm complexity and stability analysis of the major sorting algorithms to deepen our understanding of each sorting algorithm. Understanding helps us to more quickly choose the most appropriate sorting algorithm in specific scenarios in the future.



[1] Basic sorting algorithm for small data

(1) Bubble sorting

[Data structure] Bubble sort (code source implementation)

(2) Direct insertion sort

[Data structure] Insertion sort


[2] (Derived from basic sorting for) processing big data processing sorting

(1) Heap sort

[Data structure] Heap sort (C code implementation code source)

(2) Hill sorting

[Data structure] Hill sorting


[3] Big data speed sorting method

(1) Quick sort

[Data Structure] An in-depth and simple understanding of the principles behind quick sort and version optimization [10,000-word detailed explanation] (implemented in C language)

(2) Merge sort

[Data Structure] Recursive and non-recursive implementation of merge sort


[4] Sorting of integer data types with extreme speed

(1) Counting sorting

[Data Structure] Explain counting sorting in simple terms [Detailed explanations with pictures and texts, this article is enough to understand counting sorting]



[5] Other sorting

(1) Radix sort: compare one by one

(2) Bucket sorting

I won’t go into details about these two types here because they are not as good as the previous advanced sorting and are more applicable.



1. Analysis and comparison of various sorting algorithms

Insert image description here

Inner sorting: sorting in memory

External sorting: Sorting on disk [Too much data cannot be stored in the memory, so it is transferred to the disk]

  • A major feature of the disk:
    1. Sequential read Sequential write
    2. It does not support subscript access like memory, so external sorting will be very slow.

Merge sort can be sorted in memory (inner sort) or on disk (outer sort)

2. Merge sort: Detailed explanation of the idea of ​​external sorting algorithm

Insert image description here



☆3. Stability concept explanation

After the same data is sorted, does the relative position change?

The meaning and practical application of stability:

If in the exam, the top three are ranked first, the paper will be submitted firstThe paper that takes less time will be entered into the array first
In the ranking, the one with higher scores will have higher priority. If the scores are the same, the one with the shorter time will have higher priority

or if the total score is the same, the one with higher math score will have higher priority.
Insert image description here
This is often used for structural sorting ( Use the structure pointer to sort by a certain One item to compare )


4. Sorting algorithm complexity and stability analysis

  • Direct insertion sorting is stable and will not move forward when encountering equality.
    • Merge sorting is unstable and becomes stable
      Insert image description here
      Insert image description here
      Most of them are structure pointers. Talking about stability, counting sorting is worthless talking about stability.


Summarize

Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/NiNi_suanfa/article/details/134225649