INSERTION SORT
Insertion and merge sort
Insertion Sorting
•It is a simple Sorting algorithm which sorts the array by shifting elements one
• by one. Following are some of the important characteristics of Insertion Sort.
 It has one of the simplest implementation
 It is efficient for smaller data sets, but very inefficient for larger lists.
 Insertion Sort is adaptive, that means it reduces its total number of steps if
given a partially sorted list, hence it increases its efficiency.
 It is better than Selection Sort and Bubble Sort algorithms.
 Its space complexity is less, like Bubble Sorting, inerstion sort also requires a
single additional memory space.
 It is Stable, as it does not change the relative order of elements with equal keys
Waste slide
How Insertion Sorting Works
Insertion Sort
9 72 5 1 4 3 6
Example :
Insertion Sort
9 72 5 1 4 3 6
Sorted
section
We start by dividing the array in a section section and an unsorted section.
We put the first element as the only element in the sorted section, and
the rest of the array is the unsorted section.
Insertion Sort
9 72 5 1 4 3 6
Sorted
section
Item to
position
The first element in the unsorted section is the next
element to be put into the correct position.
Insertion Sort
9 7
2
5 1 4 3 6
Item to
position
2
We copy the element to be placed into another
variable so it doesn’t get overwritten.
Insertion Sort
9 7
2
5 1 4 3 62
compare
If the previous position is more than the item being placed,
copy the value into the next position
Insertion Sort
9 7
2
5 1 4 3 69
If there are no more items in the sorted section to compare
with, the item to be placed must go at the front.
belongs here
Insertion Sort
9 72 5 1 4 3 6
Insertion Sort
9 72 5 1 4 3 6
Insertion Sort
9 72 5 1 4 3 6
Item to
position
Insertion Sort
9
7
2 5 1 4 3 67
compare
Insertion Sort
9
7
2 5 1 4 3 6
Copied from previous
position
9
compare
If the item in the sorted section is less than the item to place,
the item to place goes after it in the array.
belongs here
Insertion Sort
972 5 1 4 3 6
Insertion Sort
972 5 1 4 3 6
Insertion Sort
972 5 1 4 3 6
Item to
position
Insertion Sort
972
5
1 4 3 65
compare
Insertion Sort
972
5
1 4 3 69
compare
Insertion Sort
972
5
1 4 3 67
compare
belongs here
Insertion Sort
972 5 1 4 3 6
Insertion Sort
972 5 1 4 3 6
Insertion Sort
972 5 1 4 3 6
Item to
position
Insertion Sort
972 5
1
4 3 61
compare
Insertion Sort
972 5
1
4 3 69
compare
Insertion Sort
972 5
1
4 3 67
compare
Insertion Sort
972 5
1
4 3 65
compare
Insertion Sort
972 5
1
4 3 62
belongs here
Insertion Sort
972 51 4 3 6
Insertion Sort
972 51 4 3 6
Insertion Sort
972 51 4 3 6
Item to
position
Insertion Sort
972 51
4
3 64
compare
Insertion Sort
972 51
4
3 69
compare
Insertion Sort
972 51
4
3 67
compare
Insertion Sort
972 51
4
3 6
belongs here
5
compare
Insertion Sort
972 51 4 3 6
Insertion Sort
972 51 4 3 6
Insertion Sort
972 51 4 3 6
Item to
position
Insertion Sort
972 51 4
3
63
compare
Insertion Sort
972 51 4
3
69
compare
Insertion Sort
972 51 4
3
67
compare
Insertion Sort
972 51 4
3
65
compare
Insertion Sort
972 51 4
3
64
compare
belongs here
Insertion Sort
972 51 43 6
Insertion Sort
972 51 43 6
Insertion Sort
972 51 43 6
Item to
position
Insertion Sort
972 51 43
6
6
compare
Insertion Sort
972 51 43
6
9
compare
Insertion Sort
972 51 43
6
7
compare
belongs here
Insertion Sort
972 51 43 6
Insertion Sort
972 51 43 6
SORTED!
Merge Sort
Merge Sorting
 The Merge Sort Algorithm is based on a simple operation known as Merging.
 It is combining of two ordered arrays to make one larger ordered array.
 It is mainly based on divide and conquer method paradigm.
 Divide the problem into a no of sub problems and similar sub problems of smaller size.
 Conquer the sub problems and solve them recursively.
 Solve the problems in straight manner and combine the solutions of sub-problem.
 Obtain the solution for sub-problem.
Merge Sort (cont.)
• Merge Sort Algorithm:
1. Merge Sort (Overview):
1. Split array into two halves
2. Sort the left half (recursively)
3. Sort the right half (recursively)
4. Merge the two sorted halves
Merge Sort (cont.)
• Merge Sort Algorithm (cont.):
2. Merging two halves:
1. Access the first item from both halves
2. While neither half is finished
1. Compare the current items of both
2. Copy smaller current item to the output
3. Access next item from that input half
3. Copy any remaining from first half to output
4. Copy any remaining from second half to output
Merge Sort (cont.)
• Merging:
 The key to Merge Sort is merging two sorted lists into one, such that if you
have two lists X (x1x2…xm) and Y(y1y2…yn) the resulting list is
Z(z1z2…zm+n)
 Example:
L1 = { 99,6,86,15 } L2 = { 58,35,86,4,0}
merge(L1, L2) = {0,4,6,15,35,58,86,86,99}
Merge Sort (cont.)
• Example Explanation ::
99 6 86 15 58 35 86 4 0
Merge Sort (cont.)
• Example Explanation : :
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
Merge Sort (cont.)
• Example Explanation :
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
86 1599 6 58 35 86 4 0
Merge Sort (cont.)
• Example Explanation :
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
86 1599 6 58 35 86 4 0
99 6 86 15 58 35 86 4 0
Merge Sort (cont.)
• Example Explanation :
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
86 1599 6 58 35 86 4 0
99 6 86 15 58 35 86 4 0
4 0
Merge Sort (cont.)
• Example Explanation :
99 6 86 15 58 35 86 0 4
4 0Merging
Merge Sort (cont.)
• Example Explanation :
99 6 86 15 58 35 86 0 4
Merging
15 866 99 35 58 0 4 86
Merge Sort (cont.)
• Example Explanation :
Merging
15 866 99 58 35 0 4 86
6 15 86 99 0 4 35 58 86
Merge Sort (cont.)
• Example Explanation :
Merging
6 15 86 99 0 4 35 58 86
0 4 6 15 35 58 86 86 99
Merge Sort (cont.)
• Example Explanation :
99 6 86 15 58 35 86 4 0
0 4 6 15 35 58 86 86 99
Question :
Answer :

More Related Content

PPTX
Insertion sort
PPT
SEARCHING AND SORTING ALGORITHMS
PPTX
sorting and its types
PPTX
Bubble sort | Data structure |
PPTX
Binary search in data structure
PPTX
Linear search-and-binary-search
PPT
Searching in c language
PPTX
Insertion Sorting
Insertion sort
SEARCHING AND SORTING ALGORITHMS
sorting and its types
Bubble sort | Data structure |
Binary search in data structure
Linear search-and-binary-search
Searching in c language
Insertion Sorting

What's hot (20)

PPTX
Insertion sort
PPTX
Data structures
PPSX
Data Structure (Queue)
PPT
Bubble sort
PPT
Data Structure and Algorithms Linked List
PPTX
Linear Search
PPT
Heap sort
PPTX
Quick sort
PPTX
Queue in Data Structure
PPTX
Hashing In Data Structure
PDF
linear search and binary search
PPT
Queue data structure
PPTX
Double Linked List (Algorithm)
PPTX
Bubble Sort Algorithm Presentation
PDF
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPTX
Quick sort
PPTX
heap Sort Algorithm
PDF
Algorithms Lecture 4: Sorting Algorithms I
Insertion sort
Data structures
Data Structure (Queue)
Bubble sort
Data Structure and Algorithms Linked List
Linear Search
Heap sort
Quick sort
Queue in Data Structure
Hashing In Data Structure
linear search and binary search
Queue data structure
Double Linked List (Algorithm)
Bubble Sort Algorithm Presentation
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Quick sort
heap Sort Algorithm
Algorithms Lecture 4: Sorting Algorithms I
Ad

Viewers also liked (7)

PPT
Algorithm: Quick-Sort
PPT
3.8 quicksort
PPT
Safe laparoscopy
PDF
Insertion Sort Algorithm
PPTX
Divide and conquer - Quick sort
PDF
Quicksort: illustrated step-by-step walk through
PPT
Quick sort Algorithm Discussion And Analysis
Algorithm: Quick-Sort
3.8 quicksort
Safe laparoscopy
Insertion Sort Algorithm
Divide and conquer - Quick sort
Quicksort: illustrated step-by-step walk through
Quick sort Algorithm Discussion And Analysis
Ad

Similar to Insertion and merge sort (20)

PPT
Sorting Techniques
PPT
Insert Sort & Merge Sort Using C Programming
PPTX
8.05.Merge_sort.pptxCUYGYUKTGUIJBJKGKUYGFKJBNVKUYV87VYFHHGVTFGU
PPTX
Mergesort
PPTX
10 merge sort
PPTX
Merge sort analysis and its real time applications
PPTX
Daa final
PPTX
insertionsort.pptx
PPTX
2.Problem Solving Techniques and Data Structures.pptx
PPTX
Sorting Algorithms
PPTX
sorting-160810203705.pptx
PDF
Unit v data structure-converted
PPT
DSSchapt13.ppt
PPTX
Merge sort
PPTX
sorting.pptx
PPTX
Insertion Sort
PPTX
Weak 11-12 Sorting update.pptxbhjiiuuuuu
PPTX
DSA- Merge Sort-a sorting technique.pptx
PDF
Algorithms lecture 3
PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
Sorting Techniques
Insert Sort & Merge Sort Using C Programming
8.05.Merge_sort.pptxCUYGYUKTGUIJBJKGKUYGFKJBNVKUYV87VYFHHGVTFGU
Mergesort
10 merge sort
Merge sort analysis and its real time applications
Daa final
insertionsort.pptx
2.Problem Solving Techniques and Data Structures.pptx
Sorting Algorithms
sorting-160810203705.pptx
Unit v data structure-converted
DSSchapt13.ppt
Merge sort
sorting.pptx
Insertion Sort
Weak 11-12 Sorting update.pptxbhjiiuuuuu
DSA- Merge Sort-a sorting technique.pptx
Algorithms lecture 3
Data Structures - Lecture 8 [Sorting Algorithms]

Recently uploaded (20)

PPTX
Micro1New.ppt.pptx the mai themes of micfrobiology
PPTX
Environmental studies, Moudle 3-Environmental Pollution.pptx
PPTX
SE unit 1.pptx aaahshdhajdviwhsiehebeiwheiebeiev
PDF
Unit1 - AIML Chapter 1 concept and ethics
PDF
[jvmmeetup] next-gen integration with apache camel and quarkus.pdf
PPTX
CS6006 - CLOUD COMPUTING - Module - 1.pptx
PDF
Computer organization and architecuture Digital Notes....pdf
PPTX
Environmental studies, Moudle 3-Environmental Pollution.pptx
PPTX
Cisco Network Behaviour dibuywvdsvdtdstydsdsa
PPTX
Module1.pptxrjkeieuekwkwoowkemehehehrjrjrj
PPTX
Agentic Artificial Intelligence (Agentic AI).pptx
PPTX
AI-Reporting for Emerging Technologies(BS Computer Engineering)
PPTX
Solar energy pdf of gitam songa hemant k
PDF
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
PDF
Mechanics of materials week 2 rajeshwari
PDF
Lesson 3 .pdf
PPTX
CT Generations and Image Reconstruction methods
PDF
Beginners-Guide-to-Artificial-Intelligence.pdf
PPT
UNIT-I Machine Learning Essentials for 2nd years
PPTX
chapter 1.pptx dotnet technology introduction
Micro1New.ppt.pptx the mai themes of micfrobiology
Environmental studies, Moudle 3-Environmental Pollution.pptx
SE unit 1.pptx aaahshdhajdviwhsiehebeiwheiebeiev
Unit1 - AIML Chapter 1 concept and ethics
[jvmmeetup] next-gen integration with apache camel and quarkus.pdf
CS6006 - CLOUD COMPUTING - Module - 1.pptx
Computer organization and architecuture Digital Notes....pdf
Environmental studies, Moudle 3-Environmental Pollution.pptx
Cisco Network Behaviour dibuywvdsvdtdstydsdsa
Module1.pptxrjkeieuekwkwoowkemehehehrjrjrj
Agentic Artificial Intelligence (Agentic AI).pptx
AI-Reporting for Emerging Technologies(BS Computer Engineering)
Solar energy pdf of gitam songa hemant k
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
Mechanics of materials week 2 rajeshwari
Lesson 3 .pdf
CT Generations and Image Reconstruction methods
Beginners-Guide-to-Artificial-Intelligence.pdf
UNIT-I Machine Learning Essentials for 2nd years
chapter 1.pptx dotnet technology introduction

Insertion and merge sort