Learning "Introduction to Algorithms" (9) - Why is the lower bound of the time complexity of comparative sorting algorithms determined?

series of articles

Learning "Introduction to Algorithms" (1) - insertion sort and merge sorting
"Introduction to Algorithms" learning (7) - heap sorting and priority queue (C language)
Learning "Introduction to Algorithms" (8) ---- Quick sort (C language)
Learning "Introduction to Algorithms" (9)----Why is the lower bound of the time complexity of comparative sorting algorithms determined?
"Algorithm Learning" Learning (10)----counting sort, radix sort, bucket sort (C language)



foreword

This article introduces the decision tree, which is mainly a summary overview of the comparative sorting algorithm.
Aiming at the time performance of the algorithm, the overall research is biased towards the essence.


1. Decision tree

1. What is a decision tree?

A decision tree is a complete binary tree, each node has two child nodes, which can represent the comparison operation of a specific sorting algorithm for all elements under a given input size. Among them, other operations such as control and data movement are ignored.

The comparison sorting algorithm can be abstracted as a decision tree

2. Rules of the decision tree

1. In the decision tree, each node is marked with ** i : ji:ji:j ** mark, representingi and j to compare i and j to comparei is compared with j . Among them,iii andjjj satisfies1 ⩽ i , j ⩽ n 1\leqslant i,j\leqslant n1i,jn , n is the number of elements in the input sequence.

2. The path of the decision tree is marked with a comparison method

3. Use ⟨ a 1 , a 2 , a 3 , . . . , an ⟩ \langle{a_1,a_2,a_3,...,a_n}\rangle for the successful comparison sequencea1,a2,a3,...,an way,the sequence is always the leaf node of the decision tree

3. How does comparison sort work on decision trees?

The following picture shows the decision tree case where insertion sort acts on the input sequence of three elements
insert image description here

Second, the lower bound of the comparison sorting algorithm

theorem

In the worst case, any comparison sort algorithm needs to do Ω ( nlgn ) comparisons In the worst case, any comparison sort algorithm needs to do \Omega(nlgn) comparisonsIn the worst case, any comparison sort algorithm needs to do Ω ( n l g n ) comparisons

analyze

In a decision tree, the length of the longest simple path from the root node to any reachable leaf node represents the worst-case number of comparisons in the corresponding sorting algorithm. Therefore, the worst-case number of comparisons for a sorting algorithm is equal to the height of the decision tree.
Then:
the lower bound of the decision tree is the lower bound of the running time of the comparison sorting algorithm The lower bound of the decision tree is the lower bound of the running time of the comparison sorting algorithmThe lower bound of the decision tree, that is, the lower bound of the running time of the comparison sorting algorithm

Specific analysis:
consider a star with a height of hhh , withllA decision tree with l reachable nodes, which corresponds to a comparison and sorting of n element inputs. The input data hasn ! n!n ! possible permutations.
We can get the following formula:
n ! ⩽ l ⩽ 2 hn!\leqslant l\leqslant 2^hn!l2
Taking the logarithm of both sides of this equation, we have: h
⩾ lg ( n ! ) = Ω ( nlgn ) h\geqslant lg(n!)=\Omega(nlgn)hlg ( n ! )=Ω ( n l g n )
The formula here can refer to the article:
"Introduction to Algorithms" Learning (2) ---- Algorithmic Time Scale and Function Growth


For specific comparative sorting algorithms, please refer to:
"Introduction to Algorithms" Learning (1) - Insertion sorting and merge sorting
"Introduction to Algorithms" Learning (7) - Heap sorting and priority queue (C language)
"Introduction to Algorithms" Learning (8)----quick sort (C language)

Guess you like

Origin blog.csdn.net/weixin_52042488/article/details/126813412