C++, use classes and methods not functions Required Classes and Methods NOT func
ID: 3862086 • Letter: C
Question
C++, use classes and methods not functions
Required Classes and Methods NOT functions.
Implement Inheritance, Getters, Setters, and other OOP tools as needed.
Create 2 int arrays with values between 1-1000 of sizes Array1[1000] and Array2[100000]
Create methods to fill the array with random integers
Create methods that will perform a linear search on the arrays for a key value entered by the user.
Create and implement a Bubble Sort Algorithm method that can be called to sort the given arrays.
Create and implement a Insertion Sort Algorithm method that can be called to sort the given arrays.
Create and implement a Recursive Quicksort Algorithm method that can be called to sort the given arrays.
Create a method to perform a binary search on a key value entered by the user.
Execute each method to demonstrate the methods and source code work.
Execute the Linear search methods on both arrays.
Execute the bubble sort, then binary search. (Make sure to randomize the array before the next step.)
Execute the Insertion sort, then binary search. (Make sure to randomize the array before the next step.)
Execute the Recursive Quicksort, then binary search. (Make sure to randomize the array before the next step.)
Attach Snipping Photos of source code and output below.
Explanation / Answer
#include <iostream>
#include <stdlib.h>
using namespace std;
class BinarySort{
public:
void bubble_sort (int arr[], int n)
{
for (int i = 0; i < n; ++i)
for (int j = 0; j < n - i - 1; ++j)
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
};
class InsertValues{
public :
void insertValuesInArray(int arr[], int len){
int i=0;
for( i=0;i<len;i++){
arr[i] = (rand()%len)+1;
}
}
};
class QuickSort{
public:
void quickSort(int arr[], int left, int right) {
int index = partition(arr, left, right);
if (left < index - 1)
quickSort(arr, left, index - 1);
if (index < right)
quickSort(arr, index, right);
}
int partition(int arr[], int left, int right)
{
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
}
return i;
}
};
class LinearSearch{
public:
int linearSearch(int arr[], int size, int keyToFind){
int i=0;
for(i=0;i<size;i++){
if(arr[i]==keyToFind)
{
return 1;
}
}
return 0;
}
};
class Binarysearch{
public:
int BinarysearchSort(int arr[], int size, int searchNum)
{
int f,l,m;
f=0;
l=size-1;
while(f<=l)
{
m=(f+l)/2;
if(searchNum==arr[m])
return(m);
else
if(searchNum>arr[m])
f=m+1;
else
l=m-1;
}
return -1;
}
};
class InsertionSort{
public :
void insertion_sort (int arr[], int length){
int j, temp;
for (int i = 0; i < length; i++){
j = i;
while (j > 0 && arr[j] < arr[j-1]){
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
j--;
}
}
}
};
int main()
{
BinarySort b;
InsertValues i;
QuickSort q;
LinearSearch l;
Binarysearch bs;
InsertionSort in;
int arr1[1000];
int arr2[100000];
int keyTofind;
i.insertValuesInArray(arr1,1000); // to insert random values in arr1
i.insertValuesInArray(arr2,100000); // to insert random values in arr2
cout<<"Enter any key to find ";
cin>>keyTofind;
if(l.linearSearch(arr1,1000,keyTofind)==1)
cout<<"Element found in First array"<<endl;
else
cout<<"Element is not present in first array"<<endl;
if(l.linearSearch(arr2,100000,keyTofind)==1)
cout<<"Element found in Second array"<<endl;
else
cout<<"Element is not present in Second array"<<endl;
b.bubble_sort(arr1,1000); // sort the arr1 using bubble sort
b.bubble_sort(arr2,100000); // sort the arr2 using bubble sort
cout<<"Enter any key to find ";
cin>>keyTofind;
int n =bs.BinarysearchSort(arr1,1000,keyTofind); //searching the user provided value in arr1 using BinarySearch
if(n==-1)
cout<<"Element is not present in First array"<<endl;
else
cout<<"Element is present in First array at index "<<n<<endl;
n=bs.BinarysearchSort(arr2,100000,keyTofind); //searching the user provided value in arr2 using BinarySearch
if(n==-1)
cout<<"Element is not present in Second array"<<endl;
else
cout<<"Element is present in Second array at index "<<n<<endl;
i.insertValuesInArray(arr1,1000); // to insert random values in arr1
i.insertValuesInArray(arr2,100000); // to insert random values in arr2
in.insertion_sort(arr1,1000); //sort the values of arr1 using insertion sort
in.insertion_sort(arr2,100000); //sort the values of arr2 using insertion sort
cout<<"Enter any key to find ";
cin>>keyTofind;
n = bs.BinarysearchSort(arr1,1000,keyTofind); //searching the user provided value in arr1 using BinarySearch
if(n==-1)
cout<<"Element is not present in First array"<<endl;
else
cout<<"Element is present in First array at index "<<n<<endl;
n = bs.BinarysearchSort(arr2,100000,keyTofind); //searching the user provided value in arr2 using BinarySearch
if(n==-1)
cout<<"Element is not present in Second array"<<endl;
else
cout<<"Element is present in Second array at index "<<n<<endl;
i.insertValuesInArray(arr1,1000); // to insert random values in arr1
i.insertValuesInArray(arr2,100000); // to insert random values in arr2
q.quickSort(arr1,0,1000); //sort the values of arr1 using Quick sort
q.quickSort(arr2,0,100000); //sort the values of arr2 using Quick sort
cout<<"Enter any key to find ";
cin>>keyTofind;
n = bs.BinarysearchSort(arr1,1000,keyTofind); //searching the user provided value in arr1 using BinarySearch
if(n==-1)
cout<<"Element is not present in first array"<<endl;
else
cout<<"Element is present in first array at index "<<n<<endl;
n = bs.BinarysearchSort(arr2,100000,keyTofind); //searching the user provided value in arr2 using BinarySearch
if(n==-1)
cout<<"Element is not present in Second array"<<endl;
else
cout<<"Element is present in Second array at index "<<n<<endl;
}
please find the anwser
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.