SlideShare a Scribd company logo
Data Structures I (CPCS-204) 
Week # 4: Sorting Algorithms
Sorting 
 Motivation 
 Generally, to arrange a list of elements in some order 
 List of numbers 
 10 20 50 30 40 60 25 (Unsorted list) 
 10 20 25 30 40 50 60 (Sorted list, ascending) 
 60 50 40 30 25 20 10 (Sorted list, descending) 
 List of alphabets 
 P A K I S T A N (Unsorted list) 
 A A I K N P S T (Sorted list, ascending) 
 T S P N K I A A (Sorted list, descending)
Sorting Algorithms 
1. Bubble Sort 
2. Selection Sort 
3. Insertion Sort 
4. Quick Sort 
5. Merge Sort 
There are few more sorting algorithms; you 
can find them in a book on data structures 
and algorithms (or on the Web)
Bubble Sort Algorithm: Informal 
 Repeatedly compare the elements at 
consecutive locations in a given list, and do the 
following until the elements are in required 
order: 
 If elements are not in the required order, swap them 
(change their position) 
 Otherwise do nothing
Bubble Sort in Action: Phase 1 
3 
15 
45 
40 
8 
12 
5 
22 
14 
3 
15 
45 
40 
8 
12 
5 
22 
14 
3 
15 
45 
40 
22 
8 
12 
5 
14 
3 
15 
45 
40 
22 
8 
12 
5 
14 
3 
15 
45 
40 
8 
12 
22 
5 
14 
3 
15 
45 
40 
8 
22 
12 
5 
14 
3 
15 
45 
40 
22 
8 
12 
5 
14 
3 
45 
15 
40 
22 
8 
12 
5 
14 
45 
3 
15 
40 
22 
8 
12 
5 
14 
8 
c 
o 
mpari 
s 
o 
ns
45 
3 
15 
40 
22 
8 
12 
5 
14 
Bubble Sort in Action: Phase 2 
45 
3 
15 
40 
22 
14 
8 
12 
5 
45 
3 
15 
40 
22 
14 
8 
12 
5 
45 
3 
15 
40 
22 
8 
12 
14 
5 
45 
3 
15 
40 
22 
8 
14 
12 
5 
45 
3 
15 
40 
22 
14 
8 
12 
5 
45 
3 
40 
15 
22 
14 
8 
12 
5 
45 
40 
3 
15 
22 
14 
8 
12 
5 
7 
c 
o 
mpari 
s 
o 
ns
Bubble Sort in Action: Phase 3 
45 
40 
3 
15 
22 
14 
12 
8 
5 
45 
40 
3 
15 
22 
14 
12 
8 
5 
45 
40 
3 
15 
22 
14 
8 
12 
5 
45 
40 
3 
15 
22 
14 
8 
12 
5 
45 
40 
3 
15 
22 
14 
12 
8 
5 
45 
40 
3 
22 
15 
14 
12 
8 
5 
45 
40 
22 
3 
15 
14 
12 
8 
5 
6 
c 
o 
mpari 
s 
o 
ns
Bubble Sort in Action: Phase 4 
45 
40 
22 
3 
15 
14 
12 
8 
5 
45 
40 
22 
3 
15 
14 
12 
8 
5 
45 
40 
22 
3 
15 
14 
12 
8 
5 
45 
40 
22 
3 
15 
14 
12 
8 
5 
45 
40 
22 
3 
15 
14 
12 
8 
5 
45 
40 
22 
15 
3 
14 
12 
8 
5 
5 
c 
o 
mpari 
s 
o 
ns
Bubble Sort in Action: Phase 5 
45 
40 
22 
15 
3 
14 
12 
8 
5 
4 
c 
o 
mpari 
s 
o 
ns 
45 
40 
22 
15 
3 
14 
12 
8 
5 
45 
40 
22 
15 
3 
14 
12 
8 
5 
45 
40 
22 
15 
3 
14 
12 
8 
5 
45 
40 
22 
15 
14 
3 
12 
8 
5
Bubble Sort in Action: Phase 6 
3 
c 
o 
mpari 
s 
o 
ns 
45 
40 
22 
15 
14 
3 
12 
8 
5 
45 
40 
22 
15 
14 
3 
12 
8 
5 
45 
40 
22 
15 
14 
3 
12 
8 
5 
45 
40 
22 
15 
14 
12 
3 
8 
5
Bubble Sort in Action: Phase 7 
2 
c 
o 
mpari 
s 
o 
ns 
45 
40 
22 
15 
14 
12 
3 
8 
5 
45 
40 
22 
15 
14 
12 
3 
8 
5 
45 
40 
22 
15 
14 
12 
8 
3 
5
Bubble Sort in Action: Phase 8 
1 
c 
o 
mpari 
s 
o 
n 
45 
40 
22 
15 
14 
12 
8 
3 
5 
45 
40 
22 
15 
14 
12 
8 
5 
3
Bubble Sort Algorithm in Java 
void bubbleSort(int List[]){ 
int temp; 
int size = List.length; 
for (i = 0; i < size - 1; i++) 
for (j = 0; j < size – (i + 1); j++){ 
if (List[j] > List[j+1]){ 
temp = List[j]; 
List[j] = List[j+1]; 
List[j+1] = temp; 
} 
} 
} 
} 
Time complexity of the Bubble Sort algorithm is O(n2). Think why?
Selection Sort: Informal 
 Suppose we want to sort an array in ascending 
order: 
 Locate the smallest element in the array; swap it 
with element at index 0 
 Then, locate the next smallest element in the array; 
swap it with element at index 1. 
 Continue until all elements are arranged in order
14 
15 
45 
40 
22 
12 
8 
5 
3 
14 
15 
45 
40 
8 
12 
22 
5 
3 
14 
15 
45 
40 
8 
12 
5 
22 
3 
3 
15 
45 
40 
8 
12 
5 
22 
14 
Selection Sort in Action
Selection Sort in Action 
22 
15 
45 
40 
14 
12 
8 
5 
3 
14 
15 
45 
40 
22 
12 
8 
5 
3 
14 
15 
45 
40 
22 
12 
8 
5 
3 
22 
40 
45 
15 
14 
12 
8 
5 
3
Selection Sort in Action 
22 
45 
45 
40 
40 
40 
45 
22 
22 
15 
15 
15 
14 
14 
14 
12 
12 
12 
8 
8 
8 
5 
5 
5 
3 
3 
3 
45 
40 
22 
15 
14 
12 
8 
5 
3
Selection Sort Algorithm in Java 
void selectionSort(int List[]){ 
int temp, min; 
int size = List.length; 
for (i = 0; i < size; i++){ 
min = i; 
for (j = i + 1; j < size; j++){ 
if (List[j] < List[min]){ 
min = j; 
} 
} 
temp = List[min]; 
List[min] = List[i]; 
List[i] = temp; 
} 
} 
Time complexity of the Selection Sort algorithm is O(n2). Think why?
Bubble Sort vs. Selection Sort 
 Selection Sort is more efficient than Bubble Sort, 
because of fewer exchanges in the former 
 Both Bubble Sort and Selection Sort belong to 
the same (quadratic) complexity class (O(n2)) 
 Bubble Sort may be easy to understand as 
compared to Selection Sort – What do you think?
Insertion Sort 
Works like someone who 
inserts one more card at a time 
into a hand of cards that are 
already sorted 
To insert 12, we need to make 
room for it … 
6 10 24 
36 
12 
20
21 
6 10 24 
… by shifting first 36 
towards right… 
36 
12 
21 
Insertion Sort
… and then shifting 24 
towards right 
6 10 24 36 
12 
22 
Insertion Sort
Once room is available, 
insert the element (12 in this 
case) 
6 10 12 
24 36 
23 
Insertion Sort
Insertion Sort: Informal 
 We divide the list into two parts: Sorted and 
Unsorted parts 
 Initially 
o the sorted part contains the first element (at index 0) 
o the unsorted part contains the elements from index 1 to N-1 
 Then, we move element from index 1 to an appropriate 
position in the sorted part, keeping order intact 
 Then, we move element from index 2 to an appropriate 
position in the sorted part, keeping order intact 
... 
 Finally, we move element from index N-1 to an 
appropriate position in the sorted part, keeping order intact
Insertion Sort Algorithm in Java 
void insertionSort(int List[]){ 
int temp; 
int size = List.length; 
for (int i = 1; i < size; i++){ 
int j = i; 
temp = List[i]; 
while (j > 0 && temp < List[j - 1]){ 
List[j] = List[j - 1]; // right shifting 
j--; 
} 
List[j] = temp; 
} 
} 
Time complexity of the Insertion Sort algorithm is O(n2). Think why?
Outlook 
Next week, we’ll discuss recursion

More Related Content

What's hot (20)

PPT
Selection sort
stella D
 
PDF
Java: The Complete Reference, Eleventh Edition
moxuji
 
PPTX
Datastructures in python
hydpy
 
PPTX
Different types of Linked list.
JAYANTA OJHA
 
PDF
Python modules
Learnbay Datascience
 
PPT
Circular linked list
chauhankapil
 
PPTX
linked list in data structure
shameen khan
 
PPTX
Tries data structures
Jothi Ramasamy
 
PDF
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
PPTX
DFS and BFS
satya parsana
 
PPTX
Strings in Java
Abhilash Nair
 
PPT
Arrays Data Structure
student
 
PPTX
Fourier series and its applications by md nazmul islam
Md Nazmul Islam
 
PDF
Linear search algorithm
NeoClassical
 
PDF
Minimum spanning tree
Amit Kumar Rathi
 
PPTX
Java Strings
RaBiya Chaudhry
 
PPTX
Sparse matrix and its representation data structure
Vardhil Patel
 
PPTX
Command line arguments
Ashok Raj
 
PDF
Applications of stack
eShikshak
 
PPTX
Stack and Queue
Apurbo Datta
 
Selection sort
stella D
 
Java: The Complete Reference, Eleventh Edition
moxuji
 
Datastructures in python
hydpy
 
Different types of Linked list.
JAYANTA OJHA
 
Python modules
Learnbay Datascience
 
Circular linked list
chauhankapil
 
linked list in data structure
shameen khan
 
Tries data structures
Jothi Ramasamy
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
DFS and BFS
satya parsana
 
Strings in Java
Abhilash Nair
 
Arrays Data Structure
student
 
Fourier series and its applications by md nazmul islam
Md Nazmul Islam
 
Linear search algorithm
NeoClassical
 
Minimum spanning tree
Amit Kumar Rathi
 
Java Strings
RaBiya Chaudhry
 
Sparse matrix and its representation data structure
Vardhil Patel
 
Command line arguments
Ashok Raj
 
Applications of stack
eShikshak
 
Stack and Queue
Apurbo Datta
 

Similar to Data Structures- Part4 basic sorting algorithms (20)

PPT
358 33 powerpoint-slides_14-sorting_chapter-14
sumitbardhan
 
PPTX
Data structure.pptx
SajalFayyaz
 
PPT
Sorting algorithums > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
PPTX
Unit vii sorting
Tribhuvan University
 
PPTX
Lecture 13 data structures and algorithms
Aakash deep Singhal
 
PPTX
Searching and sorting Techniques in Data structures
PRIANKA R
 
PPT
Data Structure (MC501)
Kamal Singh Lodhi
 
PDF
L 14-ct1120
Zia Ush Shamszaman
 
PPT
Sorting algorithm
Muhammad Farhan
 
PPTX
sorting algorithm graphical method
Shantanu Mishra
 
PPT
ds 3Sorting.ppt
AlliVinay1
 
PPT
Sorting algorithms
Edward Blurock
 
PPTX
Unit 7 sorting
Dabbal Singh Mahara
 
PPTX
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
suryatom5775
 
PPT
Sorting algorithms
CHANDAN KUMAR
 
PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
Muhammad Hammad Waseem
 
PPT
Sorting
Sameer Memon
 
PPT
sorting_part1.ppt
ReehaamMalikArain
 
PPT
SIMPLE SORTING MUKUND
Mukund Trivedi
 
PPTX
Chapter-2.pptx
selemonGamo
 
358 33 powerpoint-slides_14-sorting_chapter-14
sumitbardhan
 
Data structure.pptx
SajalFayyaz
 
Sorting algorithums > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
Unit vii sorting
Tribhuvan University
 
Lecture 13 data structures and algorithms
Aakash deep Singhal
 
Searching and sorting Techniques in Data structures
PRIANKA R
 
Data Structure (MC501)
Kamal Singh Lodhi
 
L 14-ct1120
Zia Ush Shamszaman
 
Sorting algorithm
Muhammad Farhan
 
sorting algorithm graphical method
Shantanu Mishra
 
ds 3Sorting.ppt
AlliVinay1
 
Sorting algorithms
Edward Blurock
 
Unit 7 sorting
Dabbal Singh Mahara
 
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
suryatom5775
 
Sorting algorithms
CHANDAN KUMAR
 
Data Structures - Lecture 8 [Sorting Algorithms]
Muhammad Hammad Waseem
 
Sorting
Sameer Memon
 
sorting_part1.ppt
ReehaamMalikArain
 
SIMPLE SORTING MUKUND
Mukund Trivedi
 
Chapter-2.pptx
selemonGamo
 
Ad

More from Abdullah Al-hazmy (7)

PPT
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
PPT
Data Structures- Part3 arrays and searching algorithms
Abdullah Al-hazmy
 
PPT
Data Structures- Part2 analysis tools
Abdullah Al-hazmy
 
PPT
Data Structures- Part9 trees simplified
Abdullah Al-hazmy
 
PPT
Data Structures- Part8 stacks and queues
Abdullah Al-hazmy
 
PPT
Data Structures- Part7 linked lists
Abdullah Al-hazmy
 
PPT
Data Structures- Part1 overview and review
Abdullah Al-hazmy
 
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
Data Structures- Part3 arrays and searching algorithms
Abdullah Al-hazmy
 
Data Structures- Part2 analysis tools
Abdullah Al-hazmy
 
Data Structures- Part9 trees simplified
Abdullah Al-hazmy
 
Data Structures- Part8 stacks and queues
Abdullah Al-hazmy
 
Data Structures- Part7 linked lists
Abdullah Al-hazmy
 
Data Structures- Part1 overview and review
Abdullah Al-hazmy
 
Ad

Recently uploaded (20)

PDF
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 

Data Structures- Part4 basic sorting algorithms

  • 1. Data Structures I (CPCS-204) Week # 4: Sorting Algorithms
  • 2. Sorting  Motivation  Generally, to arrange a list of elements in some order  List of numbers  10 20 50 30 40 60 25 (Unsorted list)  10 20 25 30 40 50 60 (Sorted list, ascending)  60 50 40 30 25 20 10 (Sorted list, descending)  List of alphabets  P A K I S T A N (Unsorted list)  A A I K N P S T (Sorted list, ascending)  T S P N K I A A (Sorted list, descending)
  • 3. Sorting Algorithms 1. Bubble Sort 2. Selection Sort 3. Insertion Sort 4. Quick Sort 5. Merge Sort There are few more sorting algorithms; you can find them in a book on data structures and algorithms (or on the Web)
  • 4. Bubble Sort Algorithm: Informal  Repeatedly compare the elements at consecutive locations in a given list, and do the following until the elements are in required order:  If elements are not in the required order, swap them (change their position)  Otherwise do nothing
  • 5. Bubble Sort in Action: Phase 1 3 15 45 40 8 12 5 22 14 3 15 45 40 8 12 5 22 14 3 15 45 40 22 8 12 5 14 3 15 45 40 22 8 12 5 14 3 15 45 40 8 12 22 5 14 3 15 45 40 8 22 12 5 14 3 15 45 40 22 8 12 5 14 3 45 15 40 22 8 12 5 14 45 3 15 40 22 8 12 5 14 8 c o mpari s o ns
  • 6. 45 3 15 40 22 8 12 5 14 Bubble Sort in Action: Phase 2 45 3 15 40 22 14 8 12 5 45 3 15 40 22 14 8 12 5 45 3 15 40 22 8 12 14 5 45 3 15 40 22 8 14 12 5 45 3 15 40 22 14 8 12 5 45 3 40 15 22 14 8 12 5 45 40 3 15 22 14 8 12 5 7 c o mpari s o ns
  • 7. Bubble Sort in Action: Phase 3 45 40 3 15 22 14 12 8 5 45 40 3 15 22 14 12 8 5 45 40 3 15 22 14 8 12 5 45 40 3 15 22 14 8 12 5 45 40 3 15 22 14 12 8 5 45 40 3 22 15 14 12 8 5 45 40 22 3 15 14 12 8 5 6 c o mpari s o ns
  • 8. Bubble Sort in Action: Phase 4 45 40 22 3 15 14 12 8 5 45 40 22 3 15 14 12 8 5 45 40 22 3 15 14 12 8 5 45 40 22 3 15 14 12 8 5 45 40 22 3 15 14 12 8 5 45 40 22 15 3 14 12 8 5 5 c o mpari s o ns
  • 9. Bubble Sort in Action: Phase 5 45 40 22 15 3 14 12 8 5 4 c o mpari s o ns 45 40 22 15 3 14 12 8 5 45 40 22 15 3 14 12 8 5 45 40 22 15 3 14 12 8 5 45 40 22 15 14 3 12 8 5
  • 10. Bubble Sort in Action: Phase 6 3 c o mpari s o ns 45 40 22 15 14 3 12 8 5 45 40 22 15 14 3 12 8 5 45 40 22 15 14 3 12 8 5 45 40 22 15 14 12 3 8 5
  • 11. Bubble Sort in Action: Phase 7 2 c o mpari s o ns 45 40 22 15 14 12 3 8 5 45 40 22 15 14 12 3 8 5 45 40 22 15 14 12 8 3 5
  • 12. Bubble Sort in Action: Phase 8 1 c o mpari s o n 45 40 22 15 14 12 8 3 5 45 40 22 15 14 12 8 5 3
  • 13. Bubble Sort Algorithm in Java void bubbleSort(int List[]){ int temp; int size = List.length; for (i = 0; i < size - 1; i++) for (j = 0; j < size – (i + 1); j++){ if (List[j] > List[j+1]){ temp = List[j]; List[j] = List[j+1]; List[j+1] = temp; } } } } Time complexity of the Bubble Sort algorithm is O(n2). Think why?
  • 14. Selection Sort: Informal  Suppose we want to sort an array in ascending order:  Locate the smallest element in the array; swap it with element at index 0  Then, locate the next smallest element in the array; swap it with element at index 1.  Continue until all elements are arranged in order
  • 15. 14 15 45 40 22 12 8 5 3 14 15 45 40 8 12 22 5 3 14 15 45 40 8 12 5 22 3 3 15 45 40 8 12 5 22 14 Selection Sort in Action
  • 16. Selection Sort in Action 22 15 45 40 14 12 8 5 3 14 15 45 40 22 12 8 5 3 14 15 45 40 22 12 8 5 3 22 40 45 15 14 12 8 5 3
  • 17. Selection Sort in Action 22 45 45 40 40 40 45 22 22 15 15 15 14 14 14 12 12 12 8 8 8 5 5 5 3 3 3 45 40 22 15 14 12 8 5 3
  • 18. Selection Sort Algorithm in Java void selectionSort(int List[]){ int temp, min; int size = List.length; for (i = 0; i < size; i++){ min = i; for (j = i + 1; j < size; j++){ if (List[j] < List[min]){ min = j; } } temp = List[min]; List[min] = List[i]; List[i] = temp; } } Time complexity of the Selection Sort algorithm is O(n2). Think why?
  • 19. Bubble Sort vs. Selection Sort  Selection Sort is more efficient than Bubble Sort, because of fewer exchanges in the former  Both Bubble Sort and Selection Sort belong to the same (quadratic) complexity class (O(n2))  Bubble Sort may be easy to understand as compared to Selection Sort – What do you think?
  • 20. Insertion Sort Works like someone who inserts one more card at a time into a hand of cards that are already sorted To insert 12, we need to make room for it … 6 10 24 36 12 20
  • 21. 21 6 10 24 … by shifting first 36 towards right… 36 12 21 Insertion Sort
  • 22. … and then shifting 24 towards right 6 10 24 36 12 22 Insertion Sort
  • 23. Once room is available, insert the element (12 in this case) 6 10 12 24 36 23 Insertion Sort
  • 24. Insertion Sort: Informal  We divide the list into two parts: Sorted and Unsorted parts  Initially o the sorted part contains the first element (at index 0) o the unsorted part contains the elements from index 1 to N-1  Then, we move element from index 1 to an appropriate position in the sorted part, keeping order intact  Then, we move element from index 2 to an appropriate position in the sorted part, keeping order intact ...  Finally, we move element from index N-1 to an appropriate position in the sorted part, keeping order intact
  • 25. Insertion Sort Algorithm in Java void insertionSort(int List[]){ int temp; int size = List.length; for (int i = 1; i < size; i++){ int j = i; temp = List[i]; while (j > 0 && temp < List[j - 1]){ List[j] = List[j - 1]; // right shifting j--; } List[j] = temp; } } Time complexity of the Insertion Sort algorithm is O(n2). Think why?
  • 26. Outlook Next week, we’ll discuss recursion