MHB This is the algorithm of Quicksort

  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Algorithm
Click For Summary
The discussion centers on the Quicksort algorithm, detailing its structure and the necessity of the swap operation after partitioning. The swap command, swap(A[q],A[r]), is essential for placing the pivot in its correct position after partitioning. When q equals p+1, the cost of Partition is O(n), leading to recursive calls that require further analysis to determine overall cost. The worst-case cost formula T(n)=max_{0 ≤ q ≤ n-1} (T(q)+T(n-q-1)) + Θ(n) reflects the maximum time complexity when the pivot selection results in unbalanced partitions. Understanding these elements is crucial for analyzing Quicksort's efficiency and performance in different scenarios.
evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hi! (Nerd)

This is the algorithm of [m] Quicksort [/m], according to my notes:

Code:
Quicksort(A,p,r)
   if (p>=r) return;
   q=Partition(A,p,r);
   swap(A[q],A[r]);
   Quicksort(A,p,q-1);
   Quicksort(A,q+1,r)

Code:
Partition(A,p,r)
  x=A[r];
  i=p-1;
  for (j=p; j<r; j++){
       if (A[j]<=x){
          i=i+1;
          swap(A[i],A[j]);
       }
  }
  swap(A[i+1],A[r]);
  return i+1;

Why is this command: [m] swap(A[q],A[r]); [/m] needed at the algorithm of [m]Quicksort[/m]?
Don't we swap these values in [m] Partition [/m] ? (Thinking)
 
Technology news on Phys.org
Also, which is the cost when $q=p+1$ and which when $q=\lfloor \frac{p+r}{2} \rfloor$ ?

When $q=p+1$, we will have the cost of [m]Partition[/m] that is $O(n)$ and we will have to call the functions [m]Quicksort(A,p,p)[/m] and [m]Quicksort(A,p+,r)[/m], right? How can we find then the cost? (Thinking)Could you also explain me why this: $T(n)=\max_{0 \leq q \leq n-1} (T(q)+T(n-q-1))+\Theta(n)$ is the cost of the worst case? (Worried)
 
Last edited:

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
Replies
9
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K