Click here to load reader
Upload
dharmendra-prasad
View
1.879
Download
1
Embed Size (px)
Citation preview
1
Description: A detailed discussion about algorithms and their measures, and understanding sorting.
Duration: 90 minutesStarts at: Saturday 12th May 2013, 11:00AM
-by Dharmendra Prasad
Data Structures and Algorithms – Sorting Techniques
2
Table of Contents
1. Continuing the Sorting Algorithms.1. Quick Sort (in place sorting algorithm)
2. Searching Algorithms1. Binary search
3. Some real world problem scenarios.1. Substring search
Data Structures and Algorithms – Sorting Techniques
3
Algorithm:
It’s a divide and conquer algorithm. Step1(Divide): Partition a given array into 2 sub arrays
around a pivot ‘x’ such that the elements in lower sub array <= x <= elements in upper sub array.
Step2(Conquer):Recursively sort 2 sub arrays. Step3(Combine):Do nothing as it is in place sorting.
Data Structures and Algorithms – Sorting Techniques
<=x x >=x
4
Partition(A, p, q) //A[p , q]
X ← A[p]i ← p
for j ← p+1 to q do if A[j] <= x
then i ← i+1exchange A[i] ↔ A[j]
exchange A[p] ↔ A[i];return i
Data Structures and Algorithms – Sorting Techniques
x <=x >=xAp qi j
5
Data Structures and Algorithms – Sorting Techniques Example:
6 10 13 5 8 3 2 11
X = 6, i = 0, j = 1
6 5 13 10 8 3 2 116 5 3 10 8 13 2 116 5 3 2 8 13 10 11
Swap pivot with i2 5 3 6 8 13 10 11
6
Algorithm:QuickSort(A, p, q)if p < q
then r <- Partition(A, p, q)QuickSort( A, p, r-1)QuickSort( A, r+1, q)
Initial Call : QuickSort( A, 1, n)
Data Structures and Algorithms – Sorting Techniques
7
Order Statistics:Problem Statement: Given an array of numbers, find the
kth smallest number.Naïve Solution: Sort the array and return the element at
index k.Case1: if k = 1, we are referring to the minimum number in
the array.
Case2: if k = length of the array, we are referring to the maximum number in the array.
Case3: when k lies between 1 and n where n is the length of the array
Data Structures and Algorithms – Sorting Techniques
8
Algorithm:OrderStat(A,p,q,k) // means kth smallest number in A
between index p and qif p==q
return A[p]r <- Partition(A,p,q)i <- r – p + 1;if k == i
return A[r]if k < i
return OrderStat(A,p,r-1,k)else
return OrderStat(A,r+1,q,k-i)
Data Structures and Algorithms – Sorting Techniques
9
Searching:Basic Idea: In an array A[a1,a2,a3,a4,…,an] find the index k
such that p = A[k]Naïve Solution: Traverse through the array in a loop and
compare each element with the given number. If the number matches, return the index of the number else return null.
Algorithm:Search (A[1.. n], p)
for i<- 1 to ndo if A[i] == p
return ireturn null
Data Structures and Algorithms – Sorting Techniques
10
Searching:Basic Idea: In an array A[a1,a2,a3,a4,…,an] find the index k such that p
= A[k]Binary Search Solution: Only if the array is sorted. Divide it into two
halves, check the element at the center, if it is less than what we are searching, look into the upper half else look into the lower half. Repeat till you find the number or the array exhausts.
Algorithm:BinarySearch (A, p, low,high)
middle = (low+high)/2if A[middle] == preturn middle;else if A[middle] > preturn BinarySearch(A, p, low, middle-1)else return BinarySearch(A,p,middle+1,high)
Data Structures and Algorithms – Sorting Techniques
11
Special Case Substring Searching:Basic Idea: In a character string search a substring and return the index of first occurrence.Naive Solution: Start from the first index of both the strings, compare the characters, if
character matches, compare the next character and so on till the substring exhausts. Return the start index of the substring in the main string.
Algorithm:SubStringSearch (S, sb)
j=0;match = false;while i < S.length or j < sb.lengthif S[i] == sb[j]match = truei++, j++elsematch = falsej=0, i++
if match == true and j = sb.lengthreturn i-sb.lengthelsereturn -1
Data Structures and Algorithms – Sorting Techniques
12
Special Case Substring Searching:Basic Idea: In a character string search a substring and
return the index of first occurrence.Better Solution: Boyre Moore algorithm is used to
effectively search substring in a given string.
Data Structures and Algorithms – Sorting Techniques
13
Question &
Answers
Data Structures and Algorithms – Sorting Techniques