SlideShare a Scribd company logo
Analysis and Design of Algorithms
Sorting Algorithms I
Analysis and Design of Algorithms
Sorting Algorithms
Bubble Sort
Selection Sort
Insertion Sort
Analysis and Design of Algorithms
 Sorting Algorithm is an algorithm made up of a series of instructions
that takes an array as input, and outputs a sorted array.
 There are many sorting algorithms, such as:
 Selection Sort, Bubble Sort, Insertion Sort, Merge Sort,
Heap Sort, QuickSort, Radix Sort, Counting Sort, Bucket
Sort, ShellSort, Comb Sort, Pigeonhole Sort, Cycle Sort
Analysis and Design of Algorithms
Bubble Sort
Analysis and Design of Algorithms
Bubble Sort is the simplest sorting algorithm
that works by repeatedly swapping the
adjacent elements if they are in wrong
order.
Analysis and Design of Algorithms
Algorithm:
 Step1: Compare each pair of adjacent elements in the list
 Step2: Swap two element if necessary
 Step3: Repeat this process for all the elements until the
entire array is sorted
Analysis and Design of Algorithms
 Example 1 Assume the following Array:
5 1 4 2
Analysis and Design of Algorithms
 First Iteration:
 Compare
5 1 4 2

j

j+1
Analysis and Design of Algorithms
 First Iteration:
 Swap
1 5 4 2

j

j+1
Analysis and Design of Algorithms
 First Iteration:
 Compare
1 5 4 2

j

j+1
Analysis and Design of Algorithms
 First Iteration:
 Swap
1 4 5 2

j

j+1
Analysis and Design of Algorithms
 First Iteration:
 Compare
1 4 5 2

j

j+1
Analysis and Design of Algorithms
 First Iteration:
 Swap
1 4 2 5

j

j+1
Analysis and Design of Algorithms
1 4 2 5
Analysis and Design of Algorithms
 Second Iteration:
 Compare
1 4 2 5

j

j+1
Analysis and Design of Algorithms
 Second Iteration:
 Compare
1 4 2 5

j

j+1
Analysis and Design of Algorithms
 Second Iteration:
 Swap
1 2 4 5

j

j+1
Analysis and Design of Algorithms
1 2 4 5
Analysis and Design of Algorithms
 Third Iteration:
 Compare
1 2 4 5

j

j+1
Analysis and Design of Algorithms
1 2 4 5
Analysis and Design of Algorithms
 Array is now sorted
1 2 3 4
Analysis and Design of Algorithms
5 4 2 1 3
4 5 2 1 3
4 2 5 1 3
4 2 1 5 3
4 2 1 3 5
4 2 1 3 5
2 4 1 3 5
2 1 4 3 5
2 1 3 4 5
2 1 3 4 5
1 2 3 4 5
1 2 3 4 5
 Example 2:
Analysis and Design of Algorithms
 What is the output of bubble sort after the 1st iteration given the
following sequence of numbers: 13 2 9 4 18 45 37 63
a) 2 4 9 13 18 37 45 63
b) 2 9 4 13 18 37 45 63
c) 13 2 4 9 18 45 37 63
d) 2 4 9 13 18 45 37 63
Analysis and Design of Algorithms
 What is the output of bubble sort after the 1st iteration given the
following sequence of numbers: 13 2 9 4 18 45 37 63
a) 2 4 9 13 18 37 45 63
b) 2 9 4 13 18 37 45 63
c) 13 2 4 9 18 45 37 63
d) 2 4 9 13 18 45 37 63
Analysis and Design of Algorithms
 Python Code
Analysis and Design of Algorithms
Analysis and Design of Algorithms
 Time Complexity: O(n2) as there are two nested loops
 Example of worst case
5 4 3 2 1
Analysis and Design of Algorithms
Selection Sort
Analysis and Design of Algorithms
The selection sort algorithm sorts an array by
repeatedly finding the minimum element
(considering ascending order) from unsorted
part and putting it at the beginning.
Analysis and Design of Algorithms
Algorithm:
 Step1: Find the minimum value in the list
 Step2: Swap it with the value in the current position
 Step3: Repeat this process for all the elements until the
entire array is sorted
Analysis and Design of Algorithms
 Example 1 Assume the following Array:
8 12 5 9 2
Analysis and Design of Algorithms
 Compare
8 12 5 9 2

i

j

min
Analysis and Design of Algorithms
 Compare
8 12 5 9 2

i

j

min
Analysis and Design of Algorithms
 Move
8 12 5 9 2

j

i

min
Analysis and Design of Algorithms
 Compare
8 12 5 9 2

i

min

j
Analysis and Design of Algorithms
 Compare
8 12 5 9 2

i

min

j
Analysis and Design of Algorithms
 Move
8 12 5 9 2

i

j

min
Analysis and Design of Algorithms
 Smallest
8 12 5 9 2

i

min
Analysis and Design of Algorithms
 Swap
8 12 5 9 2

min

i
Analysis and Design of Algorithms
 Sorted
 Un Sorted
2 12 5 9 8

Sorted

Un Sorted
Analysis and Design of Algorithms
 Compare
2 12 5 9 8

i

min

j

Sorted
Analysis and Design of Algorithms
 Move
2 12 5 9 8

i

min

j

Sorted
Analysis and Design of Algorithms
 Compare
2 12 5 9 8

Sorted

i

j

min
Analysis and Design of Algorithms
 Compare
2 12 5 9 8

Sorted

i

j

min
Analysis and Design of Algorithms
 Smallest
2 12 5 9 8

Sorted

i

min
Analysis and Design of Algorithms
 Swap
2 12 5 9 8

Sorted

i

min
Analysis and Design of Algorithms
 Sorted
 Un Sorted
2 5 12 9 8

Sorted

Un Sorted
Analysis and Design of Algorithms
 Compare
2 5 12 9 8

Sorted

i

j

min
Analysis and Design of Algorithms
 Move
2 5 12 9 8

Sorted

i

j

min
Analysis and Design of Algorithms
 Compare
2 5 12 9 8

Sorted

i

min

j
Analysis and Design of Algorithms
 Move
2 5 12 9 8

Sorted

i

min

j
Analysis and Design of Algorithms
 Smallest
2 5 12 9 8

Sorted

i

min
Analysis and Design of Algorithms
 Swap
2 5 12 9 8

Sorted

i

min
Analysis and Design of Algorithms
 Sorted
 Un Sorted
2 5 8 9 12

Sorted

Un Sorted
Analysis and Design of Algorithms
 Compare
2 5 8 9 12

Sorted

i

min

j
Analysis and Design of Algorithms
 Sorted
 Un Sorted
2 5 8 9 12

Sorted

Un Sorted
Analysis and Design of Algorithms
 Sorted
 Un Sorted
2 5 8 9 12

Sorted

i

min
Analysis and Design of Algorithms
 Array is now sorted
2 5 8 9 12

Sorted
Analysis and Design of Algorithms
12 10 16 11 9 7 Example 2:
7 10 16 11 9 12
7 9 16 11 10 12
7 9 10 11 16 12
7 9 10 11 16 12
7 9 10 11 12 16
12 10 16 11 9 7
Analysis and Design of Algorithms
 What is the output of selection sort after the 2nd iteration given
the following sequence of numbers: 13 2 9 4 18 45 37 63
a) 2 4 9 13 18 37 45 63
b) 2 9 4 13 18 37 45 63
c) 13 2 4 9 18 45 37 63
d) 2 4 9 13 18 45 37 63
Analysis and Design of Algorithms
 What is the output of selection sort after the 2nd iteration given
the following sequence of numbers: 13 2 9 4 18 45 37 63
a) 2 4 9 13 18 37 45 63
b) 2 9 4 13 18 37 45 63
c) 13 2 4 9 18 45 37 63
d) 2 4 9 13 18 45 37 63
Analysis and Design of Algorithms
 Python Code
Analysis and Design of Algorithms
Analysis and Design of Algorithms
 Time Complexity: O(n2) as there are two nested loops
 Example of worst case
2 3 4 5 1
Analysis and Design of Algorithms
Insertion Sort
Analysis and Design of Algorithms
Insertion sort is a simple sorting
algorithm that works the way we sort
playing cards in our hands.
Analysis and Design of Algorithms
 Algorithm:
 Step1: Compare each pair of adjacent elements in the list
 Step2: Insert element into the sorted list, until it occupies correct
position.
 Step3: Swap two element if necessary
 Step4: Repeat this process for all the elements until the entire
array is sorted
Analysis and Design of Algorithms
 Assume the following Array:
5 1 4 2
Analysis and Design of Algorithms
 Compare
 Store=
5 1 4 2

i

j

j+1
1
Analysis and Design of Algorithms
 Move
 Store=
5 4 2

i

j

j+1
1
Analysis and Design of Algorithms
 Move
 Store=
1 5 4 2

i

j+1
Analysis and Design of Algorithms
 Compare
 Store=
1 5 4 2

i

j
4

j+1
Analysis and Design of Algorithms
 Move
 Store=
1 5 2

i

j
4

j+1
Analysis and Design of Algorithms
 Compare
 Store=
1 5 2

i

j
4

j+1
Analysis and Design of Algorithms
 Move
 Store=
1 4 5 2

i

j

j+1
Analysis and Design of Algorithms
 Compare
 Store=
1 4 5 2

i

j
2

j+1
Analysis and Design of Algorithms
 Move
 Store=
1 4 5

i

j
2

j+1
Analysis and Design of Algorithms
 Compare
 Store=
1 4 5

i

j
2

j+1
Analysis and Design of Algorithms
 Move
 Store=
1 4 5

i

j
2

j+1
Analysis and Design of Algorithms
 Compare
 Store=
1 4 5

i

j
2

j+1
Analysis and Design of Algorithms
 Compare
 Store=
1 2 4 5

i

j

j+1
Analysis and Design of Algorithms
 Array is now sorted
1 2 4 5
Analysis and Design of Algorithms
5 1 8 3 9 2
 Example 2:
1 5 8 3 9 2
1 5 8 3 9 2
1 3 5 8 9 2
1 3 5 8 9 2
1 2 3 5 8 9
5 1 8 3 9 2
Analysis and Design of Algorithms
 What is the output of insertion sort after the 1st iteration given the
following sequence of numbers: 7 3 5 1 9 8 4 6
a) 3 7 5 1 9 8 4 6
b) 1 3 7 5 9 8 4 6
c) 3 4 1 5 6 8 7 9
d) 1 3 4 5 6 7 8 9
Analysis and Design of Algorithms
 What is the output of insertion sort after the 1st iteration given the
following sequence of numbers: 7 3 5 1 9 8 4 6
a) 3 7 5 1 9 8 4 6
b) 1 3 7 5 9 8 4 6
c) 3 4 1 5 6 8 7 9
d) 1 3 4 5 6 7 8 9
Analysis and Design of Algorithms
 What is the output of insertion sort after the 2nd iteration given the
following sequence of numbers: 7 3 5 1 9 8 4 6
a) 3 5 7 1 9 8 4 6
b) 1 3 7 5 9 8 4 6
c) 3 4 1 5 6 8 7 9
d) 1 3 4 5 6 7 8 9
Analysis and Design of Algorithms
 What is the output of insertion sort after the 2nd iteration given the
following sequence of numbers: 7 3 5 1 9 8 4 6
a) 3 5 7 1 9 8 4 6
b) 1 3 7 5 9 8 4 6
c) 3 4 1 5 6 8 7 9
d) 1 3 4 5 6 7 8 9
Analysis and Design of Algorithms
 Python Code
Analysis and Design of Algorithms
Analysis and Design of Algorithms
 Time Complexity: O(n2)
 Example of worst case
5 4 3 2 1
Analysis and Design of Algorithms
facebook.com/mloey
mohamedloey@gmail.com
twitter.com/mloey
linkedin.com/in/mloey
mloey@fci.bu.edu.eg
mloey.github.io
Analysis and Design of Algorithms
www.YourCompany.com
© 2020 Companyname PowerPoint Business Theme. All Rights Reserved.
THANKS FOR
YOUR TIME

More Related Content

What's hot (20)

Hashing
HashingHashing
Hashing
Amar Jukuntla
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
Meghaj Mallick
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
shameen khan
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
Sorting
SortingSorting
Sorting
Ashim Lamichhane
 
Time complexity
Time complexityTime complexity
Time complexity
Katang Isip
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
Sorting Techniques
Sorting TechniquesSorting Techniques
Sorting Techniques
Rafay Farooq
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
Dr Sandeep Kumar Poonia
 
Bubble sort
Bubble sortBubble sort
Bubble sort
Manek Ar
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
Janki Shah
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
Jeanie Arnoco
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
eShikshak
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
International Islamic University
 
stack & queue
stack & queuestack & queue
stack & queue
manju rani
 
Linked List
Linked ListLinked List
Linked List
Ashim Lamichhane
 
Linked list
Linked listLinked list
Linked list
akshat360
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
sumitbardhan
 
Hashing PPT
Hashing PPTHashing PPT
Hashing PPT
Saurabh Kumar
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
03446940736
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
Meghaj Mallick
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
shameen khan
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
Sorting Techniques
Sorting TechniquesSorting Techniques
Sorting Techniques
Rafay Farooq
 
Bubble sort
Bubble sortBubble sort
Bubble sort
Manek Ar
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
Janki Shah
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
Jeanie Arnoco
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
eShikshak
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
sumitbardhan
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
03446940736
 

Viewers also liked (6)

Convolutional Neural Network Models - Deep Learning
Convolutional Neural Network Models - Deep LearningConvolutional Neural Network Models - Deep Learning
Convolutional Neural Network Models - Deep Learning
Mohamed Loey
 
Computer Security Lecture 7: RSA
Computer Security Lecture 7: RSAComputer Security Lecture 7: RSA
Computer Security Lecture 7: RSA
Mohamed Loey
 
PMP Lecture 1: Introduction to Project Management
PMP Lecture 1: Introduction to Project ManagementPMP Lecture 1: Introduction to Project Management
PMP Lecture 1: Introduction to Project Management
Mohamed Loey
 
Computer Security Lecture 5: Simplified Advanced Encryption Standard
Computer Security Lecture 5: Simplified Advanced Encryption StandardComputer Security Lecture 5: Simplified Advanced Encryption Standard
Computer Security Lecture 5: Simplified Advanced Encryption Standard
Mohamed Loey
 
Deep Learning - Overview of my work II
Deep Learning - Overview of my work IIDeep Learning - Overview of my work II
Deep Learning - Overview of my work II
Mohamed Loey
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
Mohamed Loey
 
Convolutional Neural Network Models - Deep Learning
Convolutional Neural Network Models - Deep LearningConvolutional Neural Network Models - Deep Learning
Convolutional Neural Network Models - Deep Learning
Mohamed Loey
 
Computer Security Lecture 7: RSA
Computer Security Lecture 7: RSAComputer Security Lecture 7: RSA
Computer Security Lecture 7: RSA
Mohamed Loey
 
PMP Lecture 1: Introduction to Project Management
PMP Lecture 1: Introduction to Project ManagementPMP Lecture 1: Introduction to Project Management
PMP Lecture 1: Introduction to Project Management
Mohamed Loey
 
Computer Security Lecture 5: Simplified Advanced Encryption Standard
Computer Security Lecture 5: Simplified Advanced Encryption StandardComputer Security Lecture 5: Simplified Advanced Encryption Standard
Computer Security Lecture 5: Simplified Advanced Encryption Standard
Mohamed Loey
 
Deep Learning - Overview of my work II
Deep Learning - Overview of my work IIDeep Learning - Overview of my work II
Deep Learning - Overview of my work II
Mohamed Loey
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
Mohamed Loey
 
Ad

Similar to Algorithms Lecture 4: Sorting Algorithms I (20)

UNEC__1683196273.pptx
UNEC__1683196273.pptxUNEC__1683196273.pptx
UNEC__1683196273.pptx
huseynmusayev2
 
chapter 1
chapter 1chapter 1
chapter 1
yatheesha
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
Kamal Singh Lodhi
 
Lecture 1.pptx
Lecture 1.pptxLecture 1.pptx
Lecture 1.pptx
ALIZAIB KHAN
 
Lecture 01-2.ppt
Lecture 01-2.pptLecture 01-2.ppt
Lecture 01-2.ppt
RaoHamza24
 
01-Slides.pdf
01-Slides.pdf01-Slides.pdf
01-Slides.pdf
emo1421
 
Cis435 week01
Cis435 week01Cis435 week01
Cis435 week01
ashish bansal
 
Unit 1 chapter 1 Design and Analysis of Algorithms
Unit 1   chapter 1 Design and Analysis of AlgorithmsUnit 1   chapter 1 Design and Analysis of Algorithms
Unit 1 chapter 1 Design and Analysis of Algorithms
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
Sorting techniques
Sorting techniquesSorting techniques
Sorting techniques
Lovely Professional University
 
l01-intro (3).ppt
l01-intro (3).pptl01-intro (3).ppt
l01-intro (3).ppt
ssuser15a62a
 
Lect01
Lect01Lect01
Lect01
yatheesha
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3
smruti sarangi
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
jehan1987
 
InsertionSortBubbleSortSelectionSort.ppt
InsertionSortBubbleSortSelectionSort.pptInsertionSortBubbleSortSelectionSort.ppt
InsertionSortBubbleSortSelectionSort.ppt
shalinishankar0221
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
Aakash deep Singhal
 
Different Searching and Sorting Methods.pptx
Different Searching and Sorting Methods.pptxDifferent Searching and Sorting Methods.pptx
Different Searching and Sorting Methods.pptx
Minakshee Patil
 
Lec1.ppt
Lec1.pptLec1.ppt
Lec1.ppt
ssuser8bddb2
 
Sorting
SortingSorting
Sorting
Gopi Saiteja
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort  selection sortInsertion sort bubble sort  selection sort
Insertion sort bubble sort selection sort
Ummar Hayat
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
Ad

More from Mohamed Loey (19)

Lecture 6: Deep Learning Applications
Lecture 6: Deep Learning ApplicationsLecture 6: Deep Learning Applications
Lecture 6: Deep Learning Applications
Mohamed Loey
 
Lecture 5: Convolutional Neural Network Models
Lecture 5: Convolutional Neural Network ModelsLecture 5: Convolutional Neural Network Models
Lecture 5: Convolutional Neural Network Models
Mohamed Loey
 
Lecture 4: Deep Learning Frameworks
Lecture 4: Deep Learning FrameworksLecture 4: Deep Learning Frameworks
Lecture 4: Deep Learning Frameworks
Mohamed Loey
 
Lecture 4: How it Works: Convolutional Neural Networks
Lecture 4: How it Works: Convolutional Neural NetworksLecture 4: How it Works: Convolutional Neural Networks
Lecture 4: How it Works: Convolutional Neural Networks
Mohamed Loey
 
Lecture 3: Convolutional Neural Networks
Lecture 3: Convolutional Neural NetworksLecture 3: Convolutional Neural Networks
Lecture 3: Convolutional Neural Networks
Mohamed Loey
 
Lecture 2: Artificial Neural Network
Lecture 2: Artificial Neural NetworkLecture 2: Artificial Neural Network
Lecture 2: Artificial Neural Network
Mohamed Loey
 
Lecture 1: Deep Learning for Computer Vision
Lecture 1: Deep Learning for Computer VisionLecture 1: Deep Learning for Computer Vision
Lecture 1: Deep Learning for Computer Vision
Mohamed Loey
 
Design of an Intelligent System for Improving Classification of Cancer Diseases
Design of an Intelligent System for Improving Classification of Cancer DiseasesDesign of an Intelligent System for Improving Classification of Cancer Diseases
Design of an Intelligent System for Improving Classification of Cancer Diseases
Mohamed Loey
 
Computer Security - CCNA Security - Lecture 2
Computer Security - CCNA Security - Lecture 2Computer Security - CCNA Security - Lecture 2
Computer Security - CCNA Security - Lecture 2
Mohamed Loey
 
Computer Security - CCNA Security - Lecture 1
Computer Security - CCNA Security - Lecture 1Computer Security - CCNA Security - Lecture 1
Computer Security - CCNA Security - Lecture 1
Mohamed Loey
 
Algorithms Lecture 8: Pattern Algorithms
Algorithms Lecture 8: Pattern AlgorithmsAlgorithms Lecture 8: Pattern Algorithms
Algorithms Lecture 8: Pattern Algorithms
Mohamed Loey
 
Computer Security Lecture 4.1: DES Supplementary Material
Computer Security Lecture 4.1: DES Supplementary MaterialComputer Security Lecture 4.1: DES Supplementary Material
Computer Security Lecture 4.1: DES Supplementary Material
Mohamed Loey
 
PMP Lecture 4: Project Integration Management
PMP Lecture 4: Project Integration ManagementPMP Lecture 4: Project Integration Management
PMP Lecture 4: Project Integration Management
Mohamed Loey
 
Computer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Computer Security Lecture 4: Block Ciphers and the Data Encryption StandardComputer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Computer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Mohamed Loey
 
Computer Security Lecture 3: Classical Encryption Techniques 2
Computer Security Lecture 3: Classical Encryption Techniques 2Computer Security Lecture 3: Classical Encryption Techniques 2
Computer Security Lecture 3: Classical Encryption Techniques 2
Mohamed Loey
 
Computer Security Lecture 2: Classical Encryption Techniques 1
Computer Security Lecture 2: Classical Encryption Techniques 1Computer Security Lecture 2: Classical Encryption Techniques 1
Computer Security Lecture 2: Classical Encryption Techniques 1
Mohamed Loey
 
Computer Security Lecture 1: Overview
Computer Security Lecture 1: OverviewComputer Security Lecture 1: Overview
Computer Security Lecture 1: Overview
Mohamed Loey
 
PMP Lecture 3: Project Management Processes
PMP Lecture 3: Project Management ProcessesPMP Lecture 3: Project Management Processes
PMP Lecture 3: Project Management Processes
Mohamed Loey
 
PMP Lecture 2: Project Management Framework
PMP Lecture 2: Project Management FrameworkPMP Lecture 2: Project Management Framework
PMP Lecture 2: Project Management Framework
Mohamed Loey
 
Lecture 6: Deep Learning Applications
Lecture 6: Deep Learning ApplicationsLecture 6: Deep Learning Applications
Lecture 6: Deep Learning Applications
Mohamed Loey
 
Lecture 5: Convolutional Neural Network Models
Lecture 5: Convolutional Neural Network ModelsLecture 5: Convolutional Neural Network Models
Lecture 5: Convolutional Neural Network Models
Mohamed Loey
 
Lecture 4: Deep Learning Frameworks
Lecture 4: Deep Learning FrameworksLecture 4: Deep Learning Frameworks
Lecture 4: Deep Learning Frameworks
Mohamed Loey
 
Lecture 4: How it Works: Convolutional Neural Networks
Lecture 4: How it Works: Convolutional Neural NetworksLecture 4: How it Works: Convolutional Neural Networks
Lecture 4: How it Works: Convolutional Neural Networks
Mohamed Loey
 
Lecture 3: Convolutional Neural Networks
Lecture 3: Convolutional Neural NetworksLecture 3: Convolutional Neural Networks
Lecture 3: Convolutional Neural Networks
Mohamed Loey
 
Lecture 2: Artificial Neural Network
Lecture 2: Artificial Neural NetworkLecture 2: Artificial Neural Network
Lecture 2: Artificial Neural Network
Mohamed Loey
 
Lecture 1: Deep Learning for Computer Vision
Lecture 1: Deep Learning for Computer VisionLecture 1: Deep Learning for Computer Vision
Lecture 1: Deep Learning for Computer Vision
Mohamed Loey
 
Design of an Intelligent System for Improving Classification of Cancer Diseases
Design of an Intelligent System for Improving Classification of Cancer DiseasesDesign of an Intelligent System for Improving Classification of Cancer Diseases
Design of an Intelligent System for Improving Classification of Cancer Diseases
Mohamed Loey
 
Computer Security - CCNA Security - Lecture 2
Computer Security - CCNA Security - Lecture 2Computer Security - CCNA Security - Lecture 2
Computer Security - CCNA Security - Lecture 2
Mohamed Loey
 
Computer Security - CCNA Security - Lecture 1
Computer Security - CCNA Security - Lecture 1Computer Security - CCNA Security - Lecture 1
Computer Security - CCNA Security - Lecture 1
Mohamed Loey
 
Algorithms Lecture 8: Pattern Algorithms
Algorithms Lecture 8: Pattern AlgorithmsAlgorithms Lecture 8: Pattern Algorithms
Algorithms Lecture 8: Pattern Algorithms
Mohamed Loey
 
Computer Security Lecture 4.1: DES Supplementary Material
Computer Security Lecture 4.1: DES Supplementary MaterialComputer Security Lecture 4.1: DES Supplementary Material
Computer Security Lecture 4.1: DES Supplementary Material
Mohamed Loey
 
PMP Lecture 4: Project Integration Management
PMP Lecture 4: Project Integration ManagementPMP Lecture 4: Project Integration Management
PMP Lecture 4: Project Integration Management
Mohamed Loey
 
Computer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Computer Security Lecture 4: Block Ciphers and the Data Encryption StandardComputer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Computer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Mohamed Loey
 
Computer Security Lecture 3: Classical Encryption Techniques 2
Computer Security Lecture 3: Classical Encryption Techniques 2Computer Security Lecture 3: Classical Encryption Techniques 2
Computer Security Lecture 3: Classical Encryption Techniques 2
Mohamed Loey
 
Computer Security Lecture 2: Classical Encryption Techniques 1
Computer Security Lecture 2: Classical Encryption Techniques 1Computer Security Lecture 2: Classical Encryption Techniques 1
Computer Security Lecture 2: Classical Encryption Techniques 1
Mohamed Loey
 
Computer Security Lecture 1: Overview
Computer Security Lecture 1: OverviewComputer Security Lecture 1: Overview
Computer Security Lecture 1: Overview
Mohamed Loey
 
PMP Lecture 3: Project Management Processes
PMP Lecture 3: Project Management ProcessesPMP Lecture 3: Project Management Processes
PMP Lecture 3: Project Management Processes
Mohamed Loey
 
PMP Lecture 2: Project Management Framework
PMP Lecture 2: Project Management FrameworkPMP Lecture 2: Project Management Framework
PMP Lecture 2: Project Management Framework
Mohamed Loey
 

Recently uploaded (20)

TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
Quiz Club of PSG College of Arts & Science
 
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
National Information Standards Organization (NISO)
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti MpdBasic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptxRai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Strengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptxStrengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptx
SteffMusniQuiballo
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition IILDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDM & Mia eStudios
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti MpdBasic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
Strengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptxStrengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptx
SteffMusniQuiballo
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition IILDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDM & Mia eStudios
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 

Algorithms Lecture 4: Sorting Algorithms I

  • 1. Analysis and Design of Algorithms Sorting Algorithms I
  • 2. Analysis and Design of Algorithms Sorting Algorithms Bubble Sort Selection Sort Insertion Sort
  • 3. Analysis and Design of Algorithms  Sorting Algorithm is an algorithm made up of a series of instructions that takes an array as input, and outputs a sorted array.  There are many sorting algorithms, such as:  Selection Sort, Bubble Sort, Insertion Sort, Merge Sort, Heap Sort, QuickSort, Radix Sort, Counting Sort, Bucket Sort, ShellSort, Comb Sort, Pigeonhole Sort, Cycle Sort
  • 4. Analysis and Design of Algorithms Bubble Sort
  • 5. Analysis and Design of Algorithms Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.
  • 6. Analysis and Design of Algorithms Algorithm:  Step1: Compare each pair of adjacent elements in the list  Step2: Swap two element if necessary  Step3: Repeat this process for all the elements until the entire array is sorted
  • 7. Analysis and Design of Algorithms  Example 1 Assume the following Array: 5 1 4 2
  • 8. Analysis and Design of Algorithms  First Iteration:  Compare 5 1 4 2  j  j+1
  • 9. Analysis and Design of Algorithms  First Iteration:  Swap 1 5 4 2  j  j+1
  • 10. Analysis and Design of Algorithms  First Iteration:  Compare 1 5 4 2  j  j+1
  • 11. Analysis and Design of Algorithms  First Iteration:  Swap 1 4 5 2  j  j+1
  • 12. Analysis and Design of Algorithms  First Iteration:  Compare 1 4 5 2  j  j+1
  • 13. Analysis and Design of Algorithms  First Iteration:  Swap 1 4 2 5  j  j+1
  • 14. Analysis and Design of Algorithms 1 4 2 5
  • 15. Analysis and Design of Algorithms  Second Iteration:  Compare 1 4 2 5  j  j+1
  • 16. Analysis and Design of Algorithms  Second Iteration:  Compare 1 4 2 5  j  j+1
  • 17. Analysis and Design of Algorithms  Second Iteration:  Swap 1 2 4 5  j  j+1
  • 18. Analysis and Design of Algorithms 1 2 4 5
  • 19. Analysis and Design of Algorithms  Third Iteration:  Compare 1 2 4 5  j  j+1
  • 20. Analysis and Design of Algorithms 1 2 4 5
  • 21. Analysis and Design of Algorithms  Array is now sorted 1 2 3 4
  • 22. Analysis and Design of Algorithms 5 4 2 1 3 4 5 2 1 3 4 2 5 1 3 4 2 1 5 3 4 2 1 3 5 4 2 1 3 5 2 4 1 3 5 2 1 4 3 5 2 1 3 4 5 2 1 3 4 5 1 2 3 4 5 1 2 3 4 5  Example 2:
  • 23. Analysis and Design of Algorithms  What is the output of bubble sort after the 1st iteration given the following sequence of numbers: 13 2 9 4 18 45 37 63 a) 2 4 9 13 18 37 45 63 b) 2 9 4 13 18 37 45 63 c) 13 2 4 9 18 45 37 63 d) 2 4 9 13 18 45 37 63
  • 24. Analysis and Design of Algorithms  What is the output of bubble sort after the 1st iteration given the following sequence of numbers: 13 2 9 4 18 45 37 63 a) 2 4 9 13 18 37 45 63 b) 2 9 4 13 18 37 45 63 c) 13 2 4 9 18 45 37 63 d) 2 4 9 13 18 45 37 63
  • 25. Analysis and Design of Algorithms  Python Code
  • 26. Analysis and Design of Algorithms
  • 27. Analysis and Design of Algorithms  Time Complexity: O(n2) as there are two nested loops  Example of worst case 5 4 3 2 1
  • 28. Analysis and Design of Algorithms Selection Sort
  • 29. Analysis and Design of Algorithms The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning.
  • 30. Analysis and Design of Algorithms Algorithm:  Step1: Find the minimum value in the list  Step2: Swap it with the value in the current position  Step3: Repeat this process for all the elements until the entire array is sorted
  • 31. Analysis and Design of Algorithms  Example 1 Assume the following Array: 8 12 5 9 2
  • 32. Analysis and Design of Algorithms  Compare 8 12 5 9 2  i  j  min
  • 33. Analysis and Design of Algorithms  Compare 8 12 5 9 2  i  j  min
  • 34. Analysis and Design of Algorithms  Move 8 12 5 9 2  j  i  min
  • 35. Analysis and Design of Algorithms  Compare 8 12 5 9 2  i  min  j
  • 36. Analysis and Design of Algorithms  Compare 8 12 5 9 2  i  min  j
  • 37. Analysis and Design of Algorithms  Move 8 12 5 9 2  i  j  min
  • 38. Analysis and Design of Algorithms  Smallest 8 12 5 9 2  i  min
  • 39. Analysis and Design of Algorithms  Swap 8 12 5 9 2  min  i
  • 40. Analysis and Design of Algorithms  Sorted  Un Sorted 2 12 5 9 8  Sorted  Un Sorted
  • 41. Analysis and Design of Algorithms  Compare 2 12 5 9 8  i  min  j  Sorted
  • 42. Analysis and Design of Algorithms  Move 2 12 5 9 8  i  min  j  Sorted
  • 43. Analysis and Design of Algorithms  Compare 2 12 5 9 8  Sorted  i  j  min
  • 44. Analysis and Design of Algorithms  Compare 2 12 5 9 8  Sorted  i  j  min
  • 45. Analysis and Design of Algorithms  Smallest 2 12 5 9 8  Sorted  i  min
  • 46. Analysis and Design of Algorithms  Swap 2 12 5 9 8  Sorted  i  min
  • 47. Analysis and Design of Algorithms  Sorted  Un Sorted 2 5 12 9 8  Sorted  Un Sorted
  • 48. Analysis and Design of Algorithms  Compare 2 5 12 9 8  Sorted  i  j  min
  • 49. Analysis and Design of Algorithms  Move 2 5 12 9 8  Sorted  i  j  min
  • 50. Analysis and Design of Algorithms  Compare 2 5 12 9 8  Sorted  i  min  j
  • 51. Analysis and Design of Algorithms  Move 2 5 12 9 8  Sorted  i  min  j
  • 52. Analysis and Design of Algorithms  Smallest 2 5 12 9 8  Sorted  i  min
  • 53. Analysis and Design of Algorithms  Swap 2 5 12 9 8  Sorted  i  min
  • 54. Analysis and Design of Algorithms  Sorted  Un Sorted 2 5 8 9 12  Sorted  Un Sorted
  • 55. Analysis and Design of Algorithms  Compare 2 5 8 9 12  Sorted  i  min  j
  • 56. Analysis and Design of Algorithms  Sorted  Un Sorted 2 5 8 9 12  Sorted  Un Sorted
  • 57. Analysis and Design of Algorithms  Sorted  Un Sorted 2 5 8 9 12  Sorted  i  min
  • 58. Analysis and Design of Algorithms  Array is now sorted 2 5 8 9 12  Sorted
  • 59. Analysis and Design of Algorithms 12 10 16 11 9 7 Example 2: 7 10 16 11 9 12 7 9 16 11 10 12 7 9 10 11 16 12 7 9 10 11 16 12 7 9 10 11 12 16 12 10 16 11 9 7
  • 60. Analysis and Design of Algorithms  What is the output of selection sort after the 2nd iteration given the following sequence of numbers: 13 2 9 4 18 45 37 63 a) 2 4 9 13 18 37 45 63 b) 2 9 4 13 18 37 45 63 c) 13 2 4 9 18 45 37 63 d) 2 4 9 13 18 45 37 63
  • 61. Analysis and Design of Algorithms  What is the output of selection sort after the 2nd iteration given the following sequence of numbers: 13 2 9 4 18 45 37 63 a) 2 4 9 13 18 37 45 63 b) 2 9 4 13 18 37 45 63 c) 13 2 4 9 18 45 37 63 d) 2 4 9 13 18 45 37 63
  • 62. Analysis and Design of Algorithms  Python Code
  • 63. Analysis and Design of Algorithms
  • 64. Analysis and Design of Algorithms  Time Complexity: O(n2) as there are two nested loops  Example of worst case 2 3 4 5 1
  • 65. Analysis and Design of Algorithms Insertion Sort
  • 66. Analysis and Design of Algorithms Insertion sort is a simple sorting algorithm that works the way we sort playing cards in our hands.
  • 67. Analysis and Design of Algorithms  Algorithm:  Step1: Compare each pair of adjacent elements in the list  Step2: Insert element into the sorted list, until it occupies correct position.  Step3: Swap two element if necessary  Step4: Repeat this process for all the elements until the entire array is sorted
  • 68. Analysis and Design of Algorithms  Assume the following Array: 5 1 4 2
  • 69. Analysis and Design of Algorithms  Compare  Store= 5 1 4 2  i  j  j+1 1
  • 70. Analysis and Design of Algorithms  Move  Store= 5 4 2  i  j  j+1 1
  • 71. Analysis and Design of Algorithms  Move  Store= 1 5 4 2  i  j+1
  • 72. Analysis and Design of Algorithms  Compare  Store= 1 5 4 2  i  j 4  j+1
  • 73. Analysis and Design of Algorithms  Move  Store= 1 5 2  i  j 4  j+1
  • 74. Analysis and Design of Algorithms  Compare  Store= 1 5 2  i  j 4  j+1
  • 75. Analysis and Design of Algorithms  Move  Store= 1 4 5 2  i  j  j+1
  • 76. Analysis and Design of Algorithms  Compare  Store= 1 4 5 2  i  j 2  j+1
  • 77. Analysis and Design of Algorithms  Move  Store= 1 4 5  i  j 2  j+1
  • 78. Analysis and Design of Algorithms  Compare  Store= 1 4 5  i  j 2  j+1
  • 79. Analysis and Design of Algorithms  Move  Store= 1 4 5  i  j 2  j+1
  • 80. Analysis and Design of Algorithms  Compare  Store= 1 4 5  i  j 2  j+1
  • 81. Analysis and Design of Algorithms  Compare  Store= 1 2 4 5  i  j  j+1
  • 82. Analysis and Design of Algorithms  Array is now sorted 1 2 4 5
  • 83. Analysis and Design of Algorithms 5 1 8 3 9 2  Example 2: 1 5 8 3 9 2 1 5 8 3 9 2 1 3 5 8 9 2 1 3 5 8 9 2 1 2 3 5 8 9 5 1 8 3 9 2
  • 84. Analysis and Design of Algorithms  What is the output of insertion sort after the 1st iteration given the following sequence of numbers: 7 3 5 1 9 8 4 6 a) 3 7 5 1 9 8 4 6 b) 1 3 7 5 9 8 4 6 c) 3 4 1 5 6 8 7 9 d) 1 3 4 5 6 7 8 9
  • 85. Analysis and Design of Algorithms  What is the output of insertion sort after the 1st iteration given the following sequence of numbers: 7 3 5 1 9 8 4 6 a) 3 7 5 1 9 8 4 6 b) 1 3 7 5 9 8 4 6 c) 3 4 1 5 6 8 7 9 d) 1 3 4 5 6 7 8 9
  • 86. Analysis and Design of Algorithms  What is the output of insertion sort after the 2nd iteration given the following sequence of numbers: 7 3 5 1 9 8 4 6 a) 3 5 7 1 9 8 4 6 b) 1 3 7 5 9 8 4 6 c) 3 4 1 5 6 8 7 9 d) 1 3 4 5 6 7 8 9
  • 87. Analysis and Design of Algorithms  What is the output of insertion sort after the 2nd iteration given the following sequence of numbers: 7 3 5 1 9 8 4 6 a) 3 5 7 1 9 8 4 6 b) 1 3 7 5 9 8 4 6 c) 3 4 1 5 6 8 7 9 d) 1 3 4 5 6 7 8 9
  • 88. Analysis and Design of Algorithms  Python Code
  • 89. Analysis and Design of Algorithms
  • 90. Analysis and Design of Algorithms  Time Complexity: O(n2)  Example of worst case 5 4 3 2 1
  • 91. Analysis and Design of Algorithms facebook.com/mloey [email protected] twitter.com/mloey linkedin.com/in/mloey [email protected] mloey.github.io
  • 92. Analysis and Design of Algorithms www.YourCompany.com © 2020 Companyname PowerPoint Business Theme. All Rights Reserved. THANKS FOR YOUR TIME