Question

Consider an ordered array A of size n and the foll

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Il P The lawen bound semen item eu Lo at f size n

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

Add a comment
Know the answer?
Add Answer to:
Consider an ordered array A of size n and the following ternary search algorithm for finding...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Binary Search (Ternary Search)

    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...

    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...

    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...

    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...

    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...

  • 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 ar...

    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...

  • 4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up t...

    4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up to n, anda number num which we wish to insert into A, in the proper sorted position. The function Search finds the minimum index i such that num should be inserted into Ali]. It searches the array sequentially until it finds the location i. Another function MakeRoom moves A[i], .., AIn] to Ali+1]...AIn+1] same sort...

  • C++ please read all the question Create 3 large arrays to search, where the size will...

    C++ please read all the question Create 3 large arrays to search, where the size will be variable up until it reaches your machines limit. They should contain the same information since we are going to compare 3 different algorithms.You are going to do operational studies on them to determine their order. O(N), O(log(N)), and O(1)? Search algorithms.Linear, Binary, Hash You should know which algorithms are what order,now you are going to prove it. Fill the 3 arrays with the...

  • 1. Randomized Binary Search Which are true of the randomized Binary Search algorithm? Multiple answers:You can...

    1. Randomized Binary Search Which are true of the randomized Binary Search algorithm? Multiple answers:You can select more than one option A) It uses a Variable-Size Decrease-and-Conquer design technique B) Its average case time complexity is Θ(log n) C) Its worst case time complexity is Θ(n) D) It can be implemented iteratively or recursively E) None of the above 2. Randomized Binary Search: Example Assume you have an array, indexed from 0 to 9, with the numbers 1 4 9...

  • What is the maximum number of comparisons made when searching a 60 element array with Binary...

    What is the maximum number of comparisons made when searching a 60 element array with Binary Search? 60 30 5 6 Question 3 (3 points) What is the average number of comparisons made when searching a 60 element array with Linear Search? 06 5 A selection sort algorithm is used to sort an array containing the following values into ascending order. Give the order of the elements after each pass of the sorting algorithm. 6 4 7 2 3 5...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT