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))

More Related Content

PDF
Sql grant, revoke, privileges and roles
PPT
ORACLE PL SQL
PPT
Peterson Critical Section Problem Solution
PPT
7 data management design
PDF
Different addressing mode and risc, cisc microprocessor
PPTX
Counting Sort
PPTX
View, Store Procedure & Function and Trigger in MySQL - Thaipt
PPTX
Memory hierarchy unit 2 by ram k paliwal
Sql grant, revoke, privileges and roles
ORACLE PL SQL
Peterson Critical Section Problem Solution
7 data management design
Different addressing mode and risc, cisc microprocessor
Counting Sort
View, Store Procedure & Function and Trigger in MySQL - Thaipt
Memory hierarchy unit 2 by ram k paliwal

What's hot (20)

PPT
Master method theorem
PDF
Serializability
PPTX
Extendible hashing
PDF
Python tuples and Dictionary
PPTX
Computer Registers.pptx
PPTX
Merge sort algorithm
PPTX
Asymptotic notations(Big O, Omega, Theta )
PPTX
Disjoint sets union, find
PPTX
Greedy Algorithm - Knapsack Problem
PPT
Multivalued dependency
PPT
Asymptotic notations
PPTX
Quadratic probing
PDF
JAVA THREADS.pdf
PPTX
Huffman codes
PPT
Binary operator overloading
PDF
Alter table command
PDF
Abstract Data Types
PDF
Theory of computation and automata
DOC
rdbms-notes
Master method theorem
Serializability
Extendible hashing
Python tuples and Dictionary
Computer Registers.pptx
Merge sort algorithm
Asymptotic notations(Big O, Omega, Theta )
Disjoint sets union, find
Greedy Algorithm - Knapsack Problem
Multivalued dependency
Asymptotic notations
Quadratic probing
JAVA THREADS.pdf
Huffman codes
Binary operator overloading
Alter table command
Abstract Data Types
Theory of computation and automata
rdbms-notes
Ad

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

PPTX
Data Structure and algorithms for software
PPT
Unit 7 sorting
PPTX
sorting-160810203705.pptx
PPT
search_sort Search sortSearch sortSearch sortSearch sort
PPT
Lecture_4 (Sorting Algorithms) before mids - Copy.ppt
PPT
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
PPT
search_sort_v1.pptgghghhhggggjjjjjjllllllllvbbbbbcfdsdfffg
PPT
02_Gffdvxvvxzxzczcczzczcczczczxvxvxvds2.ppt
PPT
Algorithms and Data structures: Merge Sort
PDF
Sorting Algorithms and their implementations
PPT
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
PPT
03_sorting123456789454545454545444543.ppt
PPT
03_sorting and it's types with example .ppt
PDF
Sorting algorithms bubble sort to merge sort.pdf
PPTX
sorting-160810203705.pptx
PPT
PPTX
Chapter 8 Sorting in the context of DSA.pptx
PPTX
Insertion and merge sort
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
Data Structure and algorithms for software
Unit 7 sorting
sorting-160810203705.pptx
search_sort Search sortSearch sortSearch sortSearch sort
Lecture_4 (Sorting Algorithms) before mids - Copy.ppt
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
search_sort_v1.pptgghghhhggggjjjjjjllllllllvbbbbbcfdsdfffg
02_Gffdvxvvxzxzczcczzczcczczczxvxvxvds2.ppt
Algorithms and Data structures: Merge Sort
Sorting Algorithms and their implementations
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
03_sorting123456789454545454545444543.ppt
03_sorting and it's types with example .ppt
Sorting algorithms bubble sort to merge sort.pdf
sorting-160810203705.pptx
Chapter 8 Sorting in the context of DSA.pptx
Insertion and merge sort
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
Ad

More from chandankumar364348 (6)

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

Recently uploaded (20)

PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Digital Logic Computer Design lecture notes
PDF
composite construction of structures.pdf
PPTX
Construction Project Organization Group 2.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
PPT on Performance Review to get promotions
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
OOP with Java - Java Introduction (Basics)
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Lecture Notes Electrical Wiring System Components
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Digital Logic Computer Design lecture notes
composite construction of structures.pdf
Construction Project Organization Group 2.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
CYBER-CRIMES AND SECURITY A guide to understanding
PPT on Performance Review to get promotions
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
OOP with Java - Java Introduction (Basics)
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
UNIT 4 Total Quality Management .pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Lecture Notes Electrical Wiring System Components
Mechanical Engineering MATERIALS Selection
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx

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))