MHB This is the algorithm of Quicksort

  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Algorithm
AI Thread 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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top