
The experiment in C
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define n 4000
int bcount=0,tcount=0;
int main()
{
int a[n];
int i,k,pos;
clrscr();
for(i=0;i<n;i++)
{
a[i]=(int)(8*sqrt(i));
}
srand(time(NULL));
k=rand() % (int)(8*sqrt(n));
pos=binarysearch(a,0,n-1,k);
printf("%d is in %d ",k,pos);
pos=terenarysearch(a,0,n-1,k);
printf("\n%d is in %d ",k,pos);
printf("\nTotal comparison in binary search
:%d",bcount);
printf("\nTotal comparison in terenary search :
%d",tcount);
return 0;
}
int binarysearch(int a[],int p,int r,int k)
{
int mid;
if(p>r) {bcount++;
return -1;}
mid=(p+r)/2;
if (k==a[mid])
{
bcount++;
return mid;
}
if(a[mid]>k)
{
bcount++;
return
binarysearch(a,p,mid-1,k);
}
else
{
bcount++;
return
binarysearch(a,mid+1,r,k);
}
}
int terenarysearch(int a[],int p,int r,int k)
{
int mid1,mid2;
if(p>r){tcount++; return -1;}
mid1=p+(r-p)/3;
mid2= p+2*(r-p)/3;
if (k==a[mid1])
{
tcount++ ;
return mid1;
}
if (k==a[mid2])
{
tcount++ ;
return mid2;
}
if(a[mid1]>k)
{
tcount++ ;
return
terenarysearch(a,p,mid1-1,k);
}
else if(a[mid1]<k && a[mid2]>k)
{
tcount++ ;
return
terenarysearch(a,mid1+1,mid2-1,k);
}
else
{
tcount++ ;
return
terenarysearch(a,mid2+1,r,k);
}
}
|
N=500 |
1000 |
2000 |
|
|
Binary,terenary |
Binary,terenary |
Binary,terenary |
|
|
Exp-1 |
6,5 |
3,5 |
7,5 |
|
Exp-2 |
6,5 |
7,5 |
7,5 |
|
Exp-3 |
7,2 |
7,5 |
8,6 |
|
Exp-4 |
7,5 |
6,5 |
12,9 |
|
Exp-5 |
3,3 |
8,4 |
8,4 |
Sorry for only 5 experiment. Time did not permit for all experiment..
how ever you can run the code above 10 times.
From these observation, I can conclude that terenary search is efficient as compared to bany search
Consider an ordered array A of size n and the following ternary search algorithm for finding...
Ternary Search is a generalization of Binary Search that can be used to find an element in an array. Itdivides the array withnelements into three parts and determines, with two comparisons, which partmay contain the value we are searching for. For instance, initially, the array is divided into three thirdsby taking mid1=(n−1)/3 and mid2=((2(n−1))/3. Write a recurrence for the running time of Ternary Search and solve this recurrence.
We are using sequential search to search an array of size n. It is known that the item we are looking is definitely present in the array. The probability that the item we are looking for is the last one in the array is 1/3. The probabilities of all other items are equal. What is the average case time complexity(counting the number of comparisons) of the algorithm in this case?
Searching/sorting tasks and efficiency analysis - Binary Search Search for the character S using the binary search algorithm on the following array of characters: A E G K M O R S Z. For each iteration of binary search use a table similar to the table below to list: (a) the left index and (b) the right index of the array that denote the region of the array that is still being searched, (c) the middle point of the array,...
The binary search algorithm from Chapter 9 is a very efficient algorithm for searching an ordered list. The algorithm (in pseudocode) is as follows: highIndex - the maximum index of the part of the list being searched lowIndex - the minimum index of the part of the list being searched target -- the item being searched for //look in the middle middleIndex = (highIndex + lowIndex) / 2 if the list element at the middleIndex is the target return the...
JH: Student Name: 4) Given the following array, do the following (show all the work). A (56, 89, 23, 58, 22, 11, 45, 48, 90) (a - 5 pts) Construct a hash table for the given array using the hash function H(K)- K mod 5 (b- 4 pts) Determine the average number of comparisons for a successful search using the hash table of (c -3 pts) What is the worst case number of comparisons for an unsuccessful search in the...
Java: Write an application that has an array of at least 20 integers. It should call a method that uses the sequential search algorithm to locate one of the values. The method should keep a count of the number of comparisons it makes until it finds the value. Then the program should call another method that uses the binary search algorithm to locate the same value. It should also keep count of the number of comparisons it makes. Display these...
Implement a binary search function that takes an integer query key and determines whether it is in a sorted array of n integers. It should return the index of the key, if found, otherwise it should return a negative number. You should implement the function both iteratively and recursively to be sure you really understand the algorithm. Just for the fun of it, you might want to verify the O(log(n)) complexity of your function by counting the number of comparisons...
6.3.1 [10] <§6.2> Consider the following binary search algorithm (a classic divide and conquer algorithm) that searches for a value X in a sorted N-element array A and returns the index of matched entry: BinarySearch(A[0..N−1], X) { low = 0 high = N −1 while (low <= high) { mid = (low + high) / 2 if (A[mid] >X) high = mid −1 else if (A[mid] <X) low = mid + 1 else return mid // found } return −1...
The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an array in ascending order. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that...
Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. The file main1.txt on the webpage contains code that generates a specific array of integers...