//Quicksort the array
void quicksort(int* array, int left, int right)
{
if(left >= right)
return;
int index = partition(array, left, right);
quicksort(array, left, index - 1);
quicksort(array, index + 1, right);
}
//Partition the array into two halves and return the
//index about which the array is partitioned
int partition(int* array, int left, int right)
{
//Makes the leftmost element a good pivot,
//specifically the median of medians
findMedianOfMedians(array, left, right);
int pivotIndex = left, pivotValue = array[pivotIndex], index = left, i;
swap(array, pivotIndex, right);
for(i = left; i < right; i++)
{
if(array[i] < pivotValue)
{
swap(array, i, index);
index += 1;
}
}
swap(array, right, index);
return index;
}
//Computes the median of each group of 5 elements and stores
//it as the first element of the group. Recursively does this
//till there is only one group and hence only one Median
int findMedianOfMedians(int* array, int left, int right)
{
if(left == right)
return array[left];
int i, shift = 1;
while(shift <= (right - left))
{
for(i = left; i <= right; i+=shift*5)
{
int endIndex = (i + shift*5 - 1 < right) ? i + shift*5 - 1 : right;
int medianIndex = findMedianIndex(array, i, endIndex, shift);
swap(array, i, medianIndex);
}
shift *= 5;
}
return array[left];
}
//Find the index of the Median of the elements
//of array that occur at every "shift" positions.
int findMedianIndex(int* array, int left, int right, int shift)
{
int i, groups = (right - left)/shift + 1, k = left + groups/2*shift;
for(i = left; i <= k; i+= shift)
{
int minIndex = i, minValue = array[minIndex], j;
for(j = i; j <= right; j+=shift)
if(array[j] < minValue)
{
minIndex = j;
minValue = array[minIndex];
}
swap(array, i, minIndex);
}
return k;
}
//Swap integer values by array indexes
void swap(int * array, int a, int b)
{
int tmp = array[a];
array[a] = array[b];
array[b] = tmp;
}
what is the pure algorithm instead of cpp program?
Such a program is called a Quine. http://en.wikipedia.org/wiki/Quine_(computing)
Yes, that's how quick-sort works.
The .cpp extension is merely conventional; it is not required by the C++ standard. You can actually use any file extension you wish.
Quick Sort
what is the pure algorithm instead of cpp program?
Yes because a program can run without a CPP extension, especially if program extension is .exe
Such a program is called a Quine. http://en.wikipedia.org/wiki/Quine_(computing)
Go to the link. You will got use of "this" keywork with simple explanation and example. http://cpp.codenewbie.com/articles/cpp/1527/Keyword_this-Page_5.html
Yes, that's how quick-sort works.
The .cpp extension is merely conventional; it is not required by the C++ standard. You can actually use any file extension you wish.
The extension of a file containing a C program can be any extension, so long as the compiler or platform can infer the proper rules to build it. Commonly, for C programs, the extension is .c, so myfile.c would be a C program. The term cpp is not a designation for C++. It means C Program Precompiler, and it is the normal way to build one or more C programs into an executable. Over the years, cpp has evolved into being able to handle all sorts of languages. C++ is one of them. Typical extensions for C++ programs are .cc, .cpp, and .cxx.
Quick Sort
int main (void) { puts ("charminar"); return 0; }
Yes, Quick Sort is an in-place sorting algorithm.
When you want to sort an array.
Quick sort is more efficient for large datasets compared to selection sort.