Please I need help with this in C Please attach with a succe
Please I need help with this... in C++
Please attach with a successful printscreen.
***********************************************************************************************
1. What are the top 2 sorting and searching algorithms ?
Explain why so...
2. Write a OOP program that store an array of 100 integers.
Add/code 1 of the top 2 sorting algorithms you mentioned above
Add/code 1 of the top 2 searching algorithms you mentioned above
Have a function to ask the user for input.
Have a function to print out the array
Have a function to delete an integer
Have the ability to know/printout the count the number of integers
Solution
1.The top searching algorithm is binary search algorithm .Its time complexity is O(logn).
The top sorting algorithm is merge sort and quick sort.Both having time complexity is O(nlogn).
The worst complexity of quick sort is O(n^2).Merge sort is better than quick sort.
2.Following is the c++ program having class TopSearchSort having functions as per given problem statement.
#include <iostream>
using namespace std;
class TopSearchSort
{
int arr[100];
int n;
public:
//Function return number of elements in array
int getn()
{
return n;
}
void input()
{
//int n;
cout<<\"Enter the number of elements :\";
cin>>n;
cout<<\"\ Enter the elements:\ \";
for(int i=0;i<n;i++)
cin>>arr[i];
}
void output()
{
cout<<\"The array elements are : \";
for(int i=0;i<n;i++)
cout<<arr[i]<<\" \";
}
void delete_element()
{
int d,start=0;
cout<<\"Enter the number to delete : \";
cin>>d;
for(int i=0;i<n;i++)
{
if(arr[i]==d)
{
start=i;
break;
}
}
for(int i=start;i<n-1;i++)
arr[i]=arr[i+1];
n=n-1;
}
/* Function to merge the two haves arr[l..m] and arr[m+1..r] of array arr[] */
// void merge(int arr[], int l, int m, int r)
void merge(int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
/* create temp arrays */
int L[n1], R[n2];
/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays back into arr[l..r]*/
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy the remaining elements of L[], if there are any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy the remaining elements of R[], if there are any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort( int l, int r)
{
if (l < r)
{
int m = l+(r-l)/2; //Same as (l+r)/2 but avoids overflow for large l & h
mergeSort(l, m);
mergeSort(m+1, r);
// merge(arr, l, m, r);
merge(l,m,r);
}
}
void search()
{
int s;
cout<<\"Enter the number to search :\";
cin>>s;
mergeSort(0,getn()-1);
int start=0;
int end=n;
int mid=-1;
while(start<=end)
{
mid=(start+end)/2;
if(arr[mid]==s)
break;
if(arr[mid]<s)
{
start=mid+1;
}
else
end=mid-1;
}
cout<<\"The element is present at location (-1 means element is not present) :\"<<mid<<\"\ \";
}
};
int main()
{
TopSearchSort s;
while(1)
{
cout<<\"\ Enter your choice 1.Enter elements 2.Print array 3.Delete element 4.Search Element 5.Sort array\ \";
int c;
cin>>c;
if(c==1)
s.input();
else if(c==2)
s.output();
else if(c==3)
s.delete_element();
else if(c==4)
s.search();
else if(c==5)
s.mergeSort(0,s.getn()-1);
else
{cout<<\"wrong choice\ \";
break;
}
}
return 0;
}
Output :
$: g++ Topsearch.cpp
$: ./a.out
Enter your choice 1.Enter elements 2.Print array 3.Delete element 4.Search Element 5.Sort array
1
Enter the number of elements :7
Enter the elements:
432 23 1 5 16 3 7
Enter your choice 1.Enter elements 2.Print array 3.Delete element 4.Search Element 5.Sort array
5
Enter your choice 1.Enter elements 2.Print array 3.Delete element 4.Search Element 5.Sort array
2
The array elements are : 1 3 5 7 16 23 432
Enter your choice 1.Enter elements 2.Print array 3.Delete element 4.Search Element 5.Sort array
4
Enter the number to search :16
The element is present at location (-1 means element is not present) :4
Enter your choice 1.Enter elements 2.Print array 3.Delete element 4.Search Element 5.Sort array
8
wrong choice
$:



