This is the algorithm of Quicksort

  • MHB
  • Thread starter evinda
  • Start date
  • Tags
    Algorithm
In summary, the conversation discusses the algorithm of Quicksort and its implementation in the Partition function. The swap command is needed in Quicksort because it swaps the values in the array A, which is necessary for the algorithm to work correctly. When q=p+1, the cost of Partition is O(n) and we need to call Quicksort twice. The total cost in this case can be found by adding the cost of Partition to the cost of calling Quicksort twice. The cost in the worst case is given by the formula T(n)=\max_{0 \leq q \leq n-1} (T(q)+T(n-q-1))+\Theta(n).
  • #1
evinda
Gold Member
MHB
3,836
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
  • #2
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:

FAQ: This is the algorithm of Quicksort

What is Quicksort and how does it work?

Quicksort is a sorting algorithm that works by partitioning an array into two sub-arrays, one with elements smaller than a pivot element and one with elements larger than the pivot. It then recursively applies this process to each sub-array until the entire array is sorted.

How efficient is Quicksort compared to other sorting algorithms?

Quicksort has an average time complexity of O(n*log n), making it one of the most efficient sorting algorithms. However, its worst-case time complexity is O(n^2), which can occur if the pivot is always chosen to be the smallest or largest element in the array.

Can Quicksort handle arrays with duplicate elements?

Yes, Quicksort can handle arrays with duplicate elements. However, the algorithm may become less efficient if there are a large number of duplicate elements, as it will result in more recursive calls and potentially increase the time complexity.

What is the role of the pivot element in Quicksort?

The pivot element is crucial in Quicksort as it determines the partitioning of the array. It should be chosen in a way that divides the array into two roughly equal sub-arrays to achieve the best time complexity.

Are there any variations of Quicksort?

Yes, there are several variations of Quicksort, such as randomized Quicksort, three-way Quicksort, and dual-pivot Quicksort. These variations aim to improve the algorithm's efficiency by addressing the issue of worst-case time complexity or optimizing the choice of the pivot element.

Similar threads

Replies
1
Views
1K
Replies
1
Views
1K
Replies
2
Views
1K
Replies
3
Views
2K
Replies
6
Views
2K
Replies
1
Views
1K
Back
Top