Ejemplo n.º 1
0
void main()
{
//   TimeSearch3();

   int b[11];
   b[1] = 2; b[2] = 4; b[3] = 5; b[4] = 7; b[5] = 10;
   b[6] = 12; b[7] = 14; b[8] = 15; b[9] = 17; b[10] = 25;

   cout << seqsearch(b,10,10) << "\n";
   cout << seqsearch(b,10,11) << "\n";
}
Ejemplo n.º 2
0
void TimeSearch2() {

   int a[1001], n[20];
   const long r[20] = {300000, 300000, 200000, 200000, 100000, 100000, 100000,
   80000, 80000, 50000, 50000, 25000, 15000, 15000, 10000, 7500, 7000,
   6000, 5000, 5000};
   for (int j = 1; j <= 1000; j++) // intialize z
      a[j] = j;
   for (j = 0; j < 10; j++)  {// values of n
      n[j] = 10*j; n[j+10] = 100 * (j+1);
   }
   cout << "\tn\ttotal\trunTime" << endl;
   for(j = 0; j < 20; j++)  { // obtain computing times
       struct timeb start;
       struct timeb stop;

       ftime(&start); // get time
       for (long b = 1; b <= r[j]; b++)
	   int k = seqsearch(a, n[j], 0); // unsuccessful search
       ftime(&stop); // get time
       long total = (stop.time - start.time) * 100 + (stop.millitm - start.millitm)/10;
       float runTime = total/r[j];
       cout << "\t" << n[j] << "\t" << total << "\t" << runTime << endl;
   }

   cout << "Times are in hundredths of a second.\n";
}
Ejemplo n.º 3
0
int main()
{	int a[N]={6,3,2,5,1,4,8,0,7,9};
	int value,pos;
	int i;
	for(i=0;i<N;i++){
	printf("%d",a[i]);
	
	}
	printf("\n");
	printf("Please input:");
	scanf("%d",&value);
	pos=seqsearch(value,a,N);
	if(pos==-1)
	{
		printf("Failed!\n");
	
	
	}else{
	printf("Success!");
	printf("array[%d]=%d\n",pos,value);
	
	}
	
	return 0;
 } 
Ejemplo n.º 4
0
int main(void)
{
      int a[N] = {2,7,5,8,4,3,9,1};
      int key,ret;
      show(a);
      while(1){
	    printf("请输入key:");
	    scanf("%d",&key);
	    ret = seqsearch(a,key);  //找到返回记录的下标,失败返回-1
	    if(ret == -1){
		  printf("记录不存在!\n");
	    }else
		  printf("key为%d的记录在%d位置!\n",key,ret);
      }

      return 0;
}
Ejemplo n.º 5
0
void TimeSearch() {
   int a[1001], n[20];
   for (int j = 1; j <= 1000; j++) // initialize a
      a[j] = j;
   for (j = 0; j < 10; j++)  {// values of n
      n[j] = 10*j; n[j+10] = 100 * (j+1);
   }
   cout << "n\ttime" << endl;
   for(j = 0; j < 20; j++)  { // obtain computing times
       struct timeb start;
       struct timeb stop;
       ftime(&start); // get time
       int k = seqsearch(a, n[j], 0); // unsuccessful search
       ftime(&stop); // get time
       long runTime = (stop.time - start.time) * 1000 + (stop.millitm - start.millitm);
       cout << n[j] << "\t" << runTime/10 << endl;
   }

   cout << "Times are in hundredths of a second.\n";
}
Ejemplo n.º 6
0
int main()
{
	freopen("basic_inputs.txt", "r", stdin);
	setbuf(stdout, NULL);
	// inputs:
	scanf("%d %d", &n, &key);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
		
	printf("Input items:\n");
	for (int i = 1; i <= n; i++)
		printf("%d ", a[i]);
	printf("\n");

	// solve 1: find a key via sequential search
	int ret1 = seqsearch(n, key);
	printf("1.find a key = %d\n", ret1);

	// solve 2: sum of items
	int ret2 = sum();
	printf("2.sum of items = %d\n", ret2);

	// solve: qsort
	qsort(1, n);
	
	printf("Sorted items:\n");
	for (int i = 1; i <= n; i++)
		printf("%d ", a[i]);
	printf("\n");

	// solve 3: find a key via binary search
	int ret3 = binsearch(n, key);
	printf("3.find a key = %d\n", ret3);

	// solve 4: find a key via binary search based on recursion
	int ret4 = location_recursion(1, n);
	printf("4.find a key = %d\n", ret4);
	
	return 0;
}
Ejemplo n.º 7
0
int main()
{
	int i;
	int search_number;
	int return_value;
	clock_t start, finish;
	clock_t  duration;

	for(i=0;i<MAX_ELEMENTS;i++)
		list[i] = i;
	
	printf("숫자를 입력하시요.\n", &search_number);
	scanf("%d", &search_number);

    start = clock();
	return_value = seqsearch(list, MAX_ELEMENTS, search_number); 
	finish = clock();

    duration = finish - start;
    printf( "%d milliseconds\n", duration );
    printf( "문자의 수행횟수=%d\n ", count );

    start = clock();
	return_value = binsearch(list, MAX_ELEMENTS, search_number); 
	finish = clock();

    duration = finish - start;
    printf( "%d milliseconds\n", duration );
    printf( "문자의 수행횟수=%d\n ", count );

	if( return_value == -1 ){
		printf("숫자가 발견되지 않았읍니다.\n", &search_number);
	}
	else{
		printf("숫자가 위치 %d에서 발견되었읍니다.\n", return_value);
	}
	return 0;
}
Ejemplo n.º 8
0
int main()
{
	int i,j,position;
	int list[MAX_SIZE];
	int sizelist[] = {0,10,20,30,40,50,60,70,80,90,100,200,300,400,500,600,700,800,900,1000};
	int numtimes[] = {0,10,20,30,40,50,60,70,80,90,100,200,300,400,500,600,700,800,900,1000};
	clock_t start,stop;
	double duration,total;
	for(i = 0;i<MAX_SIZE;i++)
		list[i] = i;
	for(i =0;i<ITERATIONS;i++)
	{
		start = clock();
		for(j=0;j<numtimes[i];i++)
		{
			position = seqsearch(list,-1,sizelist[i]);
		}
		stop = clock();
		total = ((double)(stop-start))/CLK_TCK;
		duration = total/numtimes[i];
		printf("%5d %d  %d  %f  %f\n", sizelist[i],numtimes[i],(int)(stop-start),total,duration);
		list[sizelist[i]] =sizelist[i];
	}
}