Analysis of Binary Search algorithm and Selection Sort algorithm



Similar documents
Zabin Visram Room CS115 CS126 Searching. Binary Search

Binary search algorithm

Computer Science 210: Data Structures. Searching

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

6. Standard Algorithms

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

Loop Invariants and Binary Search

Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Binary Heap Algorithms

Searching Algorithms

Data Structures. Algorithm Performance and Big O Analysis

Chapter 7: Sequential Data Structures

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Lecture 3: Finding integer solutions to systems of linear equations

Binary Heaps. CSE 373 Data Structures

Analysis of a Search Algorithm

Section IV.1: Recursive Algorithms and Recursion Trees

Lecture Notes on Linear Search

Algorithms. Margaret M. Fleck. 18 October 2010

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

CSC 180 H1F Algorithm Runtime Analysis Lecture Notes Fall 2015

Chapter Objectives. Chapter 9. Sequential Search. Search Algorithms. Search Algorithms. Binary Search

Algorithm Analysis [2]: if-else statements, recursive algorithms. COSC 2011, Winter 2004, Section N Instructor: N. Vlajic

4.2 Sorting and Searching

Algorithm Design and Recursion

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Recursion and Dynamic Programming. Biostatistics 615/815 Lecture 5

APP INVENTOR. Test Review

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

Simple sorting algorithms and their complexity. Bubble sort. Complexity of bubble sort. Complexity of bubble sort. Bubble sort of lists

In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

A Randomized Searching Algorithm and its Performance analysis with Binary Search and Linear Search Algorithms

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Chapter 13: Query Processing. Basic Steps in Query Processing

Introduction to Programming (in C++) Sorting. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Quick Sort. Implementation

HY345 Operating Systems

Query Processing C H A P T E R12. Practice Exercises

15. Functions and Lists. Example 2: Sorting a List of Numbers. Example 1: Computing the Diameter of a Cloud of Points

Why Use Binary Trees?

Bounded Cost Algorithms for Multivalued Consensus Using Binary Consensus Instances

CS473 - Algorithms I

HW4: Merge Sort. 1 Assignment Goal. 2 Description of the Merging Problem. Course: ENEE759K/CMSC751 Title:

Pseudo code Tutorial and Exercises Teacher s Version

Binary Multiplication

Class : MAC 286. Data Structure. Research Paper on Sorting Algorithms

Closest Pair of Points. Kleinberg and Tardos Section 5.4

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

Algorithms and Data Structures

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Sample Induction Proofs

What Is Recursion? 5/12/10 1. CMPSC 24: Lecture 13 Recursion. Lecture Plan. Divyakant Agrawal Department of Computer Science UC Santa Barbara

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Biostatistics 615/815

Symbol Tables. Introduction

Persistent Data Structures

(Refer Slide Time: 01.26)

The Running Time of Programs

CS/COE

Converting a Number from Decimal to Binary

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Algorithms and Data Structures Written Exam Proposed SOLUTION

CompSci-61B, Data Structures Final Exam

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

csci 210: Data Structures Recursion

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

recursion here it is in C++ power function cis15 advanced programming techniques, using c++ fall 2007 lecture # VI.1

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

Reminder: Complexity (1) Parallel Complexity Theory. Reminder: Complexity (2) Complexity-new

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

Analysis of Computer Algorithms. Algorithm. Algorithm, Data Structure, Program

6.2 Permutations continued

Parallel Prefix Sum (Scan) with CUDA. Mark Harris

TREE BASIC TERMINOLOGIES

LZ77. Example 2.10: Let T = badadadabaab and assume d max and l max are large. phrase b a d adadab aa b

Sample Questions Csci 1112 A. Bellaachia

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

MAT Program Verication: Exercises

1/1 7/4 2/2 12/7 10/30 12/25

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

CSC2420 Fall 2012: Algorithm Design, Analysis and Theory

Partitioning under the hood in MySQL 5.5

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Mathematical Induction. Lecture 10-11

Data Preprocessing. Week 2

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Transcription:

Analysis of Binary Search algorithm and Selection Sort algorithm In this section we shall take up two representative problems in computer science, work out the algorithms based on the best strategy to solve the problems, and compute the time complexity of the algorithms. The two problems, one related to searching and the other related to data sorting are very common in many real world situations. Binary Search Method: Suppose we are given a number of integers stored in an array A, and we want to locate a specific target integer K in this array. If we do not have any information on how the integers are organized in the array, we have to sequentially examine each element of the array. This is known as linear search and would have a time complexity of O(n ) in the worst case. However, if the elements of the array are ordered, let us say in ascending order, and we wish to find out the position of an integer target K in the array, we need not make a sequential search over the complete array. We can make a faster search using the Binary search method.

The basic idea is to start with an examination of the middle element of the array. This will lead to 3 possible situations: If this matches the target K, then search can terminate successfully, by printing out the index of the element in the array. On the other hand, if K<A[middle], then search can be limited to elements to the left of A[middle]. All elements to the right of middle can be ignored. If it turns out that K >A[middle], then further search is limited to elements to the right of A[middle]. If all elements are exhausted and the target is not found in the array, then the method returns a special value such as 1. Here is one version of the Binary Search function: int BinarySearch (int A[ ], int n, int K) int L=0, Mid, R= n-1; while (L<=R) Mid = (L +R)/2; if ( K= =A[Mid] ) return Mid; else if ( K > A[Mid] ) L = Mid + 1; else R = Mid 1 ; return 1 ; Complexity Analysis: Let us now carry out an Analysis of this method to determine its time complexity. Since there are no for loops, we can not use summations to express the total number of operations. Let us examine the operations for a specific case, where the number of elements in the array n is 64. When n= 64 BinarySearch is called to reduce size to n=32 When n= 32 BinarySearch is called to reduce size to n=16 When n= 16 BinarySearch is called to reduce size to n=8 When n= 8 BinarySearch is called to reduce size to n=4 When n= 4 BinarySearch is called to reduce size to n=2 When n= 2 BinarySearch is called to reduce size to n=1

Thus we see that BinarySearch function is called 6 times ( 6 elements of the array were examined) for n =64. Note that 64 = 2 6 Also we see that the BinarySearch function is called 5 times ( 5 elements of the array were examined) for n = 32. Note that 32 = 2 5 Let us consider a more general case where n is still a power of 2. Let us say n = 2 k. Following the above argument for 64 elements, it is easily seen that after k searches, the while loop is executed k times and n reduces to size 1. Let us assume that each run of the while loop involves at most 5 operations. Thus total number of operations: 5k. The value of k can be determined from the expression 2 k = n Taking log of both sides k = log n Thus total number of operations = 5 log n. We conclude from there that the time complexity of the Binary search method is O(log n), which is much more efficient than the Linear Search method. We shall study a recursive version of the Binary search method later when we study recursion.

Selection sort Method: Suppose we are given a number of integers in an array A, and we want to sort the integers in increasing order. A number of methods are available to carry out sorting. Here we present the Selection sort method. As the name suggests, the method selects the smallest element in the array and puts it in proper place in the array. To start with, it would be the first position in the array. Once the first element has been positioned, this process is repeated by considering the remaining elements of the array. Consider the array [56 30 37 58 19 95 73 25] The first pass locates the smallest element as 19 and swaps it with the first element 56 to result in the array [19 30 37 58 56 95 73 25] The process is repeated to find the next smallest from remaining elements which is 25. It is swapped with the 2 nd element to result in the array [19 25 37 58 56 95 73 30] The process is now repeated for remaining elements of the array, till all elements have been put in proper places. Note that for this array of size 8, the process is to be executed 7 times. When 7 elements have been placed in proper position, the 8 th element will be automatically in its proper place, as it would be largest element. Here is the selection sort algorithm: for i = 1 to n 1 minposition = i; for j = i+1 to n if A[j] < A[minPosition] minposition = j; swap A[i] with A[minPosition];

Complexity Analysis: We observe that the outer for loop variable i runs from 1 to n, while the inner for loop j runs from i+1 to n. One operation is being performed inside the innermost loop. Let us assume that swap involves just one operation. Note that for every run of the outer loop, there is one swap operation. In terms of summations the total number of operations can be expressed as n-1 n Σ [ Σ 1 + 1 ] i =1 j =i+1 The second summation can be split into two summations each starting from 1. n-1 n i = Σ [ Σ 1 Σ 1 ] + 1 i =1 j =1 j =1 n-1 = Σ [ n i ] + 1 i =1 n-1 n-1 = Σ n+1 Σ i i =1 i =1 = (n+1)(n 1) (n 1)n /2 = n 2 1 n 2 /2 + n/2 = n 2 /2 + n/2 1 Thus the complexity of selection sort algorithm is O(n 2 ).