answersLogoWhite

0

#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

11y ago

What else can I help you with?

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


How does DNA polymerase function in the process of transcription?

DNA polymerase does not function in the process of transcription. Transcription is the process where RNA is synthesized from a DNA template by RNA polymerase. DNA polymerase, on the other hand, is involved in DNA replication, where it synthesizes a new DNA strand using a DNA 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.


Is using a template considered plagiarism?

Using a template is not considered plagiarism as long as the content is properly cited and the template is used as a guide or framework for creating original work.