answersLogoWhite

0

//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;

}

User Avatar

Wiki User

16y ago

What else can I help you with?

Related Questions

CPP Program for Implementing Knuth-morris-pratt pattern matching algorithm?

what is the pure algorithm instead of cpp program?


A program without an extension name CPP will run?

Yes because a program can run without a CPP extension, especially if program extension is .exe


How to print the code of a program in the output terminal when the program is being executed in cpp?

Such a program is called a Quine. http://en.wikipedia.org/wiki/Quine_(computing)


Explain a program for this pointer in cpp?

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


Divide-and-Conquer to sort numbers using quick sort?

Yes, that's how quick-sort works.


What c plus plus program must be saved in a text with the extension cpp?

The .cpp extension is merely conventional; it is not required by the C++ standard. You can actually use any file extension you wish.


Why the extension cpp of c?

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.


18 A list is ordered from smaller to largest when a sort is called Which sort would take the longest time to execute?

Quick Sort


Write a program in cpp to print the charminar?

int main (void) { puts ("charminar"); return 0; }


Is Quick Sort an in-place sorting algorithm?

Yes, Quick Sort is an in-place sorting algorithm.


When quick sort is preferred?

When you want to sort an array.


Which sorting algorithm is more efficient for large datasets: quick sort or selection sort?

Quick sort is more efficient for large datasets compared to selection sort.