26
Algorithms & Data Structures CS112 Spring 2012 Lecture 4 Syed Muhammad Raza

Algorithm & data structures lec4&5

Embed Size (px)

Citation preview

Page 1: Algorithm & data structures lec4&5

Algorithms & Data Structures CS112Spring 2012

Lecture 4

Syed Muhammad Raza

Page 2: Algorithm & data structures lec4&5

Searching Algorithms

Searching is the process of determining whether or not a given value

exists in a data structure or a storage media.

We will study two searching algorithms

Linear Search

Binary Search

Page 3: Algorithm & data structures lec4&5

Linear Search: O(n)

The linear (or sequential) search algorithm on an array is:

Start from beginning of an array/list and continues until the item is

found or the entire array/list has been searched.

Sequentially scan the array, comparing each array item with the

searched value.

If a match is found; return the index of the matched element;

otherwise return –1.

Note: linear search can be applied to both sorted and unsorted

arrays.

Page 4: Algorithm & data structures lec4&5

Linear Search bool LinSearch(double x[ ], int n, double item)

{

for(int i=0;i<n;i++)

{

if(x[i]==item)

{

return true;

}

else

{

return false;

}

}

return false;

}

Page 5: Algorithm & data structures lec4&5

Linear Search Tradeoffs Benefits

Easy to understand Array can be in any order

Disadvantages Inefficient for array of N elements

Examines N/2 elements on average for value in array, N elements for value not in array

Page 6: Algorithm & data structures lec4&5

Binary Search: O(log2 n) Binary search looks for an item in an array/list

using divide and conquer strategy

Page 7: Algorithm & data structures lec4&5

Binary Search

Binary search algorithm assumes that the items in the array being

searched is sorted

The algorithm begins at the middle of the array in a binary

search

If the item for which we are searching is less than the item in the

middle, we know that the item won’t be in the second half of the

array

Once again we examine the “middle” element

The process continues with each comparison cutting in half the

portion of the array where the item might be

Page 8: Algorithm & data structures lec4&5

Binary Search

Binary search uses a recursive method to search an array to find a

specified value

The array must be a sorted array:

a[0]≤a[1]≤a[2]≤. . . ≤ a[finalIndex]

If the value is found, its index is returned

If the value is not found, -1 is returned

Note: Each execution of the recursive method reduces the search

space by about a half

Page 9: Algorithm & data structures lec4&5

Pseudocode for Binary Search

Page 10: Algorithm & data structures lec4&5

Execution of Binary Search

Page 11: Algorithm & data structures lec4&5

Execution of Binary Search

Page 12: Algorithm & data structures lec4&5

Key Points in Binary Search

1. There is no infinite recursion

• On each recursive call, the value of first is increased, or the value

of last is decreased

• If the chain of recursive calls does not end in some other way, then

eventually the method will be called with first larger than last

2. Each stopping case performs the correct action for that case

• If first > last, there are no array elements between a[first] and

a[last], so key is not in this segment of the array, and result is

correctly set to -1

• If key == a[mid], result is correctly set to mid

Page 13: Algorithm & data structures lec4&5

Key Points in Binary Search

3. For each of the cases that involve recursion, if all recursive calls

perform their actions correctly, then the entire case performs

correctly

• If key < a[mid], then key must be one of the elements a[first]

through a[mid-1], or it is not in the array

• The method should then search only those elements, which it

does

• The recursive call is correct, therefore the entire action is

correct

Page 14: Algorithm & data structures lec4&5

Key Points in Binary Search

• If key > a[mid], then key must be one of the elements

a[mid+1] through a[last], or it is not in the array

• The method should then search only those elements, which it

does

• The recursive call is correct, therefore the entire action is

correct

The method search passes all three tests:

Therefore, it is a good recursive method definition

Page 15: Algorithm & data structures lec4&5

Efficiency of Binary Search

The binary search algorithm is extremely fast compared to an

algorithm that tries all array elements in order

About half the array is eliminated from consideration right at the

start

Then a quarter of the array, then an eighth of the array, and so

forth

Page 16: Algorithm & data structures lec4&5

Efficiency of Binary Search

Given an array with 1,000 elements, the binary search will only need

to compare about 10 array elements to the key value, as compared

to an average of 500 for a serial search algorithm

The binary search algorithm has a worst-case running time that is

logarithmic: O(log n)

A serial search algorithm is linear: O(n)

If desired, the recursive version of the method search can be

converted to an iterative version that will run more efficiently

Page 17: Algorithm & data structures lec4&5

Binary Search

Page 18: Algorithm & data structures lec4&5

Algorithms & Data Structures CSC-112Fall 2011

Lecture 5

Syed Muhammad Raza

Page 19: Algorithm & data structures lec4&5

Sorting Algorithms

Sorting is the process of rearranging your data elements/Item in

ascending or descending order

Unsorted Data

Sorted Data (Ascending)

Sorted Data (Descending)

4 3 2 7 1 6 5 8 9

1 2 3 4 5 6 7 8 9

9 8 7 6 5 4 3 2 1

Page 20: Algorithm & data structures lec4&5

Sorting Algorithms They are many

Bubble Sort Selection Sort Insertion Sort Shell sort Comb Sort Merge Sort Heap Sort Quick Sort Counting Sort Bucket Sort Radix Sort Distribution Sort Time Sort

Source: Wikipedia

Page 21: Algorithm & data structures lec4&5

Bubble Sort

Compares Adjacent Items and Exchanges Them if They are Out of

Order

When You Order Successive Pairs of Elements, the Largest Element

Bubbles to the Top(end) of the Array

Bubble Sort (Usually) Requires Several Passes Through the Array

Page 22: Algorithm & data structures lec4&5

Bubble Sort

29 10 14 37 13

10 29 14 37 13

10 14 29 37 13

10 14 29 37 13

10 14 29 13 37

Pass 1

10 14 29 13 37

10 14 29 13 37

10 14 13 29 37

10 14 29 13 37

Pass 2

Page 23: Algorithm & data structures lec4&5

Selection Sort To Sort an Array into Ascending Order, First Search for the Largest Element

Because You Want the Largest Element to be in the Last Position of the Array, You

Swap the Last Item With the Largest Item to be in the Last Position of the Array, You

Swap the Last Item with the Largest Item, Even if These Items Appear to be

Identical

Now, Ignoring the Last (Largest) Item of the Array, search Rest of the Array For Its

Largest Item and Swap it With Its Last Item, Which is the Next-to-Last Item in the

original Array

You Continue Until You Have Selected and Swapped N-1 of the N Items in the Array

The Remaining Item, Which is Now in the First Position of the Array, is in its Proper

Order

Page 24: Algorithm & data structures lec4&5

Selection Sort

29 10 14 37 13

29 10 14 13 37

13 10 14 29 37

13 10 14 29 37

10 13 14 29 37

Initial Array

After4th Swap

After1st Swap

After2nd Swap

After3rd Swap

Page 25: Algorithm & data structures lec4&5

Insertion Sort

Divide Array Into Sorted Section At Front (Initially Empty), Unsorted

Section At End

Step Iteratively Through Array, Moving Each Element To Proper

Place In Sorted Section

Sorted Unsorted

N-10After i Iterations

….. …..

i

Page 26: Algorithm & data structures lec4&5

Insertion Sort

29 29 14 37 13

10 29 29 37 13

10 14 29 37 13

10 14 29 37 13

10 14 14 29 37

Initial Array

Sorted Array

Copy 10

10 29 14 37 13

29 10 14 37 13

10 13 14 29 37

Shift 29

Insert 10, Copy 14

Shift 29

Insert 14; Copy 37

Insert 37 on Itself

Copy 13

Shift 14, 29, 37

Insert 13