Professional Documents
Culture Documents
Experiment2 AA
Experiment2 AA
Quick Sort
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics & Computer Science
Experiment No. 2
Theory:
Quick sort is a highly efficient sorting algorithm and is based on partitioning an array of data
into smaller arrays. A large array is partitioned into two arrays one of which holds values
smaller than the specified value, say pivot, based on which the partition is made and another
array holds values greater than the pivot value. Quick sort partitions an array and then calls
itself recursively twice to sort the two resulting sub arrays. This algorithm is quite efficient
for large-sized data sets as its average and worst case complexity are of Ο(n 2), where n is the
number of items.
The pivot value divides the list into two parts. And recursively, we find the pivot for each
sub- list until all lists contains only one element.
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics & Computer Science
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics & Computer Science
Algorithm:
Based on our understanding of partitioning in quick sort, we will now try to write an
algorithm for it, which is as follows.
Step 1 Choose the highest index value has pivot
Step 2
Take two variables to point left and right of the list excluding pivot
for(i=0; i<size;i++)
{
printf("\n Enter [%d] element :: \n", i+1);
scanf("%d",&list[i]);
}
quicksort(list,0,size-1);
printf("\n");
return 0;
}
void quicksort(int list[],int low,int high)
{
int pivot,i,j,temp;
if(low<high)
{
pivot=low;
i=low;
j=high;
while(i<j)
{
while(list[i]<=list[pivot] && i<=high)
{
i++;
}
while(list[j]>=list[pivot] && j>=low)
{
j--;
}
if(i<j)
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
temp=list[j];
list[j]=list[pivot];
list[pivot]=temp;
quicksort(list,low,j-1);
quicksort(list,j+1,high);
}
}
Conclusion:
In this experiment we have implemented Quick sort algorithm using Divide and Conquer
approach.