USING C++ LANGUAGE ONLY, PLEASE... THANKS A LOT IN ADVANCE ) Required Classes an
ID: 3729621 • Letter: U
Question
USING C++ LANGUAGE ONLY, PLEASE... THANKS A LOT IN ADVANCE ) Required Classes and Methods NOT functions. Implement Inheritance, Getters, Setters, and other OOP tools as needed. Creake 2 int arrays. Array1[1000] and Array2[10000 Create methods to filith 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.)Explanation / Answer
#include<iostream>
#include <stdlib.h>//for rand and srand
#include <time.h>//for time()
class A
{
public:
int Array1[1000],Array2[100000];
void fill()
{
srand(time(0));
for(int i=0;i<1000;i++)
{
Array1[i]=(rand() + 0) % (32760 + 1);
Array2[i]=(rand() + 0) % (32760 + 1);
}
for(long i=1000;i<100000;i++)
{
Array2[i]=(rand() + 0) % (32760 + 1);
}
}
void print()
{
int size=1000;
printf(" Array-1 of %d Elements ........................................................................................................................ ",size);
for(int i=0;i<size;i++)
{
printf("%d ",Array1[i]);
}
size=100000;
printf(" Array-2 of %d Elements ........................................................................................................................ ",size);
for(int i=0;i<size;i++)
{
printf("%d ",Array2[i]);
}
}
///--------------------------------------------------all search----------------------------------------------------------------------------------
long linear_search(int Array[],long size,int key)
{
for(long i=0;i<size;i++)
{
if(key==Array[i])
{
return i;
}
}
return -1;
}
long binary_search(int Array[], long l, long r, int key)
{
if (r >= l)
{
int mid = l + (r - l)/2;
if (Array[mid] == key)
{
return mid;
}
if (Array[mid] > key)
{
return binary_search(Array, l, mid-1, key);
}
return binary_search(Array, mid+1, r, key);
}
return -1;
}
//-----------------------------------------------------------------all sort----------------------------------------------------------------------------------------
void bubble_sort()
{
int temp;
//for Array1
for (long i = 0; i < 999; i++)
{
for (long j = 0; j < 1000-i-1; j++)
{
if (Array1[j] > Array1[j+1])
{
temp=Array1[j];
Array1[j]=Array1[j+1];
Array1[j+1]=temp;
//swap(&arr[j], &arr[j+1]);
}
}
}
//for Array2
for (long i = 0; i < 99999; i++)
{
for (long j = 0; j < 100000-i-1; j++)
{
if (Array2[j] > Array2[j+1])
{
temp=Array2[j];
Array2[j]=Array2[j+1];
Array2[j+1]=temp;
//swap(&arr[j], &arr[j+1]);
}
}
}
}
void insertion_sort()
{
long i, j;
long insert;
//for Array-1
for (i = 1;i<1000;i++)
{
insert = Array1[i];
j = i-1;
while (j >= 0 && Array1[j]>insert)
{
Array1[j+1] = Array1[j];
j = j-1;
}
Array1[j+1] = insert;
}
//for Array-2
for (i = 1;i<100000;i++)
{
insert = Array2[i];
j = i-1;
while (j >= 0 && Array2[j]>insert)
{
Array2[j+1] = Array2[j];
j = j-1;
}
Array2[j+1] = insert;
}
}
void recursive_quick_sort()
{
}
};
int main()
{
A obj;
long index=-1;
int key;
//------------------------------------------------------
obj.fill();
obj.print();
//------------------------------------------------------
//------------------------------------------------------
printf(" Enter the Key to search in Array-1:");
scanf("%d",&key);
if((index=obj.linear_search(obj.Array1,1000,key))!=-1)
{
printf("%d is at %ld index in Array-1",key,index);
}
else
{
printf(" %d Does not found in Array-1 ");
}
printf(" Enter the Key to search in Array-2:");
scanf("%d",&key);
if((index=obj.linear_search(obj.Array2,100000,key))!=-1)
{
printf("%d is at %ld index in Array-2",key,index);
}
else
{
printf(" %d Does not found in Array-2 ");
}
//-----------------------------------------------------
//-----------------------------------------------------
obj.bubble_sort();
obj.print();
//-----------------------------------------------------
//-----------------------------------------------------
printf(" Enter the Key to search in Array-1:");
scanf("%d",&key);
if((index=obj.binary_search(obj.Array1,0,1000,key))==-1)
{
printf(" %d not found ",key);
}
else
{
printf(" %d found at %ld Index in Array-1. ",key,index);
}
printf(" Enter the Key to search in Array-2:");
scanf("%d",&key);
if((index=obj.binary_search(obj.Array2,0,1000,key))==-1)
{
printf(" %d not found ",key);
}
else
{
printf(" %d found at %ld Index in Array-2. ",key,index);
}
//-----------------------------------------------------
//-----------------------------------------------------
obj.insertion_sort();
obj.print();
//-----------------------------------------------------
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.