SlideShare a Scribd company logo
Description: A detailed discussion about algorithms and their
measures, and understanding sorting.
Duration: 90 minutes
Starts at: Saturday 11th May 2013, 11:00AM
-by Dharmendra Prasad
1
Table of Contents
1. A detailed talk on algorithms.
2. Measuring the effectiveness of an algorithm.
3. Sorting Algorithms (In memory)
4. Some real world sorting scenarios.
2
Algorithm:
1. A step by step procedure to solve a given problem.
2. It takes an input and applies the sequence of steps to arrive
on a desired output.
3. Represented as a Pseudo Codes, which are English like
statements.
3
Analysis of Algorithms:
1. The theoretical study of computer program performance
and resource usage.
2. Things more important than the performance and resource
usage are:
1. Correctness
2. Simplicity
3. Features
4. User friendliness
5. Maintainability
6. Security
7. Algorithms
4
Why study performance if other things are more important than
performance?
Performance measures the line between the feasible or in feasible:
 E.g. : If something takes un limited time, it is not useful to us.
 If it takes a lot of space which is more than available , it is infeasible.
 Real time constraints, need the result on time else the result is not useful.
We are the decision makers when have to choose between performance and other
factors like user experience, maintainability etc.
Analyzing an algorithm means studying its performance in terms of running time,
space required, etc.
5
Sorting: The classic problem
Problem definition:
Input: We have a sequence <a1, a2, … , an> of numbers.
Output expected: A permutation <a1’, a2; …, an’> such that
a1’ <= a2’ <= a3’ … <=an’
Basic definition: Take a bunch of numbers and put them in order. Order
necessarily doesn’t mean increasing order. It can be
ascending/descending etc.
6
Sorting: The classic problem
Pseudo Code:
Insertion-Sort(A, n) // sorts A[1..n]
for j <- 2 to n
do key <- A[j];
i <- j-1
while i > 0 and A[i] > key
do A[i+1] <- A[i]
i = i -1
A[i+1] = key
7
A->
sorted
J
key
Sorting: The classic problem
Example:
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
2 3 4 6 8 9
8
Analyzing the above algorithm:
Running Time:
• Depends on the input (e.g. if it is already sorted)
• Depends on input size ( e.g. 10 elements vs. 10,000,000)
If the input is already sorted, Insertion Sort has nothing much to do.( This is
called the best case)
If the input is reverse sorted, Insertion sort will shuffle in each step and takes
the maximum possible time. ( This is called the worst case)
If the input is random that will be called as the average case.
Time taken in the above algorithm = Sum of the work inside the outer loop
T(n) =∑ f( j ) which is proportional to j²
You can also say as T(n) = θ(n²)
Can we say that insertion sort is fast?
9
Merge Sort:
Merge Sort A[1 .. n]
Step 1: if n = 1, done;
Step 2: Recursively sort A[1..(n/2)] and A[n/2 +1, n]
Step 3: Merge both the sorted lists.
Merge A, B
10
A B
1 2 7 9 11 12 13 20
How much time merging took ?
It just took n steps or we can say the work done is proportional to
n , hence the time taken will be:
T(n) proportional to n where n is the total number of elements in
the sorted array.
T(n) = θ(n)
Analyzing merge sort:
Step 1: How much this step takes?
Constant time, you just need to find the middle index of the
array. This means it is not dependent on the input size.
Also called θ(1)
Step 2: How much this step takes?
Time taken in sorting A[1, n/2] + Time taken in sorting A[n/2+1,
n]
Lets say that each of the two steps take T[n/2] time.
11
Step 3: The merge step took θ(n).
So the total time taken by the merge sort is a sum of all the
three steps. That can be defined as below:
θ (1) , if n =1
T(n) =
2T(n/2) + θ (n) if n > 1
Hence, T(n) = 2T(n/2) + c.n, where c > 0
12
13
Height of the above tree is : log n
T(n) = cn (log n) + something proportional to n
T(n) = cn (log n) + θ (n)
Hence T(n) = θ (n log n)
Comparing Insertion Sort and Merge Sort
Insertion sort time T(n) = θ(n²)
Merge sort time T’(n) = θ (n log n)
Which one do you think is more time?
14
Real life usage of sorting algorithm:
1) A cable operators personalized service.
2) Alphabetical arrangement of list of items on a shopping
website.
3) Offering admissions to applicants with highest grades.
And many more…
15
Question
&
Answers
16

More Related Content

What's hot (20)

PDF
Algorithms Lecture 2: Analysis of Algorithms I
Mohamed Loey
 
PPT
Data Structures and Algorithm Analysis
Mary Margarat
 
PPT
Analysis Of Algorithms I
Sri Prasanna
 
PDF
Analysis and design of algorithms part2
Deepak John
 
PDF
Anlysis and design of algorithms part 1
Deepak John
 
PPTX
Algorithm analysis (All in one)
jehan1987
 
PPT
Algorithm And analysis Lecture 03& 04-time complexity.
Tariq Khan
 
PPT
Sorting
Ghaffar Khan
 
PPTX
Merge sort
lakshitha perera
 
PPTX
Daa unit 2
snehajiyani
 
PPTX
Lecture 2 data structures and algorithms
Aakash deep Singhal
 
PDF
Quick Sort , Merge Sort , Heap Sort
Mohammed Hussein
 
PPT
CS8461 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
PPT
CS8451 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
PPT
Introduction to Algorithms
Venkatesh Iyer
 
PPT
chapter 1
yatheesha
 
PPTX
Performance analysis(Time & Space Complexity)
swapnac12
 
PPT
Sorting Algorithms
multimedia9
 
PDF
Introduction to Algorithms Complexity Analysis
Dr. Pankaj Agarwal
 
PPT
Lect11 Sorting
ryokollll
 
Algorithms Lecture 2: Analysis of Algorithms I
Mohamed Loey
 
Data Structures and Algorithm Analysis
Mary Margarat
 
Analysis Of Algorithms I
Sri Prasanna
 
Analysis and design of algorithms part2
Deepak John
 
Anlysis and design of algorithms part 1
Deepak John
 
Algorithm analysis (All in one)
jehan1987
 
Algorithm And analysis Lecture 03& 04-time complexity.
Tariq Khan
 
Sorting
Ghaffar Khan
 
Merge sort
lakshitha perera
 
Daa unit 2
snehajiyani
 
Lecture 2 data structures and algorithms
Aakash deep Singhal
 
Quick Sort , Merge Sort , Heap Sort
Mohammed Hussein
 
CS8461 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
CS8451 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
Introduction to Algorithms
Venkatesh Iyer
 
chapter 1
yatheesha
 
Performance analysis(Time & Space Complexity)
swapnac12
 
Sorting Algorithms
multimedia9
 
Introduction to Algorithms Complexity Analysis
Dr. Pankaj Agarwal
 
Lect11 Sorting
ryokollll
 

Viewers also liked (12)

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

Similar to Lecture 2 data structures & algorithms - sorting techniques (20)

PDF
Alg_Wks1_2.pdflklokjbhvkv jv .v.vk.hk kv h/k
227567
 
PPTX
Algorithms - "Chapter 2 getting started"
Ra'Fat Al-Msie'deen
 
PPTX
my docoment
NeeshanYonzan
 
PDF
Study on Sorting Algorithm and Position Determining Sort
IRJET Journal
 
PPT
Cis435 week01
ashish bansal
 
DOCX
Ds
Mohit Saini
 
PPTX
sorting-160810203705.pptx
VarchasvaTiwari2
 
PPTX
Merge sort analysis and its real time applications
yazad dumasia
 
PDF
Quick sort,bubble sort,heap sort and merge sort
abhinavkumar77723
 
PDF
DAA - chapter 1.pdf
ASMAALWADEE2
 
PPT
Sorting
Samsil Arefin
 
PPTX
Merge sort and quick sort
Shakila Mahjabin
 
PPT
MergesortQuickSort.ppt
AliAhmad38278
 
PPT
presentation_mergesortquicksort_1458716068_193111.ppt
ajiths82
 
PPT
02_Gffdvxvvxzxzczcczzczcczczczxvxvxvds2.ppt
DarioVelo1
 
PPT
Algorithms and Data structures: Merge Sort
pharmaci
 
PDF
Sorting algorithms bubble sort to merge sort.pdf
AyeshaMazhar21
 
PPT
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
RafishaikIT02044
 
PPT
03_sorting and it's types with example .ppt
vanshii9976
 
PPT
03_sorting123456789454545454545444543.ppt
ssuser7b9bda1
 
Alg_Wks1_2.pdflklokjbhvkv jv .v.vk.hk kv h/k
227567
 
Algorithms - "Chapter 2 getting started"
Ra'Fat Al-Msie'deen
 
my docoment
NeeshanYonzan
 
Study on Sorting Algorithm and Position Determining Sort
IRJET Journal
 
Cis435 week01
ashish bansal
 
sorting-160810203705.pptx
VarchasvaTiwari2
 
Merge sort analysis and its real time applications
yazad dumasia
 
Quick sort,bubble sort,heap sort and merge sort
abhinavkumar77723
 
DAA - chapter 1.pdf
ASMAALWADEE2
 
Sorting
Samsil Arefin
 
Merge sort and quick sort
Shakila Mahjabin
 
MergesortQuickSort.ppt
AliAhmad38278
 
presentation_mergesortquicksort_1458716068_193111.ppt
ajiths82
 
02_Gffdvxvvxzxzczcczzczcczczczxvxvxvds2.ppt
DarioVelo1
 
Algorithms and Data structures: Merge Sort
pharmaci
 
Sorting algorithms bubble sort to merge sort.pdf
AyeshaMazhar21
 
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
RafishaikIT02044
 
03_sorting and it's types with example .ppt
vanshii9976
 
03_sorting123456789454545454545444543.ppt
ssuser7b9bda1
 
Ad

Recently uploaded (20)

PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 

Lecture 2 data structures & algorithms - sorting techniques

  • 1. Description: A detailed discussion about algorithms and their measures, and understanding sorting. Duration: 90 minutes Starts at: Saturday 11th May 2013, 11:00AM -by Dharmendra Prasad 1
  • 2. Table of Contents 1. A detailed talk on algorithms. 2. Measuring the effectiveness of an algorithm. 3. Sorting Algorithms (In memory) 4. Some real world sorting scenarios. 2
  • 3. Algorithm: 1. A step by step procedure to solve a given problem. 2. It takes an input and applies the sequence of steps to arrive on a desired output. 3. Represented as a Pseudo Codes, which are English like statements. 3
  • 4. Analysis of Algorithms: 1. The theoretical study of computer program performance and resource usage. 2. Things more important than the performance and resource usage are: 1. Correctness 2. Simplicity 3. Features 4. User friendliness 5. Maintainability 6. Security 7. Algorithms 4
  • 5. Why study performance if other things are more important than performance? Performance measures the line between the feasible or in feasible:  E.g. : If something takes un limited time, it is not useful to us.  If it takes a lot of space which is more than available , it is infeasible.  Real time constraints, need the result on time else the result is not useful. We are the decision makers when have to choose between performance and other factors like user experience, maintainability etc. Analyzing an algorithm means studying its performance in terms of running time, space required, etc. 5
  • 6. Sorting: The classic problem Problem definition: Input: We have a sequence <a1, a2, … , an> of numbers. Output expected: A permutation <a1’, a2; …, an’> such that a1’ <= a2’ <= a3’ … <=an’ Basic definition: Take a bunch of numbers and put them in order. Order necessarily doesn’t mean increasing order. It can be ascending/descending etc. 6
  • 7. Sorting: The classic problem Pseudo Code: Insertion-Sort(A, n) // sorts A[1..n] for j <- 2 to n do key <- A[j]; i <- j-1 while i > 0 and A[i] > key do A[i+1] <- A[i] i = i -1 A[i+1] = key 7 A-> sorted J key
  • 8. Sorting: The classic problem Example: 8 2 4 9 3 6 2 8 4 9 3 6 2 4 8 9 3 6 2 4 8 9 3 6 2 3 4 8 9 6 2 3 4 6 8 9 8
  • 9. Analyzing the above algorithm: Running Time: • Depends on the input (e.g. if it is already sorted) • Depends on input size ( e.g. 10 elements vs. 10,000,000) If the input is already sorted, Insertion Sort has nothing much to do.( This is called the best case) If the input is reverse sorted, Insertion sort will shuffle in each step and takes the maximum possible time. ( This is called the worst case) If the input is random that will be called as the average case. Time taken in the above algorithm = Sum of the work inside the outer loop T(n) =∑ f( j ) which is proportional to j² You can also say as T(n) = θ(n²) Can we say that insertion sort is fast? 9
  • 10. Merge Sort: Merge Sort A[1 .. n] Step 1: if n = 1, done; Step 2: Recursively sort A[1..(n/2)] and A[n/2 +1, n] Step 3: Merge both the sorted lists. Merge A, B 10 A B 1 2 7 9 11 12 13 20
  • 11. How much time merging took ? It just took n steps or we can say the work done is proportional to n , hence the time taken will be: T(n) proportional to n where n is the total number of elements in the sorted array. T(n) = θ(n) Analyzing merge sort: Step 1: How much this step takes? Constant time, you just need to find the middle index of the array. This means it is not dependent on the input size. Also called θ(1) Step 2: How much this step takes? Time taken in sorting A[1, n/2] + Time taken in sorting A[n/2+1, n] Lets say that each of the two steps take T[n/2] time. 11
  • 12. Step 3: The merge step took θ(n). So the total time taken by the merge sort is a sum of all the three steps. That can be defined as below: θ (1) , if n =1 T(n) = 2T(n/2) + θ (n) if n > 1 Hence, T(n) = 2T(n/2) + c.n, where c > 0 12
  • 13. 13
  • 14. Height of the above tree is : log n T(n) = cn (log n) + something proportional to n T(n) = cn (log n) + θ (n) Hence T(n) = θ (n log n) Comparing Insertion Sort and Merge Sort Insertion sort time T(n) = θ(n²) Merge sort time T’(n) = θ (n log n) Which one do you think is more time? 14
  • 15. Real life usage of sorting algorithm: 1) A cable operators personalized service. 2) Alphabetical arrangement of list of items on a shopping website. 3) Offering admissions to applicants with highest grades. And many more… 15