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)

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

Viewers also liked (10)

PPT
Lecture 2c stacks
Victor Palmar
 
PPSX
Lecture 2 data structures & algorithms - sorting techniques
Dharmendra Prasad
 
PPTX
Arrays
Trupti Agrawal
 
PPTX
C# Arrays
Hock Leng PUAH
 
PPTX
Arrays C#
Raghuveer Guthikonda
 
PPT
Lecture 2a arrays
Victor Palmar
 
PPTX
queue & its applications
somendra kumar
 
PDF
Lecture17 arrays.ppt
eShikshak
 
PPT
Arrays
archikabhatia
 
PPTX
Array in c language
home
 
Lecture 2c stacks
Victor Palmar
 
Lecture 2 data structures & algorithms - sorting techniques
Dharmendra Prasad
 
C# Arrays
Hock Leng PUAH
 
Lecture 2a arrays
Victor Palmar
 
queue & its applications
somendra kumar
 
Lecture17 arrays.ppt
eShikshak
 
Array in c language
home
 
Ad

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

PDF
Chapter 1 - Introduction to Searching and Sorting Algorithms - Student.pdf
mylinhbangus
 
PPT
Searching Sorting-SELECTION ,BUBBBLE.ppt
kunalpatil5661
 
PDF
module2_dIVIDEncONQUER_2022.pdf
Shiwani Gupta
 
PDF
Data Structures (BE)
PRABHAHARAN429
 
PPT
4.1 sequentioal search
Krish_ver2
 
ODP
Data operatons & searching and sorting algorithms
Anushdika Jeganathan
 
PPTX
SORT AND SEARCH ARRAY WITH WITH C++.pptx
narifmsit18seecs
 
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
PPT
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
PPT
11. Arrays
Nilesh Dalvi
 
PPTX
Sorting
Ashim Lamichhane
 
PPT
Algorithms the fundamentals, For computer Science.ppt
CarloCimacio
 
PDF
Data Structures & Algorithms - Spring 2025.pdf
Syed Zaid Irshad
 
PPTX
Search and Sort algorithms. Bubble, Insertion, Selection.
FerryKemperman
 
PPTX
Searching, Sorting and Hashing Techniques
Selvaraj Seerangan
 
PPTX
Searching searching in in arrays arrays.pptx
Sahar160629
 
PDF
INDEX SORT
Waqas Tariq
 
DOCX
Ds
Mohit Saini
 
PPTX
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PradipTadme
 
PPTX
sorting and searching.pptx
ParagAhir1
 
Chapter 1 - Introduction to Searching and Sorting Algorithms - Student.pdf
mylinhbangus
 
Searching Sorting-SELECTION ,BUBBBLE.ppt
kunalpatil5661
 
module2_dIVIDEncONQUER_2022.pdf
Shiwani Gupta
 
Data Structures (BE)
PRABHAHARAN429
 
4.1 sequentioal search
Krish_ver2
 
Data operatons & searching and sorting algorithms
Anushdika Jeganathan
 
SORT AND SEARCH ARRAY WITH WITH C++.pptx
narifmsit18seecs
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
11. Arrays
Nilesh Dalvi
 
Algorithms the fundamentals, For computer Science.ppt
CarloCimacio
 
Data Structures & Algorithms - Spring 2025.pdf
Syed Zaid Irshad
 
Search and Sort algorithms. Bubble, Insertion, Selection.
FerryKemperman
 
Searching, Sorting and Hashing Techniques
Selvaraj Seerangan
 
Searching searching in in arrays arrays.pptx
Sahar160629
 
INDEX SORT
Waqas Tariq
 
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PradipTadme
 
sorting and searching.pptx
ParagAhir1
 
Ad

Recently uploaded (20)

PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 

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