Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Data Structure

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

//Meron Bahiru UGR/25880/14

#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);

return binarySearch(arr, mid + 1, r, x);


}
return -1;
}
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1])
swap(arr[j], arr[j + 1]);
}

void printArray(int arr[], int size)


{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}

void insertionSort(int arr[],


int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;

while (j >= 0 && arr[j] > key)


{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

void swap(int *xp, int *yp)


{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

void selectionSort(int arr[], int n)


{
int i, j, min_idx;

for (i = 0; i < n-1; i++)


{

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;

result = search(arr, n, find);


(result == -1) ?
cout << "Element is not present in array\n" :
cout << "Element is present at index " <<
result<<endl;
break;

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;
}

You might also like