Data Structure
Data Structure
Data Structure
#include <iostream>
using namespace std;
void input_array_from_user(int arrsize, int array[])
{
for(int i=0; i< arrsize; i++){
cout<< "Input the "<<i+1<<" 's value: ";
cin>>array[i];
}
}
int search(int arr[],
int n, int x)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == x)
return i;
return -1;
}
int binarySearch(int arr[], int l,
int r, int x)
{
if (r >= l)
{
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l,
mid - 1, x);
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
swap(&arr[min_idx], &arr[i]);
}
}
int main(void)
{
int arrsize;
cout<<"\tMenu driven program that implements linear search, binary search,\n\
t bubble sort, insertion, selection sort and display outputs \n\n\n ";
cout<< "The input size: ";
cin >> arrsize;
int arr[arrsize];
int n = sizeof(arr) / sizeof(arr[0]);
int find;
int choice;
int result;
while(1){
cout
<<"\n\t\t\t****Choose the Options below to interact "
<<"with the program****\n"
<<"\t\t\t\t 1) linear search\n"
<<"\t\t\t\t 2) binary search\n"
<<"\t\t\t\t 3) bubble sort\n"
<<"\t\t\t\t 4) insertion sort\n"
<<"\t\t\t\t 5) selection sort\n"
<<"\t\t\t\t 6) Exit"<< "\n\n\n Your choice: ";
cin>> choice;
switch(choice)
{
case 1:
input_array_from_user(arrsize, arr);
cout<< "What is the value you want to search: ";
cin>>find;
case 2:
input_array_from_user(arrsize, arr);
cout<< "What is the value you want to search: ";
cin>>find;
result = binarySearch(arr, 0,
n - 1, find);
(result == -1) ? cout <<
"Element is not present in array\n" :
cout << "Element is present at index " <<
result<<endl;
break;
case 3:
input_array_from_user(arrsize, arr);
bubbleSort(arr,n);
cout << "Sorted array: \n";
printArray(arr, n);
break;
case 4:
input_array_from_user(arrsize, arr);
insertionSort(arr, n);
printArray(arr, n);
break;
case 5:
input_array_from_user(arrsize, arr);
selectionSort(arr, n);
cout << "Sorted array: ";
printArray(arr, n);
break;
case 6:
return 0;
default:
cout<<" \t\t\t\t Invalid input!!! please try again!\n";
}
}
return 0;
}