question archive The quicksort is an example of a divide and conquer algorithm in that it divides the sorting problem into smaller problems by dividing the list of items to be sorted into smaller subsets
Subject:Computer SciencePrice:4.86 Bought12
The quicksort is an example of a divide and conquer algorithm in that it divides the sorting problem into smaller problems by dividing the list of items to be sorted into smaller subsets. The pivot is the mechanism that is used to segment the list. Describe how the quicksort works including a discussion of the pivot, how it is selected, and why the pivot is important to the quicksort.
Quick Sort is a Divide and Conquer algorithm which picks an element as pivot and partitions the given array around the picked pivot. we can pick pivot in various ways, Always picking first element of array as pivot, Always picking last element of array as pivot Picking a random element of array as pivot, Pick median of array as pivot.
partition():
The Main process in quick Sort is partition(). Output of partitions is location i , which means
This take O(n) time every time when we call.
Problem in Quick Sort Algorithm: we know that Quick Sort is a Divide and Conquer algorithm but partition (divide) in Quick sort are not always good in quick sort.
T(n) = T(n/2) + T(n/2 + O(n) // one side n/2 element and another side n/2 elements
T(n) = T(1) + T(n-1) + O(n) // one side 1 element and another side (n-1) elements
Importance of Pivot element in Quick sort:
The time behavior(time complexity of quick sort algorithm is depends on selection of pivot value .
consider various inputs
so selection of pivot in quick sort decide partition (whether equal partition ot unequal partition )which which affect running time of quick sort algorithm.
Quick sort Algorithm:
Quicksort(A,p,q) // A is array p is pointer to start and q is a pointer to end
{
if (p==q)
{ return A;}
else
{
m =partition(A,p,q); // this will call partition algorithm // O(n)
Quicksort(A,p,m-1);
Quicksort(A,m+1,q);
}
return A;
}
---------------------------
Partition(A,p,q) {
x=A[p]; // picking first element of array as pivot
i=p;
for(j=p+1; j q ; j++)
{
if (A[j] x)
{
i=i+1;
interchange(A[i],A[j]);
}
interchange(A[p],A[i]);
returen i ;
}
}