Académique Documents
Professionnel Documents
Culture Documents
Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two
smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively
sort the sub-arrays.
The steps are:
1. Pick an element, called a pivot, from the array.
2. Partitioning: reorder the array so that all elements with values less than the pivot
come before the pivot, while all elements with values greater than the pivot come after
it (equal values can go either way). After this partitioning, the pivot is in its final
position. This is called the partition operation.
3. Recursively apply the above steps to the sub-array of elements with smaller values and
separately to the sub-array of elements with greater values.
[
int const size = 5;
int i, j, increment, temp;
int array[size]={4,5,2,3,6},i1=0;
//split the array into segments unil we reach beginning of array
for(increment = size/2;increment > 0; increment /= 2)
{
for(i = increment; i<size; i++)
{
temp = array[i];
for(j = i; j >= increment ;j-=increment)
{
//perform the insertion sort for this section
if(temp < array[j-increment])
{
array[j] = array[j-increment];
}
else
{
break;
}
}
array[j] = temp;
}
}
Code
void sort(Item a[], int sequence[], int start, int stop)
{
int step, i;
for (int step = 0; sequence[step] >= 1; step++)
{
int inc = sequence[step];
for (i = start + inc; i <= stop; i++)
{
int j = i;
Item val = a[i];
while ((j >= start + inc) && val < a[j-inc])
{
a[j]=a[j-inc];
j -= inc;
}
a[j] = val;
}
}
}