Question

In: Computer Science

Suppose I have a list of 128 unsorted numbers. If I use the binary search to...

Suppose I have a list of 128 unsorted numbers. If I use the binary search to search it, how many comparisons will I have to do in the worst case, assuming I use a quadratic algorithm to sort the list first.

Solutions

Expert Solution

Hi,

Sorting is the process of ordering the elements in an array. There are different types of sorting techniques.

For 128 unsorted numbers of a list the comparisons in worst case will be: 9

Binary Search algorithm:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
struct tree
{  
int data;
struct tree *left;
struct tree *right;
};
struct tree *create();
void preorder(struct tree *);
void inorder(struct tree *);
void postorder(struct tree *);
struct tree *create()
{
struct tree *p,*root;
int m,x;
char s;
root=(struct tree *)malloc(sizeof(struct tree));
printf("\nenter the value of the main root");scanf("%d",&m);
root->data=m;
root->left=NULL;
root->right=NULL;
printf("\nEnter (y/n) to creation of the binary search tree :");
fflush(stdin);
scanf("%c",&s);
while(s!='n')
{
p=root;
printf("\nenter the value of the newnode");
fflush(stdin);
scanf("%d",&x);
while(1)
{
if(x<p->data)
{
if(p->left==NULL)
{
p->left=(struct tree *)malloc(sizeof(struct tree));
p=p->left;
p->data=x;
p->right=NULL;
p->left=NULL;
break;
}
else
p=p->left;
}
else
{
if(p->right==NULL)
{
p->right=(struct tree *)malloc(sizeof(struct tree));
p=p->right;
p->data=x;
p->right=NULL;
p->left=NULL;
break;
}
else
p=p->right;
}
}
printf("\nwant to continue-- press (y/n)");
fflush(stdin);
scanf("%c",&s);
}
return(root);
}
void preorder(struct tree *p)
{
if(p!=NULL)
{
printf("%d ",p->data);
preorder(p->left);
preorder(p->right);
}
}
void inorder(struct tree *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("\t%d",p->data);
inorder(p->right);
}
}
void postorder(struct tree *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf("\t%d",p->data);
}
}
void main()
{
int h;
struct tree *root;
clrscr();
while(1)
{
printf("\n1. for creation of the binary search tree");
printf("\n2. for preorder traversal");
printf("\n3. for inorder traversal");
printf("\n4. for postorder traversal");
printf("\n5. for exit");
printf("\nEnter your choice: ");
scanf("%d",&h);
switch(h)
{
case 1:
root=create();
break;
case 2:
preorder(root);
break;
case 3:
inorder(root);
break;
case 4:
postorder(root);
break;
case 5:
exit(0);
default:
printf("\nEntered a wrong choice.");
}
}
}


Related Solutions

I know this qu is posted but I have not got the answer BY unsorted list...
I know this qu is posted but I have not got the answer BY unsorted list using STLl!! 1.(70) An organization that your little cousin belongs to is selling low-fat cookies. If your cousin's class sells more cookies than any other class, the teacher has promised to take the whole class on a picnic. Of course, your cousin volunteered you to keep track of all the sales and determine the winner. Each class has an identification number. Each sales slip...
Create a List object that uses the binary search algorithm to search for the string "A"....
Create a List object that uses the binary search algorithm to search for the string "A". Display a message box indicating whether the value was found. Language: C#
Binary Tree Create a binary search tree using the given numbers in the order they’re presented....
Binary Tree Create a binary search tree using the given numbers in the order they’re presented. State if the resulting tree is FULL and/or BALANCED. 37, 20, 18, 56, 40, 42, 12, 5, 6, 77, 20, 54
I am trying to implement a search function for a binary search tree. I am trying...
I am trying to implement a search function for a binary search tree. I am trying to get the output to print each element preceding the the target of the search. For example, in the code when I search for 19, the output should be "5-8-9-18-20-19" Please only modify the search function and also please walk me through what I did wrong. I am trying to figure this out. Here is my code: #include<iostream> using namespace std; class node {...
In what situation could you not use a binary search
In what situation could you not use a binary search
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with comments and I need the input file to be placed with Scanner not BufferedReader Please help I need Class River Class CTRiver and Class Driver Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles...
In C++ with lots of comments please Complete a binary search tree of 20 numbers Show...
In C++ with lots of comments please Complete a binary search tree of 20 numbers Show in the output the steps while it's performing the search with 20 numbers in a text file called list.txt The numbers will be imported to the program Simple program that should let you have the option to search for numbers, remove numbers, print numbers, and insert numbers in the binary tree If the number isn't there then give an error
A binary search tree can be built with a traditional insertion method given a list of...
A binary search tree can be built with a traditional insertion method given a list of integers. Binary search trees (BSTs) are binary trees where the data is ordered such that nodes in the subtree to the left of a given node are smaller than or equal to the node, and the right subtree will contain nodes with values greater than the given node. With a built binary search tree, one can traverse the tree to print each node’s data...
Assume that you want to implement binary search with a linked list. What would be the...
Assume that you want to implement binary search with a linked list. What would be the performance of this algorithm? Compare and contrast this algorithm with the implementation of binary search on traditional sorted array and give a discussion. Your discussion and analysis must explain what the possibilities, issues and consequences of such design are, and then explain whether these issues would exist in the traditional array approach. Your answer can be around 1-2 paragraph of writing backed-up with algorithmic...
A binary search tree can be built with a traditional insertion method given a list of...
A binary search tree can be built with a traditional insertion method given a list of integers. Binary search trees (BSTs) are binary trees where the data is ordered such that nodes in the subtree to the left of a given node are smaller than or equal to the node, and the right subtree will contain nodes with values greater than the given node. With a built binary search tree, one can traverse the tree to print each node’s data...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT