algorithm & data structures lec4&5
TRANSCRIPT
Algorithms & Data Structures CS112Spring 2012
Lecture 4
Syed Muhammad Raza
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
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.
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;
}
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
Binary Search: O(log2 n) Binary search looks for an item in an array/list
using divide and conquer strategy
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
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
Pseudocode for Binary Search
Execution of Binary Search
Execution of Binary Search
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
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
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
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
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
Binary Search
Algorithms & Data Structures CSC-112Fall 2011
Lecture 5
Syed Muhammad Raza
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
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
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
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
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
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
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
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