Question

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

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 same information but use random strings of say size 20 characters.Or use 1 array and 3 separate programs.
You will perform searches on the arrays given strings that are in the array and those that are not. Make this about a 50-50 mix, or at least know what the mix is
.
a) Perform the linear search, record the time. If it does it very quickly you will need to adjust the size and the number of loops over the data till you get a few seconds. Then start increasing N. The object is to see how time increases as N increases.
b) Perform the same task with the Binary Search.Obviously using Binary the data first has to be ordered. This is an O(N^2) to O(NlogN) function just on it's own but it only needs to be done once. Don't take this time into account. Yes, I know that's bad, but I just want to show specific algorithm differences not including the sort.
c) Perform the same task with the Hash function.This is tricky since you have to develop the Hash function. Also, I want you to use chaining. This makes it easy since there are no collisions and you don't have to use algorithms for collisions, just use a linked list when a collision occurs.
When done, plot the results of each algorithm as Time vs. N It should be easy to conclude the order of the algorithm.

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

CODE:

#include<iostream>

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include<time.h>

#include <list>

using namespace std;

class Algorithms

{

public:

int *LinearArray;

int *BinaryArray;

int *Temp_array;

int size,count;

list<int> *table;

int BUCKET;

public:

Algorithms(int size)

{

LinearArray = (int *)malloc(20 * sizeof(int));

BinaryArray = (int *)malloc(20 * sizeof(int));

Temp_array = (int *)malloc(20 * sizeof(int));

this->size=size;

this->BUCKET=size;

table=new list<int>[BUCKET];

}

~Algorithms()

{

delete LinearArray;

delete BinaryArray;

delete Temp_array;

}

public:

void readData()

{

int random_Number;

srand((unsigned)time(0));

for(int i=0;i<size;i++)

{

random_Number=(rand()%100)+1;

Temp_array[i]=BinaryArray[i]=LinearArray[i]=random_Number;

}

}

public:   

void printdata()

{

for(int i=0;i<size;i++)

{

cout<<Temp_array[i]<<"\t";

}

cout<<"\n"<<endl;

}

public:

void Linear_Search(int key)

{

bool status=false;

int i;

for(i=0;i<size;i++)

{

if(key==LinearArray[i])

{

status=true;

break;

}

}

if(status==false)

cout<<"\nThe Key is Not Found"<<endl;

else

cout<<"\nThe Key is present at "<<i <<" Location"<<endl;

}

public:

int binarySearch(int array[], int leftindex, int rightindex, int key)

{

if (rightindex>= leftindex)

{

int mid =leftindex+ (rightindex -leftindex)/2;

if (array[mid] == key)  

return mid;

if (array[mid] > key)

return binarySearch(array,leftindex, mid-1,key);

return binarySearch(array, mid+1,rightindex,key);

}

return -1;

}

void insertItem(int key)

{

int index = hashFunction(key);

table[index].push_back(key);

}

int hashFunction(int x)

{

return (x % BUCKET);

}

int HashSearch(int key)

{

list <int> :: iterator i;

int index = hashFunction(key);

for (i = table[index].begin();i != table[index].end(); i++)

{

if (*i == key)

return 0;

}

return -1;

}

};

int main()

{

Algorithms obj(10);

time_t start,end;

double tc;

cout<<"\n------------------------------"<<endl;

cout<<"\n*** Implementation Of the Algorithm ***"<<endl;

obj.readData();

cout<<"\nInput Data Elements are"<<endl;

obj.printdata();

cout<<"\nEnter the key to Search in a list"<<endl;

int key;

cin>>key;

cout<<"\nPerforming Linear search "<<endl;

start=clock();

obj.Linear_Search(key);

end=clock();

tc=(difftime(end,start)/CLOCKS_PER_SEC);

cout<<"\ntime efficiency of Linear search is: "<<tc<<endl;

start=clock();

cout<<"\nNow Performing Binary Search "<<endl;

int result=obj.binarySearch(obj.BinaryArray,0,obj.size,key);

if(result==-1)

{

cout<<"\nThe Key is Not found Binary Search "<<endl;

}

else

cout<<"\nThe Key is Found at "<<result<<" Location"<<endl;

end=clock();

tc=(difftime(end,start)/CLOCKS_PER_SEC);

cout<<"\ntime efficiency of Binary search is: "<<tc<<endl;

for(int i=0;i<obj.size;i++)

{

obj.insertItem(obj.Temp_array[i]);

}

cout<<"\nNow Performing Hash Function "<<endl;

result=obj.HashSearch(key);

if(result==-1)

cout<<"\nKey Not Found in Hashtable"<<endl;

else

cout<<"\nKey is Found in Hashtable"<<endl;

}

Sample Output:-

--------------------

------------------------------

Implementation Of the Algorithm

Input Data Elements are

35 94 79 59 60 28 57 47 15 36

Enter the key to Search in a list

15

Performing Linear search

The Key is present at 8 Location

time efficiency of Linear search is: 0

Now Performing Binary Search

The Key is Not found Binary Search

time efficiency of Binary search is: 0

Now Performing Hash Function

Key is Found in Hashtable

--------------------------------

Process exited after 52.19 seconds with return value 0

Press any key to continue . . .

Implementation Of the Algorithm** Input Data Elements are 59 28 57 35 94 79 60 47 15 36 Enter the key to Search in a list 15

Please like it........

Add a comment
Know the answer?
Add Answer to:
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 t...
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
  • 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...

  • PLEASE DO BOTH #5 AND #6. The purpose of the project is to perform a timing...

    PLEASE DO BOTH #5 AND #6. The purpose of the project is to perform a timing experiment. You are required to complete the following activities: Write a computer program that prompts the user for a number, creates an array for that number of random integers, and then usees the bubble sort to order the array. The program should print out the array prior to the call to the sorting algorithm and afterwards. You can write the program in either Java,...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • How to write the insert, search, and remove functions for this hash table program? I'm stuck......

    How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...

  • In C++ Please!!!!! Example main: #include <iostream> #include <fstream> #include <istream> #include <cstring> using namespace std;...

    In C++ Please!!!!! Example main: #include <iostream> #include <fstream> #include <istream> #include <cstring> using namespace std; const int MAXRESULTS = 20; // Max matches that can be found const int MAXDICTWORDS = 30000; // Max words that can be read in int main() { string results[MAXRESULTS]; string dict[MAXDICTWORDS]; ifstream dictfile; // file containing the list of words int nwords; // number of words read from dictionary string word; dictfile.open("words.txt"); if (!dictfile) { cout << "File not found!" << endl; return...

  • Detecting Substrings (C++ Version) Introduction A very common task that is often performed by programs that...

    Detecting Substrings (C++ Version) Introduction A very common task that is often performed by programs that work with text files is the problem of locating a specific substring within the file. I am sure we’ve all done this many times when working with Word, Notepad, or other editors. Since we don’t have a GUI or other means of displaying the contents of a file all at once, let’s modify the problem slightly. Rather than locating a specific substring within a...

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

  • These are my answere to the following questions: are they right? 1. B 2. T 3....

    These are my answere to the following questions: are they right? 1. B 2. T 3. T 4. T 5. F 6. T 7. A 8. D 9. E 10. B 11. B 12. A 13. A 14. D 15. C 16. D 17. T 18. C 19. T 20. T 21. T 22. A 23. T 24. D 25. B 26. A 27. A 28. A 29. T 30. C 31. D 32. A 33. T 34. F 35....

  • In Problem Set 7 you designed and implemented a Message class. This time, let's design and...

    In Problem Set 7 you designed and implemented a Message class. This time, let's design and implement a Mailbox class in a file named Mailbox java. Do the following with this class • You may use the Message class from PS 7. You will have to add new features to the Message class from PS 7 as you work through this problem. You are welcome to start with my sample solution if you wish • Suppose there are multiple mail...

  • C#: Implement a multiplayer Battleship game with AI The rules are the same as before. The...

    C#: Implement a multiplayer Battleship game with AI The rules are the same as before. The game is played on an NxN grid. Each player will place a specified collection of ships: The ships will vary in length (size) from 2 to 5; There can be any number or any size ship. There may be no ships of a particular size; EXCEPT the battleship – which there will always be 1 and only 1. Player order will be random but...

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