Sherwood algorithm (turn to explain the introduction of the algorithm topic!!!) )

Source: Internet
Author: User
Tags random shuffle shuffle

Randomization algorithm (3)-Sherwood (Sherwood) algorithm

Already out of serial:

1. randomization algorithm (1)-Random number

2. randomization algorithm (2)-Numerical probability algorithm

Body:

How to say this chapter, I personally feel bad understanding, on-line check some information, did not find specific to Sherwood algorithm introduction.

By far the most comprehensive is Wang Xiaodong's "computer Algorithm design and analysis". Some of the information on the Internet is basically the same as the book, as the book first written, or other places first written, I will not comment.

(I have time to photograph the book about Sherwood and put it in the article)

The book on Sherwood speak more detailed, but not very good understanding, must see a few more times.

Here I only say his basic idea : In the general input data of the program, input more or less will affect the computational complexity of the algorithm. At this point the Sherwood algorithm can be used to eliminate the calculation time required by the algorithm and the input instance of this connection.

Contact example, in the quick sort, we start with the first element as the benchmark, in order to avoid this situation, can be solved with the Sherwood algorithm, that is, the first datum element is random.

In advance: In later chapters, if you include the RandomNumber.h header file in the header file in your code, please see the pseudo-random number class Randomnumber in the "Probabilistic algorithm (1)-random number" that was written earlier. I'll stop reminding you later.

This is a non-recursive version of the Sherwood Quick Sort algorithm:

/*

* Author:tanky Woo
* Blog:www.WuTianQi.com
* date:2010.12.8
* Sherwood (Sherwood) algorithm application (1)--linear time selection algorithm
* Code to Wang Xiaodong "Computer Algorithm design and analysis"
*/

#include "RandomNumber.h"
#include <iostream>
#include <iomanip>
#include <time.h>
using namespace Std;
const int INF = 9999;

Exchange A, B's value
Template <typename type>
void Swap (Type &a, type &b)
{
Type temp;
temp = A;
A = b;
b = temp;
}

Template <typename type>
Type select (Type a[], int LT, int rt, int k)
{
Calculates the small element K in A[lt:rt]
Static Randomnumber Rnd;
while (true)
{
if (lt > rt)
return A[LT];
int i = lt, j = lt+rnd.   Random (rt-lt+1); Random selection of the dividing datum
Swap (A[i], a[j]);
j = rt+1;
Type pivot = a[lt];
Element exchange by dividing datum into axes
while (true)
{
while (A[++i] < pivot);
while (A[--j] > Pivot);
if (i >= j)
Break
Swap (A[i], a[j]);
}
if (j-lt + 1 = = k)
return pivot;
A[LT] = A[j];
A[J] = pivot;
Repeating the process of sub-array partitioning
if (J-lt + 1 < k)
{
K = k-j + lt-1;
lt = j + 1;
}
Else
RT = J-1;
}
}

Template <typename type>
Type Select (type a[], int n, int k)
{
Calculates the small element K in a[0:n-1]
Suppose A[n] is an element with an infinite key value
if (K < 1 | | k > N)
Cerr << "wrong!" << Endl;
Return select (A, 0, n-1, K);
}

int main ()
{
int arr[7] = {3, 2, 5, 7, Ten, INF};
cout << Select (arr, 6, 4) << Endl;
}

Of course, the Sherwood algorithm is not omnipotent. It is also sometimes encountered that the deterministic algorithm given cannot directly transform the Chengxhe-wood algorithm. At this time can be aided by the random preprocessing technology, do not change the original deterministic algorithm, only the random shuffle of its input, the same can receive the effect of Sherwood algorithm.

The following is a random shuffle algorithm:

/*
* Author:tanky Woo
* Blog:www.WuTianQi.com
* date:2010.12.8
* and Sherwood algorithm effect similar to a program-random shuffle
* Code to Wang Xiaodong "Computer Algorithm design and analysis"
*/

#include "RandomNumber.h"
#include <iostream>
#include <iomanip>
#include <time.h>
using namespace Std;

Exchange A, B's value
Template <typename type>
void Swap (Type &a, type &b)
{
Type temp;
temp = A;
A = b;
b = temp;
}

Template <typename type>
void Shuffle (Type a[], int len)
{
Random Shuffle algorithm
Static Randomnumber Rnd;
for (int i = 0; i < len; ++i)
{
Int J = rnd. Random (len-i) + i;
Swap (A[i], a[j]);
}
}

Template <typename type>
void Print (Type a[], int len)
{
for (int i=0; i<len; ++i)
cout << A[i] << "";
cout << Endl;
}

int main ()
{
int arr[10];
Original order
for (int i=0; i<10; ++i)
Arr[i] = i+1;
Print (arr, 10);


Shuffle for the first time
Shuffle (arr, 10);
Print (arr, 10);

Shuffle the second time
Shuffle (arr, 10);
Print (arr, 10);

return 0;
}

The original order and the first shuffle and the second shuffle are not the same.

To tell the truth, I am also very confused in this chapter, if there is a problem, you are welcome to point out.

The next chapter will be: "Randomization algorithm (4)-Las Vegas (Las Vegas) algorithm"

Tanky Woo Original, welcome reprint, reprint please attach link, please do not delete the article in any about this blog link.

Tanky Woo Tags: sherwood algorithm, stochastic algorithm

Sherwood algorithm (turn to explain the introduction of the algorithm topic!!!) )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.