SlideShare a Scribd company logo
Description: A detailed discussion about algorithms and their
measures, and understanding sorting.
Duration: 90 minutes
Starts at: Saturday 12th May 2013, 11:00AM
-by Dharmendra Prasad
1
Table of Contents
1. Continuing the Sorting Algorithms.
1. Quick Sort (in place sorting algorithm)
2. Searching Algorithms
1. Binary search
3. Some real world problem scenarios.
1. Substring search
2
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.
3
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+1
exchange A[i] ↔ A[j]
exchange A[p] ↔ A[i];
return i
4
A
p qi j
5
 Example:
6 10 13 5 8 3 2 11
X = 6, i = 0, j = 1
6 5 13 10 8 3 2 11
6 5 3 10 8 13 2 11
6 5 3 2 8 13 10 11
Swap pivot with i
2 5 3 6 8 13 10 11
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)
6
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
7
Algorithm:
OrderStat(A,p,q,k) // means kth smallest number in A between
index p and q
if 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)
8
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 n
do if A[i] == p
return i
return null
9
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)/2
if A[middle] == p
return middle;
else if A[middle] > p
return BinarySearch(A, p, low, middle-1)
else
return BinarySearch(A,p,middle+1,high)
10
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.length
if S[i] == sb[j]
match = true
i++, j++
else
match = false
j=0, i++
if match == true and j = sb.length
return i-sb.length
else
return -1
11
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.
12
Question
&
Answers
13

More Related Content

What's hot (20)

Sorting
SortingSorting
Sorting
Ghaffar Khan
 
Chapter 14 Searching and Sorting
Chapter 14 Searching and SortingChapter 14 Searching and Sorting
Chapter 14 Searching and Sorting
MuhammadBakri13
 
Sorting ppt
Sorting pptSorting ppt
Sorting ppt
Hassan Mustafa
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
Dorina Isaj
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
Rahul Jamwal
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Mohammed Hussein
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
university of Gujrat, pakistan
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1
Kumar
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting
Fadhil Ismail
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
Hossain Md Shakhawat
 
Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sorting
ryokollll
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
Trupti Agrawal
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Pranay Neema
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
Lovely Professional University
 
Data Structures- Part4 basic sorting algorithms
Data Structures- Part4 basic sorting algorithmsData Structures- Part4 basic sorting algorithms
Data Structures- Part4 basic sorting algorithms
Abdullah Al-hazmy
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
Abdul Khan
 
Binary search
Binary search Binary search
Binary search
Raghu nath
 
Quicksort
QuicksortQuicksort
Quicksort
Gayathri Gaayu
 
Chapter 14 Searching and Sorting
Chapter 14 Searching and SortingChapter 14 Searching and Sorting
Chapter 14 Searching and Sorting
MuhammadBakri13
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
Rahul Jamwal
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1
Kumar
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting
Fadhil Ismail
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
Hossain Md Shakhawat
 
Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sorting
ryokollll
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Pranay Neema
 
Data Structures- Part4 basic sorting algorithms
Data Structures- Part4 basic sorting algorithmsData Structures- Part4 basic sorting algorithms
Data Structures- Part4 basic sorting algorithms
Abdullah Al-hazmy
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
Abdul Khan
 
Binary search
Binary search Binary search
Binary search
Raghu nath
 

Viewers also liked (10)

Lecture 2c stacks
Lecture 2c stacksLecture 2c stacks
Lecture 2c stacks
Victor Palmar
 
Lecture 2 data structures & algorithms - sorting techniques
Lecture 2  data structures & algorithms - sorting techniquesLecture 2  data structures & algorithms - sorting techniques
Lecture 2 data structures & algorithms - sorting techniques
Dharmendra Prasad
 
Arrays
ArraysArrays
Arrays
Trupti Agrawal
 
C# Arrays
C# ArraysC# Arrays
C# Arrays
Hock Leng PUAH
 
Arrays C#
Arrays C#Arrays C#
Arrays C#
Raghuveer Guthikonda
 
Lecture 2a arrays
Lecture 2a arraysLecture 2a arrays
Lecture 2a arrays
Victor Palmar
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
somendra kumar
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
eShikshak
 
Arrays
ArraysArrays
Arrays
archikabhatia
 
Array in c language
Array in c languageArray in c language
Array in c language
home
 
Ad

Similar to Lecture 3 data structures & algorithms - sorting techniques - http://techieme.in (20)

DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
RUHULAMINHAZARIKA
 
lecture 10
lecture 10lecture 10
lecture 10
sajinsc
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
Monika Choudhery
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
Nv Thejaswini
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
B.Kirron Reddi
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
Shiwani Gupta
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
ramya marichamy
 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
pinakspatel
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
infanciaj
 
21-algorithms (1).ppt
21-algorithms (1).ppt21-algorithms (1).ppt
21-algorithms (1).ppt
DaniloMislosAlbay
 
21-algorithms.ppt
21-algorithms.ppt21-algorithms.ppt
21-algorithms.ppt
ashwinraiyani1
 
Algorithm, Pseudocode and Flowcharting in C++
Algorithm, Pseudocode and Flowcharting in C++Algorithm, Pseudocode and Flowcharting in C++
Algorithm, Pseudocode and Flowcharting in C++
Johnny Jean Tigas
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
maamir farooq
 
Counting Sort Lowerbound
Counting Sort LowerboundCounting Sort Lowerbound
Counting Sort Lowerbound
despicable me
 
21-algorithms.ppt
21-algorithms.ppt21-algorithms.ppt
21-algorithms.ppt
Madurai Kamaraj University Madurai Tamil Nadu India
 
Lect-2.pptx
Lect-2.pptxLect-2.pptx
Lect-2.pptx
mrizwan38
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptx
DeepakM509554
 
lecture 11
lecture 11lecture 11
lecture 11
sajinsc
 
Unit6 C
Unit6 C Unit6 C
Unit6 C
arnold 7490
 
Sorting2
Sorting2Sorting2
Sorting2
Saurabh Mishra
 
Ad

Recently uploaded (20)

Types of Actions in Odoo 18 - Odoo Slides
Types of Actions in Odoo 18 - Odoo SlidesTypes of Actions in Odoo 18 - Odoo Slides
Types of Actions in Odoo 18 - Odoo Slides
Celine George
 
LDMMIA Free Reiki Yoga S7 Weekly Workshops
LDMMIA Free Reiki Yoga S7 Weekly WorkshopsLDMMIA Free Reiki Yoga S7 Weekly Workshops
LDMMIA Free Reiki Yoga S7 Weekly Workshops
LDM & Mia eStudios
 
Pragya Champion's Chalice 2025 Set , General Quiz
Pragya Champion's Chalice 2025 Set , General QuizPragya Champion's Chalice 2025 Set , General Quiz
Pragya Champion's Chalice 2025 Set , General Quiz
Pragya - UEM Kolkata Quiz Club
 
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
RVSPSOA
 
K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910
PankajRodey1
 
STUDENT LOAN TRUST FUND DEFAULTERS GHANA
STUDENT LOAN TRUST FUND DEFAULTERS GHANASTUDENT LOAN TRUST FUND DEFAULTERS GHANA
STUDENT LOAN TRUST FUND DEFAULTERS GHANA
Kweku Zurek
 
"Hymenoptera: A Diverse and Fascinating Order".pptx
"Hymenoptera: A Diverse and Fascinating Order".pptx"Hymenoptera: A Diverse and Fascinating Order".pptx
"Hymenoptera: A Diverse and Fascinating Order".pptx
Arshad Shaikh
 
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATIONTHE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
PROF. PAUL ALLIEU KAMARA
 
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
SweetytamannaMohapat
 
Exploring Identity Through Colombian Companies
Exploring Identity Through Colombian CompaniesExploring Identity Through Colombian Companies
Exploring Identity Through Colombian Companies
OlgaLeonorTorresSnch
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
"Dictyoptera: The Order of Cockroaches and Mantises" Or, more specifically: ...
"Dictyoptera: The Order of Cockroaches and Mantises"  Or, more specifically: ..."Dictyoptera: The Order of Cockroaches and Mantises"  Or, more specifically: ...
"Dictyoptera: The Order of Cockroaches and Mantises" Or, more specifically: ...
Arshad Shaikh
 
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdf
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdfTechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdf
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdf
TechSoup
 
Cloud Computing ..PPT ( Faizan ALTAF )..
Cloud Computing ..PPT ( Faizan ALTAF )..Cloud Computing ..PPT ( Faizan ALTAF )..
Cloud Computing ..PPT ( Faizan ALTAF )..
faizanaltaf231
 
Coleoptera: The Largest Insect Order.pptx
Coleoptera: The Largest Insect Order.pptxColeoptera: The Largest Insect Order.pptx
Coleoptera: The Largest Insect Order.pptx
Arshad Shaikh
 
How to Create Time Off Request in Odoo 18 Time Off
How to Create Time Off Request in Odoo 18 Time OffHow to Create Time Off Request in Odoo 18 Time Off
How to Create Time Off Request in Odoo 18 Time Off
Celine George
 
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT PatnaSwachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Quiz Club, Indian Institute of Technology, Patna
 
Multicultural approach in education - B.Ed
Multicultural approach in education - B.EdMulticultural approach in education - B.Ed
Multicultural approach in education - B.Ed
prathimagowda443
 
POS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 SlidesPOS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 Slides
Celine George
 
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
Sritoma Majumder
 
Types of Actions in Odoo 18 - Odoo Slides
Types of Actions in Odoo 18 - Odoo SlidesTypes of Actions in Odoo 18 - Odoo Slides
Types of Actions in Odoo 18 - Odoo Slides
Celine George
 
LDMMIA Free Reiki Yoga S7 Weekly Workshops
LDMMIA Free Reiki Yoga S7 Weekly WorkshopsLDMMIA Free Reiki Yoga S7 Weekly Workshops
LDMMIA Free Reiki Yoga S7 Weekly Workshops
LDM & Mia eStudios
 
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
RVSPSOA
 
K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910
PankajRodey1
 
STUDENT LOAN TRUST FUND DEFAULTERS GHANA
STUDENT LOAN TRUST FUND DEFAULTERS GHANASTUDENT LOAN TRUST FUND DEFAULTERS GHANA
STUDENT LOAN TRUST FUND DEFAULTERS GHANA
Kweku Zurek
 
"Hymenoptera: A Diverse and Fascinating Order".pptx
"Hymenoptera: A Diverse and Fascinating Order".pptx"Hymenoptera: A Diverse and Fascinating Order".pptx
"Hymenoptera: A Diverse and Fascinating Order".pptx
Arshad Shaikh
 
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATIONTHE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
PROF. PAUL ALLIEU KAMARA
 
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
SweetytamannaMohapat
 
Exploring Identity Through Colombian Companies
Exploring Identity Through Colombian CompaniesExploring Identity Through Colombian Companies
Exploring Identity Through Colombian Companies
OlgaLeonorTorresSnch
 
"Dictyoptera: The Order of Cockroaches and Mantises" Or, more specifically: ...
"Dictyoptera: The Order of Cockroaches and Mantises"  Or, more specifically: ..."Dictyoptera: The Order of Cockroaches and Mantises"  Or, more specifically: ...
"Dictyoptera: The Order of Cockroaches and Mantises" Or, more specifically: ...
Arshad Shaikh
 
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdf
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdfTechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdf
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdf
TechSoup
 
Cloud Computing ..PPT ( Faizan ALTAF )..
Cloud Computing ..PPT ( Faizan ALTAF )..Cloud Computing ..PPT ( Faizan ALTAF )..
Cloud Computing ..PPT ( Faizan ALTAF )..
faizanaltaf231
 
Coleoptera: The Largest Insect Order.pptx
Coleoptera: The Largest Insect Order.pptxColeoptera: The Largest Insect Order.pptx
Coleoptera: The Largest Insect Order.pptx
Arshad Shaikh
 
How to Create Time Off Request in Odoo 18 Time Off
How to Create Time Off Request in Odoo 18 Time OffHow to Create Time Off Request in Odoo 18 Time Off
How to Create Time Off Request in Odoo 18 Time Off
Celine George
 
Multicultural approach in education - B.Ed
Multicultural approach in education - B.EdMulticultural approach in education - B.Ed
Multicultural approach in education - B.Ed
prathimagowda443
 
POS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 SlidesPOS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 Slides
Celine George
 
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
Sritoma Majumder
 

Lecture 3 data structures & algorithms - sorting techniques - http://techieme.in

  • 1. Description: A detailed discussion about algorithms and their measures, and understanding sorting. Duration: 90 minutes Starts at: Saturday 12th May 2013, 11:00AM -by Dharmendra Prasad 1
  • 2. Table of Contents 1. Continuing the Sorting Algorithms. 1. Quick Sort (in place sorting algorithm) 2. Searching Algorithms 1. Binary search 3. Some real world problem scenarios. 1. Substring search 2
  • 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. 3
  • 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+1 exchange A[i] ↔ A[j] exchange A[p] ↔ A[i]; return i 4 A p qi j
  • 5. 5  Example: 6 10 13 5 8 3 2 11 X = 6, i = 0, j = 1 6 5 13 10 8 3 2 11 6 5 3 10 8 13 2 11 6 5 3 2 8 13 10 11 Swap pivot with i 2 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) 6
  • 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 7
  • 8. Algorithm: OrderStat(A,p,q,k) // means kth smallest number in A between index p and q if 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) 8
  • 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 n do if A[i] == p return i return null 9
  • 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)/2 if A[middle] == p return middle; else if A[middle] > p return BinarySearch(A, p, low, middle-1) else return BinarySearch(A,p,middle+1,high) 10
  • 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.length if S[i] == sb[j] match = true i++, j++ else match = false j=0, i++ if match == true and j = sb.length return i-sb.length else return -1 11
  • 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. 12