SlideShare a Scribd company logo
1
Insertion Sort
2
Insertion Sort
• Suppose we know how to insert a new element x in its proper place
in an already sorted array A of size k, to get a new sorted array of
size k+1
• Use this to sort the given array A of size n as follows:
– Insert A[1] in the sorted array A[0]. So now A[0],A[1] are sorted
– Insert A[2] in the sorted array A[0],A[1]. So now
A[0],A[1],A[2] are sorted
– Insert A[3] in the sorted array A[0],A[1],A[2]. So now
A[0],A[1],A[2],A[3] are sorted
– …..
– Insert A[i] in the sorted array A[0],A[1],…,A[i-1]. So now
A[0],A[1],…A[i] are sorted
– Continue until i = n-1 (outer loop)
3
How to do the first step
• Compare x with A[k-1] (the last element)
– If x ≥ A[k-1], we can make A[k] = x (as x is the max of
all the elements)
– If x < A[k-1], put A[k] = A[k-1] to create a hole in the k-
th position, put x there
• Now repeat by comparing x with A[k-2] (inserting x in its
proper place in the sorted subarray A[0],A[1],…A[k-1] of
k-2 elements)
• The value x bubbles to the left until it finds an element A[i]
such that x ≥ A[i]
• No need to compare any more as all elements A[0], A[1],
A[i] are less than x
4
Example of first step
5 7 11 13 20 22
A Insert x = 15
5
Example of first step
5 7 11 13 20 22
5 7 11 13 20 15 22
A Insert x = 15
Compare with 22. x < 22, so move 22 right
6
Example of first step
5 7 11 13 20 22
5 7 11 13 20 15 22
5 7 11 13 15 20 22
A Insert x = 15
Compare with 22. x < 22, so move 22 right
Compare with 20. x < 20, so move 20 right
7
Example of first step
5 7 11 13 20 22
5 7 11 13 20 15 22
5 7 11 13 15 20 22
5 7 11 13 15 20 22
A Insert x = 15
Compare with 22. x < 22, so move 22 right
Compare with 20. x < 20, so move 20 right
Compare with 13. x > 13, so stop
A
8
Sort using the insertion
7 5 13 11 22 20
5 7 13 11 22 20
5 7 13 11 22 20
5 7 11 13 22 20
A
Insert 5 in 7
Insert 13 in 5, 7
Insert 11 in 5, 7, 13
Insert 22 in 5, 7, 11, 13
Insert 20 in 5, 7, 11, 13, 22
5 7 11 13 20 22
5 7 11 13 22 20
9
Insertion Sort Code
void InsertionSort (int A[ ], int size)
{
int i, j, item;
for (i=1; i<size; i++)
{ /* Insert the element in A[i] */
item = A[i] ;
for (j = i-1; j >= 0; j--)
if (item < A[j])
{ /* push elements down*/
A[j+1] = A[j];
A[j] = item ; /* can do this on
}
else break; /*inserted, exit loop */
}
}
10
Look at the so
8
2
9
4
7
6
2
1
5
i = 1:: 2, 9, 4, 7
i = 2:: 9, 2, 4, 7
i = 3:: 9, 4, 2, 7
i = 4:: 9, 7, 4, 2
i = 5:: 9, 7, 6, 4
i = 6:: 9, 7, 6, 4
i = 7:: 9, 7, 6, 4
Result = 9, 7, 6, 5
void InsertionSort (int A[ ], int size) {
int i,j, item;
for (i=1; i<size; i++) {
printf("i = %d:: ",i);
for (j=0;j<size;j++) printf("%d, ",A[j]);
printf("n"); item = A[i] ;
for (j=i-1; j>=0; j--)
if (item > A[j])
{ A[j+1] = A[j]; A[j] = item ; }
else break;
}
int main() {
int X[100], i, size;
scanf("%d",&size);
for (i=0;i<size;i++) scanf("%d",&X[i]);
InsertionSort(X,size);
printf("Result = ");
for (i=0;i<size;i++) printf("%d, ",X[i]);
printf("n"); return 0;
}
11
Merge Sort
• Review of Sorting
• Merge Sort
12
Sorting Algorithms
• Selection Sort uses a priority queue P implemented
with an unsorted sequence:
– Phase 1: the insertion of an item into P takes O(1) time;
overall O(n) time for inserting n items.
– Phase 2: removing an item takes time proportional to the
number of elements in P, which is O(n): overall O(n2)
– Time Complexity: O(n2)
13
Sorting Algorithms (cont.)
• Insertion Sort is performed on a priority queue P which is a
sorted sequence:
– Phase 1: the first insertItem takes O(1), the second O(2), until the
last insertItem takes O(n): overall O(n2)
– Phase 2: removing an item takes O(1) time; overall O(n).
– Time Complexity: O(n2)
• Heap Sort uses a priority queue K which is a heap.
– insertItem and removeMin each take O(logk), k being the
number of elements in the heap at a given time.
– Phase 1: n elements inserted: O (nlogn) time
– Phase 2: n elements removed: O (nlogn) time.
– Time Complexity: O (nlog n)
14
Divide-and-Conquer
• Divide and Conquer is more than just a military strategy; it is also a
method of algorithm design that has created such efficient algorithms
as Merge Sort.
• In terms or algorithms, this method has three distinct steps:
– Divide: If the input size is too large to deal with in a straightforward manner,
divide the data into two or more disjoint subsets.
– Recur: Use divide and conquer to solve the subproblems associated with the
data subsets.
– Conquer: Take the solutions to the subproblems and “merge” these solutions
into a solution for the original problem.
15
Merge-Sort
• Algorithm:
– Divide: If S has at leas two elements (nothing needs to be done if S has zero or
one elements), remove all the elements from S and put them into two sequences,
S1 and S2, each containing about half of the elements of S. (i.e. S1 contains the
first n/2 elements and S2 contains the remaining n/2 elements.
– Recur: Recursive sort sequences S1 and S2.
– Conquer: Put back the elements into S by merging the sorted sequences S1 and
S2 into a unique sorted sequence.
• Merge Sort Tree:
– Take a binary tree T
– Each node of T represents a recursive call of the merge sort algorithm.
– We associate with each node v of T a the set of input passed to the invocation v
represents.
– The external nodes are associated with individual elements of S, upon which no
recursion is called.
16
Merge-Sort
17
Merge-Sort(cont.)
18
Merge-Sort (cont.)
19
Merge-Sort (cont.)
20
Merge-Sort (cont.)
21
Merge-Sort (cont.)
22
Merge-Sort (cont.)
23
Merge-Sort(cont.)
24
Merge-Sort (cont.)
25
Merge-Sort (cont.)
26
Merge-Sort (cont.)
27
Merging Two Sequences
28
Merging Two Sequences (cont.)
29
Merging Two Sequences (cont.)
30
Merging Two Sequences (cont.)
31
Merging Two Sequences (cont.)
32
Merging Two Sequences (cont.)
Merging two
sorted arrays
Splitting arrays
Example
3 12 -5 6 72 21 -7 45
x:
3 12 -5 6 72 21 -7 45
3 12 -5 6 72 21 -7 45
3 12 -5 6 72 21 -7 45
3 12 -5 6 21 72 -7 45
-5 3 6 12 -7 21 45 72
-7 -5 3
-7 -5 3 6 12 21 45 72
-5 3 6 12 -7 21 45 72
Sorted Arr-1 Sorted Arr-2
i=j=k=0
m
n
i j
k
-7
j
k
-5
i
k
3
k
i
6
k
i
12
k
21
j
k
45
j
k
72
Merge Sort C program
#include<stdio.h>
void mergesort(int a[],int i,int j);
void merge(int a[],int i1,int j1,int i2,int j2);
int main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("nSorted array is :");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
void mergesort(int a[],int i,int j)
{
int mid;
if(i<j) {
mid=(i+j)/2;
/* left recursion */
mergesort(a,i,mid);
/* right recursion */
mergesort(a,mid+1,j);
/* merging of two sorted sub-arrays */
merge(a,i,mid,mid+1,j);
}
}
Merge Sort C program
void merge(int a[],int i1,int j1,int i2,int j2)
{
int temp[50]; //array used for merging
int i=i1,j=i2,k=0;
while(i<=j1 && j<=j2) //while elements in both lists
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=j1) //copy remaining elements of the first list
temp[k++]=a[i++];
while(j<=j2) //copy remaining elements of the second list
temp[k++]=a[j++];
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j]; //Transfer elements from temp[] back to a[]
}
Splitting Trace
-56 23 43 -5 -3 0 123 -35 87 56 75 80
-56 -35 -5 -3 0 23 43 56 75 80 87 123
-56 23 43
23 43
-56 23 43 -5 -3 0 123 -35 87 56 75 80
-56 23 43 -5 -3 0
-3 0
-3 0
123 -35 87
-35 87
123 -35 87 56 75 80
56
75 80
75 80
Worst Case: O(n.log(n))
Ad

Recommended

Estrutura de Dados - Aula 12 - Pesquisa de Dados (Sequencial e Binária)
Estrutura de Dados - Aula 12 - Pesquisa de Dados (Sequencial e Binária)
Leinylson Fontinele
 
อุปกรณ์รับและแสดงผล
อุปกรณ์รับและแสดงผล
skiats
 
Manual-de-php
Manual-de-php
diogoa21
 
A Linguagem sql
A Linguagem sql
Centro Paula Souza
 
OCL: Object Constraint Language
OCL: Object Constraint Language
elliando dias
 
Tree in data structure
Tree in data structure
ghhgj jhgh
 
Microsoft Access
Microsoft Access
Wagner Luiz Fernandes
 
Exemplo de Plano Estratégico de TI - MEC
Exemplo de Plano Estratégico de TI - MEC
Fernando Palma
 
01 introduction to data mining
01 introduction to data mining
phakhwan22
 
Arquitetura de Computadores: Assembly
Arquitetura de Computadores: Assembly
Elaine Cecília Gatto
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
Aakash deep Singhal
 
Apostila de Banco de Dados
Apostila de Banco de Dados
info_cimol
 
Logica programacao python-slides
Logica programacao python-slides
ronaldo ramos
 
Aula 5 - Dicionário de Dados
Aula 5 - Dicionário de Dados
Janynne Gomes
 
Introduction to Database, Purpose of Data, Data models, Components of Database
Introduction to Database, Purpose of Data, Data models, Components of Database
kasthurimukila
 
Lecture 2a arrays
Lecture 2a arrays
Victor Palmar
 
Algoritmo recursivo
Algoritmo recursivo
Carlos Rodrigo de Araujo
 
Trees data structure
Trees data structure
Sumit Gupta
 
Data Structure (Tree)
Data Structure (Tree)
Adam Mukharil Bachtiar
 
Trees and graphs
Trees and graphs
Lokesh Singrol
 
Data structures using C
Data structures using C
Prof. Dr. K. Adisesha
 
Estrutura de Dados - Aula 15 - Pesquisa de Dados (Árvore de Pesquisa)
Estrutura de Dados - Aula 15 - Pesquisa de Dados (Árvore de Pesquisa)
Leinylson Fontinele
 
Aula 02 - Tipos de dados, Variáveis, Constantes e Operadores Aritméticos
Aula 02 - Tipos de dados, Variáveis, Constantes e Operadores Aritméticos
Messias Batista
 
AVL Tree Data Structure
AVL Tree Data Structure
Afaq Mansoor Khan
 
Linguagem C - Strings
Linguagem C - Strings
Elaine Cecília Gatto
 
Sistema acadêmico
Sistema acadêmico
Leinylson Fontinele
 
Cheat Sheet for Machine Learning in Python: Scikit-learn
Cheat Sheet for Machine Learning in Python: Scikit-learn
Karlijn Willems
 
Anahtarlar Ve Tablolar Arasi Iliskiler
Anahtarlar Ve Tablolar Arasi Iliskiler
Sevdanur Genc
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sort
Shanmuganathan C
 
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
Kanupriya731200
 

More Related Content

What's hot (20)

01 introduction to data mining
01 introduction to data mining
phakhwan22
 
Arquitetura de Computadores: Assembly
Arquitetura de Computadores: Assembly
Elaine Cecília Gatto
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
Aakash deep Singhal
 
Apostila de Banco de Dados
Apostila de Banco de Dados
info_cimol
 
Logica programacao python-slides
Logica programacao python-slides
ronaldo ramos
 
Aula 5 - Dicionário de Dados
Aula 5 - Dicionário de Dados
Janynne Gomes
 
Introduction to Database, Purpose of Data, Data models, Components of Database
Introduction to Database, Purpose of Data, Data models, Components of Database
kasthurimukila
 
Lecture 2a arrays
Lecture 2a arrays
Victor Palmar
 
Algoritmo recursivo
Algoritmo recursivo
Carlos Rodrigo de Araujo
 
Trees data structure
Trees data structure
Sumit Gupta
 
Data Structure (Tree)
Data Structure (Tree)
Adam Mukharil Bachtiar
 
Trees and graphs
Trees and graphs
Lokesh Singrol
 
Data structures using C
Data structures using C
Prof. Dr. K. Adisesha
 
Estrutura de Dados - Aula 15 - Pesquisa de Dados (Árvore de Pesquisa)
Estrutura de Dados - Aula 15 - Pesquisa de Dados (Árvore de Pesquisa)
Leinylson Fontinele
 
Aula 02 - Tipos de dados, Variáveis, Constantes e Operadores Aritméticos
Aula 02 - Tipos de dados, Variáveis, Constantes e Operadores Aritméticos
Messias Batista
 
AVL Tree Data Structure
AVL Tree Data Structure
Afaq Mansoor Khan
 
Linguagem C - Strings
Linguagem C - Strings
Elaine Cecília Gatto
 
Sistema acadêmico
Sistema acadêmico
Leinylson Fontinele
 
Cheat Sheet for Machine Learning in Python: Scikit-learn
Cheat Sheet for Machine Learning in Python: Scikit-learn
Karlijn Willems
 
Anahtarlar Ve Tablolar Arasi Iliskiler
Anahtarlar Ve Tablolar Arasi Iliskiler
Sevdanur Genc
 
01 introduction to data mining
01 introduction to data mining
phakhwan22
 
Arquitetura de Computadores: Assembly
Arquitetura de Computadores: Assembly
Elaine Cecília Gatto
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
Aakash deep Singhal
 
Apostila de Banco de Dados
Apostila de Banco de Dados
info_cimol
 
Logica programacao python-slides
Logica programacao python-slides
ronaldo ramos
 
Aula 5 - Dicionário de Dados
Aula 5 - Dicionário de Dados
Janynne Gomes
 
Introduction to Database, Purpose of Data, Data models, Components of Database
Introduction to Database, Purpose of Data, Data models, Components of Database
kasthurimukila
 
Trees data structure
Trees data structure
Sumit Gupta
 
Estrutura de Dados - Aula 15 - Pesquisa de Dados (Árvore de Pesquisa)
Estrutura de Dados - Aula 15 - Pesquisa de Dados (Árvore de Pesquisa)
Leinylson Fontinele
 
Aula 02 - Tipos de dados, Variáveis, Constantes e Operadores Aritméticos
Aula 02 - Tipos de dados, Variáveis, Constantes e Operadores Aritméticos
Messias Batista
 
Cheat Sheet for Machine Learning in Python: Scikit-learn
Cheat Sheet for Machine Learning in Python: Scikit-learn
Karlijn Willems
 
Anahtarlar Ve Tablolar Arasi Iliskiler
Anahtarlar Ve Tablolar Arasi Iliskiler
Sevdanur Genc
 

Similar to Insert Sort & Merge Sort Using C Programming (20)

search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sort
Shanmuganathan C
 
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
Kanupriya731200
 
Introduction to Algorithms
Introduction to Algorithms
pppepito86
 
Data Structure Sorting
Data Structure Sorting
Muhazzab Chouhadry
 
PEMDAS - The Proper Order of Mathematical Operations
PEMDAS - The Proper Order of Mathematical Operations
Mark (Mong) Montances
 
2.2 add real numbers day 1-2
2.2 add real numbers day 1-2
bweldon
 
Chapter 2
Chapter 2
jenpenbrad
 
8.05.Merge_sort.pptxCUYGYUKTGUIJBJKGKUYGFKJBNVKUYV87VYFHHGVTFGU
8.05.Merge_sort.pptxCUYGYUKTGUIJBJKGKUYGFKJBNVKUYV87VYFHHGVTFGU
denveramoson
 
Unit 7 sorting
Unit 7 sorting
kalyanineve
 
Sorting techniques
Sorting techniques
JayeshGadhave1
 
Sorting Methods.pptx
Sorting Methods.pptx
Bharati Vidyapeeth COE, Navi Mumbai
 
BASIC OF ALGORITHM AND MATHEMATICS STUDENTS
BASIC OF ALGORITHM AND MATHEMATICS STUDENTS
jainyshah20
 
Sorting ppt
Sorting ppt
Hassan Mustafa
 
Sortings .pptx
Sortings .pptx
MuhammadSheraz836877
 
Different Searching and Sorting Methods.pptx
Different Searching and Sorting Methods.pptx
Minakshee Patil
 
Numerical Linear Algebra in digital image processing
Numerical Linear Algebra in digital image processing
Indra Hermawan
 
Sorting
Sorting
Shaista Qadir
 
Quick sort.pptx
Quick sort.pptx
Anbarasan Radhakrishnan R
 
Linear-Programming-by-Simplex-Method.pptx
Linear-Programming-by-Simplex-Method.pptx
teresemondejar
 
2.ppt
2.ppt
TapodhirAcharjee2
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sort
Shanmuganathan C
 
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
Kanupriya731200
 
Introduction to Algorithms
Introduction to Algorithms
pppepito86
 
PEMDAS - The Proper Order of Mathematical Operations
PEMDAS - The Proper Order of Mathematical Operations
Mark (Mong) Montances
 
2.2 add real numbers day 1-2
2.2 add real numbers day 1-2
bweldon
 
8.05.Merge_sort.pptxCUYGYUKTGUIJBJKGKUYGFKJBNVKUYV87VYFHHGVTFGU
8.05.Merge_sort.pptxCUYGYUKTGUIJBJKGKUYGFKJBNVKUYV87VYFHHGVTFGU
denveramoson
 
BASIC OF ALGORITHM AND MATHEMATICS STUDENTS
BASIC OF ALGORITHM AND MATHEMATICS STUDENTS
jainyshah20
 
Different Searching and Sorting Methods.pptx
Different Searching and Sorting Methods.pptx
Minakshee Patil
 
Numerical Linear Algebra in digital image processing
Numerical Linear Algebra in digital image processing
Indra Hermawan
 
Linear-Programming-by-Simplex-Method.pptx
Linear-Programming-by-Simplex-Method.pptx
teresemondejar
 
Ad

More from chandankumar364348 (6)

Multi-class Alzheimer’s Disease Classification Using Deep Learning Techniques
Multi-class Alzheimer’s Disease Classification Using Deep Learning Techniques
chandankumar364348
 
Minimum Spanning Tree (Data Structure and Algorithm)
Minimum Spanning Tree (Data Structure and Algorithm)
chandankumar364348
 
Introduction and basic of Trees and Binary Trees
Introduction and basic of Trees and Binary Trees
chandankumar364348
 
Level of Program Correctness_Program_Reasoning.pptx
Level of Program Correctness_Program_Reasoning.pptx
chandankumar364348
 
session-1_Design_Analysis_Algorithm.pptx
session-1_Design_Analysis_Algorithm.pptx
chandankumar364348
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
Multi-class Alzheimer’s Disease Classification Using Deep Learning Techniques
Multi-class Alzheimer’s Disease Classification Using Deep Learning Techniques
chandankumar364348
 
Minimum Spanning Tree (Data Structure and Algorithm)
Minimum Spanning Tree (Data Structure and Algorithm)
chandankumar364348
 
Introduction and basic of Trees and Binary Trees
Introduction and basic of Trees and Binary Trees
chandankumar364348
 
Level of Program Correctness_Program_Reasoning.pptx
Level of Program Correctness_Program_Reasoning.pptx
chandankumar364348
 
session-1_Design_Analysis_Algorithm.pptx
session-1_Design_Analysis_Algorithm.pptx
chandankumar364348
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
Ad

Recently uploaded (20)

IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
Pavement and its types, Application of rigid and Flexible Pavements
Pavement and its types, Application of rigid and Flexible Pavements
Sakthivel M
 
Low Power SI Class E Power Amplifier and Rf Switch for Health Care
Low Power SI Class E Power Amplifier and Rf Switch for Health Care
ieijjournal
 
A Cluster-Based Trusted Secure Multipath Routing Protocol for Mobile Ad Hoc N...
A Cluster-Based Trusted Secure Multipath Routing Protocol for Mobile Ad Hoc N...
IJCNCJournal
 
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Shabista Imam
 
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
ijab2
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
 
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
 
20CE601- DESIGN OF STEEL STRUCTURES ,INTRODUCTION AND ALLOWABLE STRESS DESIGN
20CE601- DESIGN OF STEEL STRUCTURES ,INTRODUCTION AND ALLOWABLE STRESS DESIGN
gowthamvicky1
 
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
samueljackson3773
 
Cadastral Maps
Cadastral Maps
Google
 
Structured Programming with C++ :: Kjell Backman
Structured Programming with C++ :: Kjell Backman
Shabista Imam
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
CenterEnamel
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
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
 
Complete guidance book of Asp.Net Web API
Complete guidance book of Asp.Net Web API
Shabista Imam
 
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
23Q95A6706
 
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
Pavement and its types, Application of rigid and Flexible Pavements
Pavement and its types, Application of rigid and Flexible Pavements
Sakthivel M
 
Low Power SI Class E Power Amplifier and Rf Switch for Health Care
Low Power SI Class E Power Amplifier and Rf Switch for Health Care
ieijjournal
 
A Cluster-Based Trusted Secure Multipath Routing Protocol for Mobile Ad Hoc N...
A Cluster-Based Trusted Secure Multipath Routing Protocol for Mobile Ad Hoc N...
IJCNCJournal
 
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Shabista Imam
 
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
ijab2
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
 
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
 
20CE601- DESIGN OF STEEL STRUCTURES ,INTRODUCTION AND ALLOWABLE STRESS DESIGN
20CE601- DESIGN OF STEEL STRUCTURES ,INTRODUCTION AND ALLOWABLE STRESS DESIGN
gowthamvicky1
 
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
samueljackson3773
 
Cadastral Maps
Cadastral Maps
Google
 
Structured Programming with C++ :: Kjell Backman
Structured Programming with C++ :: Kjell Backman
Shabista Imam
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
CenterEnamel
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
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
 
Complete guidance book of Asp.Net Web API
Complete guidance book of Asp.Net Web API
Shabista Imam
 
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
23Q95A6706
 

Insert Sort & Merge Sort Using C Programming

  • 2. 2 Insertion Sort • Suppose we know how to insert a new element x in its proper place in an already sorted array A of size k, to get a new sorted array of size k+1 • Use this to sort the given array A of size n as follows: – Insert A[1] in the sorted array A[0]. So now A[0],A[1] are sorted – Insert A[2] in the sorted array A[0],A[1]. So now A[0],A[1],A[2] are sorted – Insert A[3] in the sorted array A[0],A[1],A[2]. So now A[0],A[1],A[2],A[3] are sorted – ….. – Insert A[i] in the sorted array A[0],A[1],…,A[i-1]. So now A[0],A[1],…A[i] are sorted – Continue until i = n-1 (outer loop)
  • 3. 3 How to do the first step • Compare x with A[k-1] (the last element) – If x ≥ A[k-1], we can make A[k] = x (as x is the max of all the elements) – If x < A[k-1], put A[k] = A[k-1] to create a hole in the k- th position, put x there • Now repeat by comparing x with A[k-2] (inserting x in its proper place in the sorted subarray A[0],A[1],…A[k-1] of k-2 elements) • The value x bubbles to the left until it finds an element A[i] such that x ≥ A[i] • No need to compare any more as all elements A[0], A[1], A[i] are less than x
  • 4. 4 Example of first step 5 7 11 13 20 22 A Insert x = 15
  • 5. 5 Example of first step 5 7 11 13 20 22 5 7 11 13 20 15 22 A Insert x = 15 Compare with 22. x < 22, so move 22 right
  • 6. 6 Example of first step 5 7 11 13 20 22 5 7 11 13 20 15 22 5 7 11 13 15 20 22 A Insert x = 15 Compare with 22. x < 22, so move 22 right Compare with 20. x < 20, so move 20 right
  • 7. 7 Example of first step 5 7 11 13 20 22 5 7 11 13 20 15 22 5 7 11 13 15 20 22 5 7 11 13 15 20 22 A Insert x = 15 Compare with 22. x < 22, so move 22 right Compare with 20. x < 20, so move 20 right Compare with 13. x > 13, so stop A
  • 8. 8 Sort using the insertion 7 5 13 11 22 20 5 7 13 11 22 20 5 7 13 11 22 20 5 7 11 13 22 20 A Insert 5 in 7 Insert 13 in 5, 7 Insert 11 in 5, 7, 13 Insert 22 in 5, 7, 11, 13 Insert 20 in 5, 7, 11, 13, 22 5 7 11 13 20 22 5 7 11 13 22 20
  • 9. 9 Insertion Sort Code void InsertionSort (int A[ ], int size) { int i, j, item; for (i=1; i<size; i++) { /* Insert the element in A[i] */ item = A[i] ; for (j = i-1; j >= 0; j--) if (item < A[j]) { /* push elements down*/ A[j+1] = A[j]; A[j] = item ; /* can do this on } else break; /*inserted, exit loop */ } }
  • 10. 10 Look at the so 8 2 9 4 7 6 2 1 5 i = 1:: 2, 9, 4, 7 i = 2:: 9, 2, 4, 7 i = 3:: 9, 4, 2, 7 i = 4:: 9, 7, 4, 2 i = 5:: 9, 7, 6, 4 i = 6:: 9, 7, 6, 4 i = 7:: 9, 7, 6, 4 Result = 9, 7, 6, 5 void InsertionSort (int A[ ], int size) { int i,j, item; for (i=1; i<size; i++) { printf("i = %d:: ",i); for (j=0;j<size;j++) printf("%d, ",A[j]); printf("n"); item = A[i] ; for (j=i-1; j>=0; j--) if (item > A[j]) { A[j+1] = A[j]; A[j] = item ; } else break; } int main() { int X[100], i, size; scanf("%d",&size); for (i=0;i<size;i++) scanf("%d",&X[i]); InsertionSort(X,size); printf("Result = "); for (i=0;i<size;i++) printf("%d, ",X[i]); printf("n"); return 0; }
  • 11. 11 Merge Sort • Review of Sorting • Merge Sort
  • 12. 12 Sorting Algorithms • Selection Sort uses a priority queue P implemented with an unsorted sequence: – Phase 1: the insertion of an item into P takes O(1) time; overall O(n) time for inserting n items. – Phase 2: removing an item takes time proportional to the number of elements in P, which is O(n): overall O(n2) – Time Complexity: O(n2)
  • 13. 13 Sorting Algorithms (cont.) • Insertion Sort is performed on a priority queue P which is a sorted sequence: – Phase 1: the first insertItem takes O(1), the second O(2), until the last insertItem takes O(n): overall O(n2) – Phase 2: removing an item takes O(1) time; overall O(n). – Time Complexity: O(n2) • Heap Sort uses a priority queue K which is a heap. – insertItem and removeMin each take O(logk), k being the number of elements in the heap at a given time. – Phase 1: n elements inserted: O (nlogn) time – Phase 2: n elements removed: O (nlogn) time. – Time Complexity: O (nlog n)
  • 14. 14 Divide-and-Conquer • Divide and Conquer is more than just a military strategy; it is also a method of algorithm design that has created such efficient algorithms as Merge Sort. • In terms or algorithms, this method has three distinct steps: – Divide: If the input size is too large to deal with in a straightforward manner, divide the data into two or more disjoint subsets. – Recur: Use divide and conquer to solve the subproblems associated with the data subsets. – Conquer: Take the solutions to the subproblems and “merge” these solutions into a solution for the original problem.
  • 15. 15 Merge-Sort • Algorithm: – Divide: If S has at leas two elements (nothing needs to be done if S has zero or one elements), remove all the elements from S and put them into two sequences, S1 and S2, each containing about half of the elements of S. (i.e. S1 contains the first n/2 elements and S2 contains the remaining n/2 elements. – Recur: Recursive sort sequences S1 and S2. – Conquer: Put back the elements into S by merging the sorted sequences S1 and S2 into a unique sorted sequence. • Merge Sort Tree: – Take a binary tree T – Each node of T represents a recursive call of the merge sort algorithm. – We associate with each node v of T a the set of input passed to the invocation v represents. – The external nodes are associated with individual elements of S, upon which no recursion is called.
  • 33. Merging two sorted arrays Splitting arrays Example 3 12 -5 6 72 21 -7 45 x: 3 12 -5 6 72 21 -7 45 3 12 -5 6 72 21 -7 45 3 12 -5 6 72 21 -7 45 3 12 -5 6 21 72 -7 45 -5 3 6 12 -7 21 45 72 -7 -5 3 -7 -5 3 6 12 21 45 72
  • 34. -5 3 6 12 -7 21 45 72 Sorted Arr-1 Sorted Arr-2 i=j=k=0 m n i j k -7 j k -5 i k 3 k i 6 k i 12 k 21 j k 45 j k 72
  • 35. Merge Sort C program #include<stdio.h> void mergesort(int a[],int i,int j); void merge(int a[],int i1,int j1,int i2,int j2); int main() { int a[30],n,i; printf("Enter no of elements:"); scanf("%d",&n); printf("Enter array elements:"); for(i=0;i<n;i++) scanf("%d",&a[i]); mergesort(a,0,n-1); printf("nSorted array is :"); for(i=0;i<n;i++) printf("%d ",a[i]); return 0; } void mergesort(int a[],int i,int j) { int mid; if(i<j) { mid=(i+j)/2; /* left recursion */ mergesort(a,i,mid); /* right recursion */ mergesort(a,mid+1,j); /* merging of two sorted sub-arrays */ merge(a,i,mid,mid+1,j); } }
  • 36. Merge Sort C program void merge(int a[],int i1,int j1,int i2,int j2) { int temp[50]; //array used for merging int i=i1,j=i2,k=0; while(i<=j1 && j<=j2) //while elements in both lists { if(a[i]<a[j]) temp[k++]=a[i++]; else temp[k++]=a[j++]; } while(i<=j1) //copy remaining elements of the first list temp[k++]=a[i++]; while(j<=j2) //copy remaining elements of the second list temp[k++]=a[j++]; for(i=i1,j=0;i<=j2;i++,j++) a[i]=temp[j]; //Transfer elements from temp[] back to a[] }
  • 37. Splitting Trace -56 23 43 -5 -3 0 123 -35 87 56 75 80 -56 -35 -5 -3 0 23 43 56 75 80 87 123 -56 23 43 23 43 -56 23 43 -5 -3 0 123 -35 87 56 75 80 -56 23 43 -5 -3 0 -3 0 -3 0 123 -35 87 -35 87 123 -35 87 56 75 80 56 75 80 75 80 Worst Case: O(n.log(n))