Quicksort-How did we get the relation?

  • MHB
  • Thread starter mathmari
  • Start date
  • Tags
    Relation
In summary, the equation for the expected time of Quicksort is calculated by taking into account all possible partitions of the array and the probability of each partition occurring. This results in a sum of the expected times for each partition, with the probability weighting each term.
  • #1
mathmari
Gold Member
MHB
5,049
7
Hey! :eek:

At the proof of the theorem that the expected time of Quicksort is $O(n \log n)$, there is the following sentence:

We suppose that the partitions are equally likely, so the possibility that the sizes of the sequences $S_1$ and $S_3$ are $i-1$ and $n-i$, respectively, is $\frac{1}{n}$.

Let $T(n)$ be the expected time of Quicksort.

Taking into consideration all the possible values of $i$, we get the relation: $$T(n)=\sum_{i=1}^{n}\frac{1}{n}[T(i-1)+T(n-i)]+\Theta(n)$$Could you explain to me how we get the above relation?? (Wondering) How did we get the sum?? (Wondering)
 
Technology news on Phys.org
  • #2
The relation above comes from the fact that for any partition of the array into two subarrays, the expected time of Quicksort is the sum of the expected times of the two subarrays plus the cost of the partition itself.The sum over $i$ in the equation is taking into account all the possible partitions of the array into two subarrays. For each possible value of $i$, we have a different partition of the array and thus a different expected time. The $\frac{1}{n}$ is the probability of each partition occurring. Therefore, the equation is summing up all the expected times for each possible partition, with the probability weighting each term.
 

FAQ: Quicksort-How did we get the relation?

What is Quicksort?

Quicksort is a sorting algorithm that works by selecting a pivot element, partitioning the array around the pivot, and recursively sorting the subarrays formed by the partition.

How does Quicksort work?

Quicksort works by selecting a pivot element from the array, partitioning the array into two subarrays (one with elements smaller than the pivot and one with elements larger than the pivot), and recursively sorting the subarrays until the entire array is sorted.

What is the time complexity of Quicksort?

The average time complexity of Quicksort is O(n*log n), while the worst-case time complexity is O(n^2). This is because the pivot selection can greatly influence the efficiency of the algorithm.

Why is Quicksort a popular sorting algorithm?

Quicksort is popular because it is efficient for large datasets and can be implemented with minimal additional memory. It also has a relatively simple implementation compared to other sorting algorithms.

How did we get the relation between Quicksort and pivot selection?

The relation between Quicksort and pivot selection was determined through mathematical analysis and experimentation. Various pivot selection methods were tested and their effect on the time complexity of Quicksort was observed.

Similar threads

Replies
1
Views
1K
Replies
2
Views
1K
Replies
1
Views
1K
Replies
2
Views
1K
Replies
11
Views
3K
Replies
5
Views
3K
Replies
10
Views
1K
Replies
17
Views
1K
Back
Top