SlideShare a Scribd company logo
ESIT137: Fundamentals of Data Structure
Sanjivani Rural Education Society’s
Sanjivani College of Engineering, Kopargaon-423603
(An Autonomous Institute Affiliated to Savitribai Phule Pune University, Pune)
NACC ‘A’ Grade Accredited, ISO 9001:2015 Certified
Department of Information Technology
(UG Programme - NBAAccredited)
Dr. M.A. Jawale
Professor and Head, Dept. of IT
Searching and Sorting
 Sorting Algorithms
 Insertion Sort,
 Merge Sort.
 Time complexity of all sorting algorithms and their comparison.
 References
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
Insertion Sort
 Insertion sort is a simple sorting algorithm that works similarly to the way you
sort playing cards in your hands.
 The array is virtually split into a sorted and an unsorted part. Values from the
unsorted part are picked and placed in the correct position in the sorted part.
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
Insertion Sort Algorithm
 To sort an array of size N in ascending order iterate over the array and compare
the current element (key) to its predecessor, if the key element is smaller than its
predecessor, compare it to the elements before.
 Move the greater elements one position up to make space for the swapped
element.
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
Working of Insertion Sort algorithm
 Consider an example: arr[]: {12, 11, 13, 5, 6}
 First Pass:
 Initially, the first two elements of the array are compared in insertion sort.
 Here, 12 is greater than 11 hence they are not in the ascending order and 12 is
not at its correct position. Thus, swap 11 and 12.
 So, for now 11 is stored in a sorted sub-array.
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
Continue….
 Second Pass:
 Now, move to the next two elements and compare them.
 Here, 13 is greater than 12, thus both elements seems to be in ascending order,
hence, no swapping will occur. 12 also stored in a sorted sub-array along with
11.
 Third Pass:
 Now, two elements are present in the sorted sub-array which are 11 and 12
 Moving forward to the next two elements which are 13 and 5
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
Continue….
 Both 5 and 13 are not present at their correct place so swap them.
 After swapping, elements 12 and 5 are not sorted, thus swap again.
 Here, again 11 and 5 are not sorted, hence swap again
 Here, 5 is at its correct position
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
Continue….
 Fourth Pass:
 Now, the elements which are present in the sorted sub-array are 5, 11 and 12
 Moving to the next two elements 13 and 6
 Clearly, they are not sorted, thus perform swap between both.
 Now, 6 is smaller than 12, hence, swap again.
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
Continue….
 Now, the elements which are present in the sorted sub-array are 5, 11 and 12
 Here, also swapping makes 11 and 6 unsorted hence, swap again.
 Finally, the array is completely sorted.
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
Continue….
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
Continue….
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
Continue….
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
// function to print an array of size n
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("n");
}
/* Driver program to test insertion sort */
int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
Complexity Analysis of Insertion Sort
 Time Complexity of Insertion Sort:
 The worst-case time complexity of the Insertion sort is O(N2
)
 The average case time complexity of the Insertion sort is O(N2
)
 The time complexity of the best case is O(N).
 Space Complexity of Insertion Sort:
 The auxiliary space complexity of Insertion Sort is O(1)
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
Characteristics of Insertion Sort
 This algorithm is one of the simplest algorithms with a simple implementation
 Basically, Insertion sort is efficient for small data values
 Insertion sort is adaptive in nature, i.e. it is appropriate for data sets that are
already partially sorted.
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
Merge Sort
 Merge sort is defined as a sorting algorithm that works by dividing an array into
smaller subarrays, sorting each subarray, and then merging the sorted subarrays
back together to form the final sorted array.
 In simple terms, we can say that the process of merge sort is to divide the array
into two halves, sort each half, and then merge the sorted halves back together.
This process is repeated until the entire array is sorted.
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
Continue….
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
How does Merge Sort work?
 Merge sort is a recursive algorithm that continuously splits the array in half until
it cannot be further divided i.e., the array has only one element left (an array with
one element is always sorted). Then the sorted subarrays are merged into one
sorted array.
 Lets consider an array arr[] = {38, 27, 43, 10}
Initially divide the array into two equal halves:
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
How does Merge Sort work?
 Lets consider an array arr[] = {38, 27, 43, 10}
 Initially divide the array into two equal halves:
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
Continue….
 These subarrays are further divided into two halves. Now they become array
of unit length that can no longer be divided and array of unit length are
always sorted.
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
Continue….
 These sorted subarrays are merged together, and we get bigger sorted
subarrays.
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
Continue….
 This merging process is continued until the sorted array is built from the
smaller subarrays.
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
Continue….
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
// Create temp arrays
int L[n1], R[n2];
// Copy data to temp arrays L[] and R[]
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
// Merge the temp arrays back into arr[l..r
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
Continue….
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
// Copy the remaining elements of L[],
// if there are any
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
// Copy the remaining elements of R[],
// if there are any
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
// l is for left index and r is right index of the
// sub-array of arr to be sorted
void mergeSort(int arr[], int l, int r)
{
if (l < r) {
int m = l + (r - l) / 2;
// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
Continue….
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
// Function to print an array
void printArray(int A[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", A[i]);
printf("n");
}
// Driver code
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int arr_size = sizeof(arr) / sizeof(arr[0]);
printf("Given array is n");
printArray(arr, arr_size);
mergeSort(arr, 0, arr_size - 1);
printf("nSorted array is n");
printArray(arr, arr_size);
return 0;
}
Complexity Analysis of Merge Sort
 Time Complexity: O(N log(N)), Merge Sort is a recursive algorithm and time
complexity can be expressed as following recurrence relation.
T(n) = 2T(n/2) + θ(n)
 Auxiliary Space: O(N), In merge sort all elements are copied into an auxiliary
array. So N auxiliary space is required for merge sort.
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
Applications of Merge Sort
 Sorting large datasets: Merge sort is particularly well-suited for sorting large
datasets due to its guaranteed worst-case time complexity of O(n log n).
 External sorting: Merge sort is commonly used in external sorting, where the
data to be sorted is too large to fit into memory.
 Custom sorting: Merge sort can be adapted to handle different input
distributions, such as partially sorted, nearly sorted, or completely unsorted data.
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
Advantages of Merge Sort
 Stability: Merge sort is a stable sorting algorithm, which means it maintains the
relative order of equal elements in the input array.
 Guaranteed worst-case performance: Merge sort has a worst-case time
complexity of O(NlogN), which means it performs well even on large datasets.
 Parallelizable: Merge sort is a naturally parallelizable algorithm, which means it
can be easily parallelized to take advantage of multiple processors or threads.
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
Drawbacks of Merge Sort
 Space complexity: Merge sort requires additional memory to store the merged
sub-arrays during the sorting process.
 Not in-place: Merge sort is not an in-place sorting algorithm, which means it
requires additional memory to store the sorted data. This can be a disadvantage in
applications where memory usage is a concern.
 Not always optimal for small datasets: For small datasets, Merge sort has a
higher time complexity than some other sorting algorithms, such as insertion sort.
This can result in slower performance for very small datasets.
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
Comparison among Bubble Sort, Selection Sort and Insertion Sort
 Bubble Sort:
 Time complexity: O(n2) in the worst and average cases, O(n) in the best case
(when the input array is already sorted)
 Space complexity: O(1)
 Basic idea: Iterate through the array repeatedly, comparing adjacent pairs of
elements and swapping them if they are in the wrong order. Repeat until the
array is fully sorted..
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
Comparison among Bubble Sort, Selection Sort and Insertion Sort
 Selection Sort:
 Time complexity: O(n2
) in all cases (worst, average, and best)
 Space complexity: O(1)
 Basic idea: Find the minimum element in the unsorted portion of the array and
swap it with the first unsorted element. Repeat until the array is fully sorted.
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
Comparison among Bubble Sort, Selection Sort and Insertion Sort
 Insertion Sort:
 Time complexity: O(n2
) in the worst and average cases, O(n) in the best case
(when the input array is already sorted)
 Space complexity: O(1)
 Basic idea: Build up a sorted subarray from left to right by inserting each new
element into its correct position in the subarray. Repeat until the array is fully
sorted.
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
Reference
1. Richard F. Gilberg & Behrouz A. Forouzan, “Data Structures: A Pseudocode
Approach with C, Second Edition”, Cengage Learning.
2. Ellis Horowitz, Sartaj Sahani, Susan Anderson-Freed “Fundamentals of Data
Structures in C”, Universities Press, 2008.
Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology

More Related Content

What's hot (20)

ITE Course Unit 1Productivity Tools For An Engineers
ITE Course Unit 1Productivity Tools For An Engineers
NitinShelake4
 
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
prasadmutkule1
 
list.pptx
list.pptx
SwapnaliGawali5
 
ESIT135: Unit 3 Topic: functions in python
ESIT135: Unit 3 Topic: functions in python
SwapnaliGawali5
 
Unit4-Basic Concepts and methods of Dictionary.pptx
Unit4-Basic Concepts and methods of Dictionary.pptx
SwapnaliGawali5
 
Unit-4 Basic Concepts of Tuple in Python .pptx
Unit-4 Basic Concepts of Tuple in Python .pptx
SwapnaliGawali5
 
ESIT135 : Unit 1 Python Basics Concepts
ESIT135 : Unit 1 Python Basics Concepts
SwapnaliGawali5
 
JAVASRIPT and PHP (Hypertext Preprocessor)
JAVASRIPT and PHP (Hypertext Preprocessor)
shelakenitinit
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptx
skilljiolms
 
VCE Unit 01 (1).pptx
VCE Unit 01 (1).pptx
skilljiolms
 
VCE - UNIT-II.pptx
VCE - UNIT-II.pptx
skilljiolms
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Albin562191
 
Circular Queue data structure
Circular Queue data structure
Dhananjaysinh Jhala
 
Data Structure (Queue)
Data Structure (Queue)
Adam Mukharil Bachtiar
 
VCE Unit 04vv.pptx
VCE Unit 04vv.pptx
skilljiolms
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
03446940736
 
Linear Search
Linear Search
SWATHIR72
 
Queue Data Structure
Queue Data Structure
Zidny Nafan
 
Deletion operation in array(ds)
Deletion operation in array(ds)
chauhankapil
 
Python programming : Strings
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
ITE Course Unit 1Productivity Tools For An Engineers
ITE Course Unit 1Productivity Tools For An Engineers
NitinShelake4
 
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
prasadmutkule1
 
ESIT135: Unit 3 Topic: functions in python
ESIT135: Unit 3 Topic: functions in python
SwapnaliGawali5
 
Unit4-Basic Concepts and methods of Dictionary.pptx
Unit4-Basic Concepts and methods of Dictionary.pptx
SwapnaliGawali5
 
Unit-4 Basic Concepts of Tuple in Python .pptx
Unit-4 Basic Concepts of Tuple in Python .pptx
SwapnaliGawali5
 
ESIT135 : Unit 1 Python Basics Concepts
ESIT135 : Unit 1 Python Basics Concepts
SwapnaliGawali5
 
JAVASRIPT and PHP (Hypertext Preprocessor)
JAVASRIPT and PHP (Hypertext Preprocessor)
shelakenitinit
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptx
skilljiolms
 
VCE Unit 01 (1).pptx
VCE Unit 01 (1).pptx
skilljiolms
 
VCE - UNIT-II.pptx
VCE - UNIT-II.pptx
skilljiolms
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Albin562191
 
VCE Unit 04vv.pptx
VCE Unit 04vv.pptx
skilljiolms
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
03446940736
 
Linear Search
Linear Search
SWATHIR72
 
Queue Data Structure
Queue Data Structure
Zidny Nafan
 
Deletion operation in array(ds)
Deletion operation in array(ds)
chauhankapil
 

Similar to Insertion Sort, Merge Sort. Time complexity of all sorting algorithms and their comparison. (20)

sorting-160810203705.pptx
sorting-160810203705.pptx
VarchasvaTiwari2
 
How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...
How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...
PhD Assistance
 
Searching,sorting
Searching,sorting
LavanyaJ28
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
ch16.pptx
ch16.pptx
lordaragorn2
 
ch16 (1).pptx
ch16 (1).pptx
lordaragorn2
 
INDEX SORT
INDEX SORT
Waqas Tariq
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sort
Shanmuganathan C
 
Search and Sort algorithms. Bubble, Insertion, Selection.
Search and Sort algorithms. Bubble, Insertion, Selection.
FerryKemperman
 
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
Kanupriya731200
 
How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...
How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...
PhD Assistance
 
DSSchapt13.ppt
DSSchapt13.ppt
Safia Kanwal
 
03_sorting and it's types with example .ppt
03_sorting and it's types with example .ppt
vanshii9976
 
03_sorting123456789454545454545444543.ppt
03_sorting123456789454545454545444543.ppt
ssuser7b9bda1
 
Insertion Sorting
Insertion Sorting
FarihaHabib123
 
SORT AND SEARCH ARRAY WITH WITH C++.pptx
SORT AND SEARCH ARRAY WITH WITH C++.pptx
narifmsit18seecs
 
Insertion and merge sort
Insertion and merge sort
Preetham Devisetty
 
Data structure chapter 2 Time complexity of known algorithms.pptx
Data structure chapter 2 Time complexity of known algorithms.pptx
fikadumeuedu
 
Sorting
Sorting
Gopi Saiteja
 
Chapter 14 Searching and Sorting
Chapter 14 Searching and Sorting
MuhammadBakri13
 
How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...
How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...
PhD Assistance
 
Searching,sorting
Searching,sorting
LavanyaJ28
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sort
Shanmuganathan C
 
Search and Sort algorithms. Bubble, Insertion, Selection.
Search and Sort algorithms. Bubble, Insertion, Selection.
FerryKemperman
 
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
Kanupriya731200
 
How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...
How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...
PhD Assistance
 
03_sorting and it's types with example .ppt
03_sorting and it's types with example .ppt
vanshii9976
 
03_sorting123456789454545454545444543.ppt
03_sorting123456789454545454545444543.ppt
ssuser7b9bda1
 
SORT AND SEARCH ARRAY WITH WITH C++.pptx
SORT AND SEARCH ARRAY WITH WITH C++.pptx
narifmsit18seecs
 
Data structure chapter 2 Time complexity of known algorithms.pptx
Data structure chapter 2 Time complexity of known algorithms.pptx
fikadumeuedu
 
Chapter 14 Searching and Sorting
Chapter 14 Searching and Sorting
MuhammadBakri13
 
Ad

Recently uploaded (20)

Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
Water demand - Types , variations and WDS
Water demand - Types , variations and WDS
dhanashree78
 
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
CenterEnamel
 
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
Journal of Soft Computing in Civil Engineering
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
djiceramil
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
First Come First Serve Scheduling in real time operating system.pptx
First Come First Serve Scheduling in real time operating system.pptx
KavitaBagewadi2
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
samueljackson3773
 
22PCOAM16 _ML_Unit 3 Notes & Question bank
22PCOAM16 _ML_Unit 3 Notes & Question bank
Guru Nanak Technical Institutions
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
Présentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptx
KHADIJAESSAKET
 
grade 9 science q1 quiz.pptx science quiz
grade 9 science q1 quiz.pptx science quiz
norfapangolima
 
Understanding Amplitude Modulation : A Guide
Understanding Amplitude Modulation : A Guide
CircuitDigest
 
3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt
engaash9
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
Water demand - Types , variations and WDS
Water demand - Types , variations and WDS
dhanashree78
 
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
CenterEnamel
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
djiceramil
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
First Come First Serve Scheduling in real time operating system.pptx
First Come First Serve Scheduling in real time operating system.pptx
KavitaBagewadi2
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
samueljackson3773
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
Présentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptx
KHADIJAESSAKET
 
grade 9 science q1 quiz.pptx science quiz
grade 9 science q1 quiz.pptx science quiz
norfapangolima
 
Understanding Amplitude Modulation : A Guide
Understanding Amplitude Modulation : A Guide
CircuitDigest
 
3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt
engaash9
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Ad

Insertion Sort, Merge Sort. Time complexity of all sorting algorithms and their comparison.

  • 1. ESIT137: Fundamentals of Data Structure Sanjivani Rural Education Society’s Sanjivani College of Engineering, Kopargaon-423603 (An Autonomous Institute Affiliated to Savitribai Phule Pune University, Pune) NACC ‘A’ Grade Accredited, ISO 9001:2015 Certified Department of Information Technology (UG Programme - NBAAccredited) Dr. M.A. Jawale Professor and Head, Dept. of IT
  • 2. Searching and Sorting  Sorting Algorithms  Insertion Sort,  Merge Sort.  Time complexity of all sorting algorithms and their comparison.  References Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 3. Insertion Sort  Insertion sort is a simple sorting algorithm that works similarly to the way you sort playing cards in your hands.  The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed in the correct position in the sorted part. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 4. Insertion Sort Algorithm  To sort an array of size N in ascending order iterate over the array and compare the current element (key) to its predecessor, if the key element is smaller than its predecessor, compare it to the elements before.  Move the greater elements one position up to make space for the swapped element. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 5. Working of Insertion Sort algorithm  Consider an example: arr[]: {12, 11, 13, 5, 6}  First Pass:  Initially, the first two elements of the array are compared in insertion sort.  Here, 12 is greater than 11 hence they are not in the ascending order and 12 is not at its correct position. Thus, swap 11 and 12.  So, for now 11 is stored in a sorted sub-array. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 6. Continue….  Second Pass:  Now, move to the next two elements and compare them.  Here, 13 is greater than 12, thus both elements seems to be in ascending order, hence, no swapping will occur. 12 also stored in a sorted sub-array along with 11.  Third Pass:  Now, two elements are present in the sorted sub-array which are 11 and 12  Moving forward to the next two elements which are 13 and 5 Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 7. Continue….  Both 5 and 13 are not present at their correct place so swap them.  After swapping, elements 12 and 5 are not sorted, thus swap again.  Here, again 11 and 5 are not sorted, hence swap again  Here, 5 is at its correct position Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 8. Continue….  Fourth Pass:  Now, the elements which are present in the sorted sub-array are 5, 11 and 12  Moving to the next two elements 13 and 6  Clearly, they are not sorted, thus perform swap between both.  Now, 6 is smaller than 12, hence, swap again. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 9. Continue….  Now, the elements which are present in the sorted sub-array are 5, 11 and 12  Here, also swapping makes 11 and 6 unsorted hence, swap again.  Finally, the array is completely sorted. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 10. Continue…. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 11. Continue…. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology /* Function to sort an array using insertion sort*/ void insertionSort(int arr[], int n) { int i, key, j; for (i = 1; i < n; i++) { key = arr[i]; j = i - 1; /* Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position */ while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = key; } }
  • 12. Continue…. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology // function to print an array of size n void printArray(int arr[], int n) { int i; for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("n"); } /* Driver program to test insertion sort */ int main() { int arr[] = { 12, 11, 13, 5, 6 }; int n = sizeof(arr) / sizeof(arr[0]); insertionSort(arr, n); printArray(arr, n); return 0; }
  • 13. Complexity Analysis of Insertion Sort  Time Complexity of Insertion Sort:  The worst-case time complexity of the Insertion sort is O(N2 )  The average case time complexity of the Insertion sort is O(N2 )  The time complexity of the best case is O(N).  Space Complexity of Insertion Sort:  The auxiliary space complexity of Insertion Sort is O(1) Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 14. Characteristics of Insertion Sort  This algorithm is one of the simplest algorithms with a simple implementation  Basically, Insertion sort is efficient for small data values  Insertion sort is adaptive in nature, i.e. it is appropriate for data sets that are already partially sorted. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 15. Merge Sort  Merge sort is defined as a sorting algorithm that works by dividing an array into smaller subarrays, sorting each subarray, and then merging the sorted subarrays back together to form the final sorted array.  In simple terms, we can say that the process of merge sort is to divide the array into two halves, sort each half, and then merge the sorted halves back together. This process is repeated until the entire array is sorted. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 16. Continue…. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 17. How does Merge Sort work?  Merge sort is a recursive algorithm that continuously splits the array in half until it cannot be further divided i.e., the array has only one element left (an array with one element is always sorted). Then the sorted subarrays are merged into one sorted array.  Lets consider an array arr[] = {38, 27, 43, 10} Initially divide the array into two equal halves: Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 18. How does Merge Sort work?  Lets consider an array arr[] = {38, 27, 43, 10}  Initially divide the array into two equal halves: Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 19. Continue….  These subarrays are further divided into two halves. Now they become array of unit length that can no longer be divided and array of unit length are always sorted. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 20. Continue….  These sorted subarrays are merged together, and we get bigger sorted subarrays. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 21. Continue….  This merging process is continued until the sorted array is built from the smaller subarrays. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 22. Continue…. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology // Merges two subarrays of arr[]. // First subarray is arr[l..m] // Second subarray is arr[m+1..r] void merge(int arr[], int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; // Create temp arrays int L[n1], R[n2]; // Copy data to temp arrays L[] and R[] for (i = 0; i < n1; i++) L[i] = arr[l + i]; for (j = 0; j < n2; j++) R[j] = arr[m + 1 + j]; // Merge the temp arrays back into arr[l..r i = 0; j = 0; k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; }
  • 23. Continue…. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology // Copy the remaining elements of L[], // if there are any while (i < n1) { arr[k] = L[i]; i++; k++; } // Copy the remaining elements of R[], // if there are any while (j < n2) { arr[k] = R[j]; j++; k++; } } // l is for left index and r is right index of the // sub-array of arr to be sorted void mergeSort(int arr[], int l, int r) { if (l < r) { int m = l + (r - l) / 2; // Sort first and second halves mergeSort(arr, l, m); mergeSort(arr, m + 1, r); merge(arr, l, m, r); } }
  • 24. Continue…. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology // Function to print an array void printArray(int A[], int size) { int i; for (i = 0; i < size; i++) printf("%d ", A[i]); printf("n"); } // Driver code int main() { int arr[] = { 12, 11, 13, 5, 6, 7 }; int arr_size = sizeof(arr) / sizeof(arr[0]); printf("Given array is n"); printArray(arr, arr_size); mergeSort(arr, 0, arr_size - 1); printf("nSorted array is n"); printArray(arr, arr_size); return 0; }
  • 25. Complexity Analysis of Merge Sort  Time Complexity: O(N log(N)), Merge Sort is a recursive algorithm and time complexity can be expressed as following recurrence relation. T(n) = 2T(n/2) + θ(n)  Auxiliary Space: O(N), In merge sort all elements are copied into an auxiliary array. So N auxiliary space is required for merge sort. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 26. Applications of Merge Sort  Sorting large datasets: Merge sort is particularly well-suited for sorting large datasets due to its guaranteed worst-case time complexity of O(n log n).  External sorting: Merge sort is commonly used in external sorting, where the data to be sorted is too large to fit into memory.  Custom sorting: Merge sort can be adapted to handle different input distributions, such as partially sorted, nearly sorted, or completely unsorted data. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 27. Advantages of Merge Sort  Stability: Merge sort is a stable sorting algorithm, which means it maintains the relative order of equal elements in the input array.  Guaranteed worst-case performance: Merge sort has a worst-case time complexity of O(NlogN), which means it performs well even on large datasets.  Parallelizable: Merge sort is a naturally parallelizable algorithm, which means it can be easily parallelized to take advantage of multiple processors or threads. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 28. Drawbacks of Merge Sort  Space complexity: Merge sort requires additional memory to store the merged sub-arrays during the sorting process.  Not in-place: Merge sort is not an in-place sorting algorithm, which means it requires additional memory to store the sorted data. This can be a disadvantage in applications where memory usage is a concern.  Not always optimal for small datasets: For small datasets, Merge sort has a higher time complexity than some other sorting algorithms, such as insertion sort. This can result in slower performance for very small datasets. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 29. Comparison among Bubble Sort, Selection Sort and Insertion Sort  Bubble Sort:  Time complexity: O(n2) in the worst and average cases, O(n) in the best case (when the input array is already sorted)  Space complexity: O(1)  Basic idea: Iterate through the array repeatedly, comparing adjacent pairs of elements and swapping them if they are in the wrong order. Repeat until the array is fully sorted.. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 30. Comparison among Bubble Sort, Selection Sort and Insertion Sort  Selection Sort:  Time complexity: O(n2 ) in all cases (worst, average, and best)  Space complexity: O(1)  Basic idea: Find the minimum element in the unsorted portion of the array and swap it with the first unsorted element. Repeat until the array is fully sorted. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 31. Comparison among Bubble Sort, Selection Sort and Insertion Sort  Insertion Sort:  Time complexity: O(n2 ) in the worst and average cases, O(n) in the best case (when the input array is already sorted)  Space complexity: O(1)  Basic idea: Build up a sorted subarray from left to right by inserting each new element into its correct position in the subarray. Repeat until the array is fully sorted. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology
  • 32. Reference 1. Richard F. Gilberg & Behrouz A. Forouzan, “Data Structures: A Pseudocode Approach with C, Second Edition”, Cengage Learning. 2. Ellis Horowitz, Sartaj Sahani, Susan Anderson-Freed “Fundamentals of Data Structures in C”, Universities Press, 2008. Unit-II: Part-III Searching and Sorting Dr. Madhuri Jawale Department of Information Technology