Quick Sort Algorithm
*QuickSort Algorithm:
*QuickSort is a Divide and Conquer algorithm;
Now for making this clear let's take an Array -
A[]={10,16,8,12,15,6,3,9,5,∞}
Step-1: First we will make l (start position) as pivot and ∞ as h position.
Step-2: we need to use two certain loop and i(keep incrementing) is always search for value which is greater than pivot and j(keep decrementing) is search for value which is smaller then pivot. i start from l and j start from h.If we get our desire value then keep swaping.
Step-3: if (i>j) or i cross j then sawp pivot with A[j].Then we get two partition of Array and pivot get it's sorted position .Then Step 1,2,3 continuing in every partition and at last we get our desired sorted Array.
procedure:
*10(pivot)
* swaping(16,5)
*swaping(12,9)
*swaping(3,15)
*Then (i,j) crossed and i will be greater then j so we swap j with pivot and we'll get two partition of Array and then we use the same process into the two partition and when (l=h) then it's finished.
Algorithm:
QuickSort(l,h)
{
if(l<h)
{
j=partition(l,h);
QuickSort(l,j);
QuickSort(j+1,h);
}
}
PartioN(l,h)
{
pivot=A[l];
i=l,j=h;
while(i<j)
{
do
{
i++;
}while(A[i]<=pivot);
do
{
j--;
}while(A[j]>pivot);
if(i<j)
swap(A[i],A[j]);
}
swap(A[l],A[j]);
return j;
}
*QuickSort is a Divide and Conquer algorithm;
Now for making this clear let's take an Array -
A[]={10,16,8,12,15,6,3,9,5,∞}
Step-1: First we will make l (start position) as pivot and ∞ as h position.
Step-2: we need to use two certain loop and i(keep incrementing) is always search for value which is greater than pivot and j(keep decrementing) is search for value which is smaller then pivot. i start from l and j start from h.If we get our desire value then keep swaping.
Step-3: if (i>j) or i cross j then sawp pivot with A[j].Then we get two partition of Array and pivot get it's sorted position .Then Step 1,2,3 continuing in every partition and at last we get our desired sorted Array.
procedure:
*10(pivot)
* swaping(16,5)
*swaping(12,9)
*swaping(3,15)
*Then (i,j) crossed and i will be greater then j so we swap j with pivot and we'll get two partition of Array and then we use the same process into the two partition and when (l=h) then it's finished.
Algorithm:
QuickSort(l,h)
{
if(l<h)
{
j=partition(l,h);
QuickSort(l,j);
QuickSort(j+1,h);
}
}
PartioN(l,h)
{
pivot=A[l];
i=l,j=h;
while(i<j)
{
do
{
i++;
}while(A[i]<=pivot);
do
{
j--;
}while(A[j]>pivot);
if(i<j)
swap(A[i],A[j]);
}
swap(A[l],A[j]);
return j;
}
Comments
Post a Comment