Report 02(Binary Search)

8
Premier University, Chittagong Department of Computer Science & Engineering Course Code : CSE-226 Course Name : Algorithm Design and Analysis Laboratory Report Number : 02 Topics of Report : To search a Binary Number from an array and its position using Iteractive Approach also by Divide and Conquer Approach. Report Submission Date : 02-February-2016, Tuesday. Submitted By : Md. Bashartullah Student ID : 1202310200480 Section : C-4-A Session : January-2016 Remarks :

Transcript of Report 02(Binary Search)

Page 1: Report 02(Binary Search)

Premier University, Chittagong

Department of Computer Science & Engineering

Course Code : CSE-226

Course Name : Algorithm Design and Analysis Laboratory

Report Number : 02

Topics of Report : To search a Binary Number from an array and its position using Iteractive Approach also by Divide and Conquer Approach.

Report Submission Date : 02-February-2016, Tuesday.

Submitted By : Md. Bashartullah Student ID : 1202310200480

Section : C-4-A

Session : January-2016

Remarks :

Submitted To : Mr. Mohammad Imran ChowdhuryLecturerDepartment of Computer Science &

Engineering

Page 2: Report 02(Binary Search)

Premier University, Chittagong.

Objective: I. To search of a binary number and its’ position from an array

using Iteractive approach and Using Divide and Conquer Approach.

II. Finding the way of implementing the best use of the algorithm to solve the problem.

III. Analyzing the data and outputs and finding a best possible way to solve problems.

Problem Description: We will try to solve the problem in two ways. First of all, we will

follow the Iteractive Approach and than we will follow the Divide and Conquer Approach.

We will run the code with the following data: [1] [2] [3] [4] [5]15 25 35 45 55

Figure 1: Binary Search Tree

Page 3: Report 02(Binary Search)

Algorithm:

Algorithm BinSearch (a,n,x){low:= 1; high:=n;while(low<=high) do{mid:= [(low+high)/2];if(x<a[mid])then high:=mid-1;else if(x>a[mid])then low:=mid+1;elsereturn mid;}return 0;}

Algorithm BinSearch (a, i, l, x){if(l==i) then{if(x==a[i]) then return I;else return 0;}

else{mid:= [(i+l)/2];if (x==a[mid]) then return mid;else if (x<a[mid]) thenreturn BinSearch (a, I, mid-1, x);else return BinSearch (a, mid+1, l, x);}}

Result Analysis:

Data Table:Input: Enter the elements of the array5

Iteractive Approach

Divide And Conquer Approach

Page 4: Report 02(Binary Search)

Enter 5 integers1525354555

Output:Enter value to find out5555 found at the location of 5

Page 5: Report 02(Binary Search)

Discussion: We run the algorithm of Binary Search in two different approaches like

Iteractive Approach and Divide And Conquer Approach. Complexity Analysis:

Successful SearchesΘ(1) Θ(log n) Θ(log n)Best Average Worst

Unsuccessful Searches

Θ(log n) Θ(log n) Θ(log n)Best Average Worst

Appendix:

Page 6: Report 02(Binary Search)

Source Code for Iteractive Approach: #include<stdio.h>int main(){ int array[100], c, l, i, middle, n, search; printf("Enter the elements of the array\n "); scanf("%d", &n); printf("Enter %d integers\n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); printf("Enter value to find out\n"); scanf("%d", &search); l = 0; i = n - 1; middle = (l + i) / 2; while(l <= i) { if(array[middle] < search) l = middle + 1; else if(array[middle] = search) { printf("%d found at the location of %d", search, middle + 1); break; } else i = middle - 1; middle =(l + i) / 2;}if(l > i) printf("Not Found! %d is not exists in the list you entered\n", search);return 0;}

Source Code for Divide And Conquer Approach: #include<stdio.h>

Page 7: Report 02(Binary Search)

int binary_search(int *a, int n, int x);int main(){ int n, a[30], search, i, j, middle, high, low; printf("Enter how many elements you want:\n"); scanf("%d", &n); printf("Enter the %d elements:\n", n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } printf("\nEnter the value to search\n"); scanf("%d", &search); low = 0; high = n; do { middle = (low + high) / 2; if (search < a[middle]) high = middle - 1; else if (search > a[middle]) low = middle + 1; } while (search != a[middle] && low <= high); if (search == a[middle]) { printf("Yes! Searching successful!!\n"); printf("\n %d found in position: %d\n", search, middle + 1); } else { printf("\n Oops! Searching Unsuccessful\n %d is not found in the list you gave us\n", search); } return 0;}