0% found this document useful (0 votes)
12 views

Quick Sort: Presented By: Shivani (B-Tech Cse)

Uploaded by

shivaniii5757
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Quick Sort: Presented By: Shivani (B-Tech Cse)

Uploaded by

shivaniii5757
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

QUICK SORT

PRESENTED BY : SHIVANI (B-TECH


CSE)
DEFINITION :
THE QUICK SORT ALGORITHM IS A SORTING ALGORITHM THAT
WORKS BY SELECTING A PIVOT POINT , AND THERE AFTER
PARTITIONING THE NUMBER SET , OR ARRAY , AROUND THE
PIVOT POINT . ALSO KNOWN AS PARTITION EXCHANGE SORT , A
QUICK SORT WAS DEVELOPED BY TONNY HOARE , A BRITISH
COMPUTER SCIENTIST , IN 1959.
WORKING :
IN THE GIVEN ARRAY , WE CONSIDER THE LEFTMOST ELEMENT
AS PIVOT. SO , IN THIS CASE , A[LEFT] = 24, A[RIGHT] = 27 AND
A[PIVOT] = 24.
24 9 29 14 19 27
PARTITION :
Partition (A, lb, ub)
{
Pivot = a[lb]
Start = lb;
End = ub;
While(a[start] <= pivot)
{
start ++;
}
While(a[end] >= pivot)
{
End --;
}
If (start < end)
{ swap(a[start], a[end]];
}
}
ALGORITHM :
 STEP1: choose a random value as pivot element.

 STEP2: if the pivot element is at right , compare it with the element present at left and if the pivot element is present at
left , then compare it with right element.
 STEP3: while value at right is greater than pivot move left.

 STEP4: while value at left is greater than pivot move right.

 STEP5: if both step 3 and step 4 does not match swap left and right.

 STEP6: if left>right , the point where they met is new pivot.


IMPLEMENTATION :
#include<stdio.h>
int partition (int a[], int start, int end)
{
Int pivot = a[end];
Int I = (start – 1);

For (int j = start; j <= end – 1; j++)


{
If(a[j] < pivot)
{
i++;
Int t = a[i];
a[i] = a[j];
a[j] = t;
}
IMPLEMENTATION :
Int t = a[i + 1];
a[i +1] = a[end];
a[end] = t;
return (i + 1);
}
void quick(int a[], int start, int end)
{
if (start < end)
{
Int p = partition(a, start, end);
quick(a, start, p – 1);
quick(a, p+1, end);
}
}
IMPLEMENTATION :
void printArr(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
printf(“%d”, a[i]);
}
int main()
{
int a[] = { 24,9,29,14,19,27};
int n = sizeof(a) / sizeof(a[0]);
printf(“before sorting array elements are - \n”);
printArr(a, n);
quick(a, 0, n – 1);
printf(“\nAfter sorting array elements are - \n”);
printArr(a, n);
IMPLEMENTATION :
return 0;
}

Output :
Before sorting array elements are –
24 9 29 14 19 27
After sorting array elements are –
9 14 19 24 27 29
COMPLEXITY :
case Time complexity
Best case O(n*logn)
Average case 0(n*logn)
Worst case O(n^2)

You might also like