answersLogoWhite

0


Best Answer

#include<iostream>

#include<time.h> // used by srand()

// Swap the given values (by reference).

template<class T>

void exch( T& x, T& y )

{

T tmp=x;

x=y;

y=tmp;

}

// Generates a pseudo-random number in the range min to max (inclusive).

int get_rand( const int range_min=0, const int range_max=RAND_MAX )

{

static bool seeded = false;

if( !seeded )

{

seeded = !seeded;

std::srand((unsigned) time(NULL));

while( get_rand() );

}

return((int)((double)std::rand() / (RAND_MAX+1) * ((range_max+1) - range_min) + range_min));

}

template<class T>

void quicksort( T a[], int l, int r )

{

int i;

static int j;

tail_call:

if(r<=l)

return;

// Select random pivot and move it to end of array.

exch( a[get_rand(l,r)], a[r] );

i = l-1;

j = r;

while(1)

{

// Locate the 1st value from left that is not less than the pivot.

while(a[++i]<a[r]);

// Locate the 1st value from right

while(a[r]<a[--j])

if(j==l)

break;

if(i>=j)

break;

exch(a[i], a[j]);

}

exch( a[i], a[r] );

// Determine the smaller subset

static int l1, l2;

l1=l, l2=i-1;

int r1=i+1, r2=r;

if(r2-r1<l2-l1)

{

l1=r1, l2=r2;

r1=l, r2=i-1;

}

// Recurse over smaller subset

quicksort( a, l1, l2);

// Tail-call over largest subset

l=r1, r=r2;

goto tail_call;

}

// Helper method: sorts an array up to the given size.

template<class T>

void quicksort( T a[], const size_t size )

{

if( size < 2 )

return;

quicksort( a, 0, size-1 );

}

template<class T>

void print_array(T a[], const size_t size)

{

for(int i=0; i<size; ++i)

std::cout<<my_array[i]<<" ";

std::cout<<'\n'<<std::endl;

}

int main()

{

size_t size=1000000;

int my_array[size];

for( int i=0; i<size; ++i )

my_array[i]=get_rand( 1, size );

print_array( my_array, size );

std::cout<<"Sorting...\n"<<std::endl;

quicksort(a,size);

print_array( my_array, size );

}

User Avatar

Wiki User

โˆ™ 10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the C plus plus program for quick sort using template function?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Program to find biggest number among two numbers using template function?

template&lt;class T&gt; T&amp; max(const T&amp; x, const T&amp;y){return(x&gt;y?x:y); }


What is the purpose of template functions?

Template functions provide the ability to write a single function that can accept different types of inputs and produce different types of output, while still using the same logic. For example, a template function can accept an int, long, or float and perform the same logic on any of the three types of parameters. The compiler ensures that the template function will make sense when compiled with a particular type. This was an early attempt at polymorphism (using the same code on different types of objects), and has since been superseded in newer languages using objects and inheritance.


Using only one program implement bubble sorting insertion sorting quick sorting and selection sorting?

Its simple!dirve a menu based prog by using switch case &amp; then apply every sorting function to it.


Write a program that read phrase and print the number of lower-case letter in it using function of counting?

write a program that reads a phrase and prints the number of lowercase latters in it using a function for counting? in C program


Distinguish between class template and template class?

Template class: A generic definition or a parametrized class not instantiated until the client provides the needed information. It?s jargon for plain templates. Class template: A class template specifies how individual classes can be constructed much like the way a class specifies how individual objects can be constructed. It?s jargon for plain classes. Class template is a template used to generate template classes. We cannot declare an object of a class template. Template class is an instance of a class template.

Related questions

Program to find biggest number among two numbers using template function?

template&lt;class T&gt; T&amp; max(const T&amp; x, const T&amp;y){return(x&gt;y?x:y); }


How do you open the badge template on an image editor in Roblox?

Save the template to your computer, and then open it using any image editing program on your computer. You can use Microsoft paint or another image editing program you bought, or you can download a free program off the Internet. I would recommend using Gimp or Paint.net.


What is the use of function in c?

using function we can call the function in the program any where. by using functions we can reduce the code redundancy


What is the purpose of template functions?

Template functions provide the ability to write a single function that can accept different types of inputs and produce different types of output, while still using the same logic. For example, a template function can accept an int, long, or float and perform the same logic on any of the three types of parameters. The compiler ensures that the template function will make sense when compiled with a particular type. This was an early attempt at polymorphism (using the same code on different types of objects), and has since been superseded in newer languages using objects and inheritance.


Advantage of using template in an organization?

advantage of using template in an orgainsation


Using only one program implement bubble sorting insertion sorting quick sorting and selection sorting?

Its simple!dirve a menu based prog by using switch case &amp; then apply every sorting function to it.


Write a program that read phrase and print the number of lower-case letter in it using function of counting?

write a program that reads a phrase and prints the number of lowercase latters in it using a function for counting? in C program


Write a c program for matrix addition using function?

#include&lt;


How can you create a spreadsheet using sales invoice template?

how you can create a spreadsheet using sales invoice template


Distinguish between class template and template class?

Template class: A generic definition or a parametrized class not instantiated until the client provides the needed information. It?s jargon for plain templates. Class template: A class template specifies how individual classes can be constructed much like the way a class specifies how individual objects can be constructed. It?s jargon for plain classes. Class template is a template used to generate template classes. We cannot declare an object of a class template. Template class is an instance of a class template.


How would you make lava invisible in a minecraft texture pack?

First open the Minecraft texture template with a program that supports transparency. Either select the lava texture(found in the lower left corner of the template), and delete it; or paint over the texture with transparent color (The method you use depends on what program you are using). Finally, save the finished template as a file format that supports transparency, such as GIF or PNG.


What is the importance of using functions in a c program?

C programs do not function without functions.