The fourth chapter prepares the correct procedure __ writes the correct procedure

Source: Internet
Author: User
Tags assert rand

Write the correct principle of the program:

Write the correct two-part finder:

#include <stdio.h> #include <stdlib.h> #include <time.h> #define MAXN 1000000 typedef int DATATYPE;
DataType X[MAXN];

int n;
/* Scaffolding/int i =-999999;

#define ASSERT (V) {if ((v) = 0) printf ("BinarySearch Bug%d%d\n", I, n);}
	/* ALG 1:from programming pearls, Column 4:raw transliteration */int binarysearch1 (DataType t) {int L, u, M;
	L = 0;
	u = n-1; for (;;)
		{if (L > U) return-1;
		m = (l + u)/2;
		if (X[m] < T) L = m+1;
		else if (x[m] = = t) return m;
	else/* X[m] > t/u = m-1;
	}/* Alg 2:make binarysearch1 more c-ish */int binarysearch2 (DataType t) {int L, u, M;
	L = 0;
	u = n-1;
		while (l <= u) {m = (l + u)/2;
		if (X[m] < T) L = m+1;
		else if (x[m] = = t) return m;
	else/* X[m] > t/u = m-1;
} return-1;
	/* ALG 3:from PP, Col 8 */int Binarysearch3 (DataType t) {int L, u, M;
	L =-1;
	u = N;
		while (l+1!= u) {m = (l + u)/2;
		if (X[m] < T) L = m;
Else			u = m;
	} if (U >= n | | x[u]!= t) return-1;
return u;
	/* ALG 4:from PP, Col 9 */int binarysearch4 (DataType t) {int L, p;
	if (n!= 1000) return Binarysearch3 (t);
	L =-1;
	if (x[511] < T) L = 1000-512;
	if (x[l+256] < T) L + = 256;
	if (x[l+128] < T) L + = 128;
	if (x[l+64] < T) L + = 64;
	if (x[l+32] < T) L + = 32;
	if (x[l+16] < T) L + = 16;
	if (X[l+8] < T) L + = 8;
	if (X[l+4] < T) L + = 4;
	if (x[l+2] < T) L + = 2;
	if (X[l+1] < T) L + = 1;
	p = l+1;
	if (P >= n | | x[p]!= t) return-1;
return p;
    /* ALG 9:buggy, from Programming pearls, Column 5 */int sorted () {int i;
    for (i = 0; i < n-1 i++) if (x[i) > x[i+1]) return 0;
return 1;
	int binarysearch9 (DataType t) {int L, u, m/* int oldsize, size = n+1; * L = 0;
	u = n-1; while (l <= u) {/* oldsize = size, size = U-l +1; assert (Size < oldsize); */M = (l + u)/2;/printf ("%d% D%d\n ", L, M, u); */if (x[M] < T) L = m;
		else if (X[m] > t) u = m;
		else {/* ASSERT (x[m] = = t);/return m; }/* ASSERT (X[l] > t && x[u] < T);
* * RETURN-1;
	/* ALG 21:simple Sequential search */int seqsearch1 (DataType t) {int i;
	for (i = 0; i < n; i++) if (x[i] = = t) return i;
return-1;
	}/* ALG 22:faster sequential search:sentinel */int seqsearch2 (DataType t) {int i;
	DataType hold = X[n];
	X[n] = t;
	for (i = 0;; i++) if (x[i] = = t) break;
	X[n] = hold;
	if (i = = n) return-1;
else return i;
	}/* ALG 23:faster sequential search:loop unrolling */int Seqsearch3 (DataType t) {int i;
	DataType hold = X[n];
	X[n] = t;
		for (i = 0;; i+=8) {if (x[i] = = t) {break;}
		if (x[i+1] = = T) {i = 1; break;}
		if (x[i+2] = = T) {i = 2; break;}
		if (x[i+3] = = T) {i = 3; break;}
		if (x[i+4] = = T) {i = 4; break;}
		if (x[i+5] = = T) {i = 5; break;}
		if (x[i+6] = = T) {i = 6; break;} if (x[i+7] = = T) {i = 7; Break
	}} X[n] = hold;
	if (i = = n) return-1;
else return i;
	}/* Scaffolding to probe one algorithm/void Probe1 () {int i;
	DataType T;
		while (scanf ("%d%d", &n, &t)!= EOF) {for (i = 0; i < n; i++) x[i] = 10*i;
	printf ("%d\n", Binarysearch9 (t));
	}/* Torture test ONE algorithm */#define s SEQSEARCH3 void Test (int maxn) {int i;
		for (n = 0; n <= maxn; n++) {printf ("n=%d\n", N);
		/* DISTINCT elements (plus one at top) * * for (i = 0; I <= N; i++) x[i] = 10*i;
			for (i = 0; i < n; i++) {assert (S (10*i) = i);
		ASSERT (S (10*i-5) = = 1);
		ASSERT (S (10*n-5) = = 1);
		ASSERT (S (10*n) = = 1);
		/* Equal elements * * for (i = 0; i < n; i++) x[i] = 10;
		if (n = = 0) {assert (S (10) = = 1);
		else {assert (0 <= s () && s () < n);
		ASSERT (S (5) = = 1);
	ASSERT (S (15) = = 1);

}/* Timing */int P[MAXN];
	void Scramble (int n) {int i, J;
	DataType T; for (i = n-1 i > 0; i--) {j = (Rand_max*rand () + RAND ())% (i + 1); t = P[i]; P[i] = P[j];
	P[J] = t;
	} void Timedriver () {int I, algnum, numtests, test, start, clicks;
		while (scanf ("%d%d%d", &algnum, &n, &numtests)!= EOF) {for (i = 0; i < n; i++) x[i] = i;
		for (i = 0; i < n; i++) p[i] = i;
		Scramble (n);
		start = Clock (); for (test = 0; test < numtests. test++) {for (i = 0; i < n; i++) {switch (algnum) {Case 1:assert (bin Arysearch1 (p[i]) = = P[i]);
				Break Case 2:assert (BINARYSEARCH2 (p[i]) = = P[i]);
				Break Case 3:assert (Binarysearch3 (p[i]) = = P[i]);
				Break Case 4:assert (BINARYSEARCH4 (p[i]) = = P[i]);
				Break Case 9:assert (BINARYSEARCH9 (p[i]) = = P[i]);
				Break Case 21:assert (Seqsearch1 (p[i]) = = P[i]);
				Break Case 22:assert (SEQSEARCH2 (p[i]) = = P[i]);
				Break Case 23:assert (Seqsearch3 (p[i]) = = P[i]);
				Break
		}} clicks = Clock ()-start;
printf ("%d\t%d\t%d\t%d\t%g\n", Algnum, N, numtests, clicks,			1e9*clicks/((float) clocks_per_sec*n*numtests));
	}/* Main/int main () {/* PROBE1 (); */* Test (); */Timedriver ();
return 0; }

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.