Example #1
0
/* binary search function. recursion */
bool Bsearch(int goal, int st, int ed) {
	int now = (st + ed) / 2;
	if(st > ed)	return false;
	if(arrN[now] > goal) 		return Bsearch(goal, st, now - 1);
	else if(arrN[now] < goal)	return Bsearch(goal, now + 1, ed);
	else return true;
}
Example #2
0
int main()
{
	int numbers[]={6,3,4,11,2,14,20,31,21};
	int n = (sizeof(numbers) / sizeof(numbers[0]));
	print(numbers, n);
	Qsort(numbers, n);
	print(numbers, n);

	int idx = Bsearch(numbers, n, 3);
	printf("idx of %d is %d\n", 3, idx);
	idx = Bsearch(numbers, n, 2);
	printf("idx of %d is %d\n", 2, idx);
	idx = Bsearch(numbers, n, 4);
	printf("idx of %d is %d\n", 4, idx);
}
int main()
{
	int i, len, res, pos, ncase = 1;
	while(scanf("%d", &arr[0]), arr[0] != -1)
	{
		for(i = 1;arr[i-1] != -1;i++)
			scanf("%d", &arr[i]);

		len = i -1 ;
		res = 0;
		tailM[0] = arr[0];
		for(i = 1;i < len;i++)
		{
			if (tailM[res] > arr[i])
			{
				res++;
				tailM[res] = arr[i];
			}
			else
			{
				pos = Bsearch(arr[i], res);
				tailM[pos] = arr[i];
			}
		}
		printf("Test #%d:\n  maximum possible interceptions: %d\n\n", ncase++, res + 1);
	}
	return 0;
}
int main()
{
	int i, n, res, pos;
	while(scanf("%d", &n) != EOF)
	{
		for(i = 0;i < n;i++)
			scanf("%d", &arr[i]);

		res = 0;
		tailM[0] = arr[0];
		for(i = 1;i < n;i++)
		{
			if (tailM[res] < arr[i])
			{
				res++;
				tailM[res] = arr[i];
			}
			else
			{
				pos = Bsearch(arr[i], res);
				tailM[pos] = arr[i];
			}
		}
		printf("%d\n", res + 1);
	}
	return 0;
}
int main(int argc, char **argv)
{
    debugMode= debugSwitch(argv);//either TRUE or FALSE
    DIR           *theDirectory;
    struct dirent *aFile;
    char inFileName[12];
    FILE *inFile;
    content *tableofContents;//point to the structs
    int fileCount;//number of .txt files
    char sml[7]; //the user will specify small,medium or large
    int stringLength;
    int displayPairs;
    int mossSpace;
    

    intro();
    
    theDirectory=specifyDirLengthPairs(sml,&stringLength,&displayPairs);
    
    tableofContents= allocateSpaceStruct(fileCount=ListFileNames(theDirectory));
    
    readInFileNames(tableofContents, theDirectory, sml);
    
    combineDIRandTxtName(tableofContents, sml, fileCount);
    
    numbValidChar(tableofContents,fileCount);
    
    mallocSpaceChar (tableofContents, fileCount);
    
    readInChar(tableofContents,fileCount);
    
    numberofBlocks(tableofContents,stringLength,fileCount);
    
    mallocBlocksofChar(tableofContents,fileCount, stringLength);
    
    sortBlocksChar(tableofContents,fileCount);
    
    moss* mossKeeper=mossMalloc(fileCount,&mossSpace);

    int mKindex=0;
    while(mKindex<mossSpace)
    {
        //printf("mkindex::%d,,txtA:%d,,txtB::%d\n",mKindex ,mossKeeper[mKindex].txtNumA,mossKeeper[mKindex].txtNumB);
        
        Bsearch(mossKeeper,tableofContents,mKindex,stringLength);
        mKindex++;
    }
    
    diplayTop(displayPairs,mossKeeper,mossSpace,tableofContents);
    
    freeAll(tableofContents,fileCount,mossKeeper);
    
    return 0;
 
}
Example #6
0
int main()
 {   
     int i,k,m;
	 double sum,total,temp;
     scanf("%lf %d",&total,&n);
	 for(i=1;i<=n;i++)
	 {
	   node[i].index=i;
	   scanf("%s",node[i].name);
	 }
	 qsort(node+1,n,sizeof(node[1]),cmp1);
	 scanf("%d",&m);
	 for(i=1;i<=m;i++)
	 {
		  scanf("%s %s %lf",name1,name2,&temp);
	      c[i].i=Bsearch(name1);
		  c[i].j=Bsearch(name2);
		  c[i].value=temp;
	 }	
	 qsort(c+1,m,sizeof(c[1]),cmp2); 
	 for(i=1;i<=n;i++)
			Makeset(i);
	 sum=0.0;k=1;
	 for(i=1;i<=m;i++)
	 {
		if(Find(c[i].i)!=Find(c[i].j))
		{
				sum=sum+c[i].value;
				Union(c[i].i,c[i].j);
				k++;
		}
		if(k==n)
				break;
	}
	if(total>=sum)
	    printf("Need %.1lf miles of cable\n",sum);
	else
	    printf("Not enough cable\n");
    return 0;
 }     
Example #7
0
int main(void) {
	int i, num;
	scanf("%d", &n);
	for(i=0; i<n; i++) {
		scanf("%d", &arrN[i]);
	}
	qsort(arrN, n, sizeof(int), compare);
	scanf("%d", &m);
	for(i=0; i<m; i++) {
		scanf("%d", &num);
		printf("%d\n", (int)Bsearch(num, 0, n - 1));
	}
}
int RMQ(int i,int j)
{
 int m,max,s;
 m=int(log((double)(j-i+1))/log(2.0));
 max=f[i][m]>f[j-p[m]+1][m]?f[i][m]:f[j-p[m]+1][m];
 if(h[i+p[m]-1]==h[i+p[m]])
 {
   s=Bsearch(i+p[m],j,h[i+p[m]])-(i+p[m])+1;
   max=max>s+right[i][m]?max:s+right[i][m];
 }
 if(h[j-p[m]]==h[j-p[m]+1])
 {
   s=j-p[m]-Msearch(i,j-p[m],h[j-p[m]])+1;
   max=max>s+left[j-p[m]+1][m]?max:s+left[j-p[m]+1][m];
 }
 return max;
}
Example #9
0
int main()
{
	int s[100], ans[100];
        int best[100], len[100];
        int max;
        int n, i;
	int j;

        scanf("%d", &n);
        scanf("%d", &s[0]);
	best[1] = s[0];
        len[0] = max = 1;
        for(i=1; i<n; i++){
		printf("%d: %d\n\t", i, max);
		for(j=1; j<=max; j++)
			printf("%d ", best[j]);
		printf("\n");
		scanf("%d", &s[i]);
		if(s[i] > best[max]){
			max++;
			best[max] = s[i];
			len[i] = max;
		}
		else{
			int index = Bsearch(best, max, s[i]);
			if(index != -1){
				best[index] = s[i];
				len[i] = index;
			}
		}
        }
	printf("------ANSWER------\n");
	printf("max: %d\n", max);
	int t=max;
	for(i=n-1; i>=0; i--){
		if(len[i] == t){
			ans[t] = s[i];
			t--;
		}
	}
	for(i=1; i<=max; i++)
		printf("%d ", ans[i]);
	printf("\n");
        return 0;
}
int main()
{
    int test;
    
    int terminal=-999;//this is for input text, so the loop knows when to stop

    int size=100;//Initial size
    int i=0;

    int *Data=Create(size);
    
    scanf("%d",&Data[i]);//Read in our initialize variable for our array
    //printf("i:%d and val:%d\n",i,Data[i]);
    i++;
    
    //????Even without increasing the allocated space the array was not Seg faulting
    
    while (Data[i-1]!=terminal)//This looks at the previous posi
    {
        scanf("%d",&Data[i]);
        //printf("i:%d and val:%d\n",i,Data[i]);
        i++;
        if (i==size)//this counter counts up size malloced for array, if true goes into function
        {
            size=size*2;
            Data=Inc_Cap(Data,size);
        }
    }
    printf("\nReading in Data Complete\n");
    size=i-1;//the get rid of the -999 in the array because we dont want that sorted
   
    //printf("Test_shot\n");
    
    int *lsearch_numComp_P;//This is for number of comparisons because we want to return to values from functions
    int *bsearch_numComp_P;
    
    int lsearch_numComp=0;
    int bsearch_numComp=0;
    
    lsearch_numComp_P=&lsearch_numComp;//Take the address of the right because we need to point to that space
    bsearch_numComp_P=&bsearch_numComp;
    
    
    int* toArray=Create(size);//We are mallocing because to we can free it later
    
    arrayCopy(Data,toArray,size);//self- explanatory
    
    sort(Data,size);
    
    i=0;
   
    int target;//This is the value that need to be found for binary and linear search
    scanf("%d",&target);
     //printf("Target Value: %d\n",target);
    while (target!=terminal)
    {
        
        //scanf("%d",&target);
        //printf("Target Value: %d\n",target);
    
        int Print_Lsearch=lsearch(toArray,size,target,lsearch_numComp_P);//reason for this becaue we are sending the index location
    
        if (Print_Lsearch==-1)//because of the above statement is the reason for this "if" statement
        {//we dont want to run the same function twice becasue it would be unneccassay
            printf("Value not found in Linear search\n");
        }
        else
            printf("Target Value:%d found with Linear search in Array position %d with %d comparisons\n",target,Print_Lsearch,*lsearch_numComp_P);
    
    
        int Print_Bsearch=Bsearch(Data,size,target,bsearch_numComp_P);
    
        if (Print_Bsearch==-1)
        {  //Same sort of logic as lsearch explaniation for "if"
            printf("Value not found in Binary search\n");
        }
        else
            printf("Target Value:%d found with Binary search in Array position %d with %d comparisons\n",target,Print_Bsearch,*bsearch_numComp_P);
        
        lsearch_numComp=0;
        bsearch_numComp=0;
        scanf("%d",&target);
    
    }
    
    free_space(Data);
    free_space(toArray);
    
    return 0;
}