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
Ad

More Related Content

What's hot (20)

Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
Rabin BK
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
MAHALAKSHMI P
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
District Administration
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Zidny Nafan
 
Double Linked List (Algorithm)
Double Linked List (Algorithm)Double Linked List (Algorithm)
Double Linked List (Algorithm)
Huba Akhtar
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
Omprakash Chauhan
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
Array
ArrayArray
Array
PRN USM
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
Ninad Mankar
 
Trigger
TriggerTrigger
Trigger
VForce Infotech
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
Adam Mukharil Bachtiar
 
Trigger
TriggerTrigger
Trigger
Slideshare
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
SIVASHANKARIRAJAN
 
Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())
Sameer Rathoud
 
Queues
QueuesQueues
Queues
Lovely Professional University
 
Insertion sort
Insertion sort Insertion sort
Insertion sort
Monalisa Patel
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSA
junnubabu
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
V.V.Vanniaperumal College for Women
 
Unit 2 linked list and queues
Unit 2   linked list and queuesUnit 2   linked list and queues
Unit 2 linked list and queues
kalyanineve
 

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

Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
Flynce Miguel
 
Sorting
SortingSorting
Sorting
Zaid Shabbir
 
Sorting
SortingSorting
Sorting
Zaid Shabbir
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
ssuserd602fd
 
Unit6 C
Unit6 C Unit6 C
Unit6 C
arnold 7490
 
Insertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityInsertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexity
Motaleb Hossen Manik
 
Decimal Long Double Double Double. Represents double-precision floating-point...
Decimal Long Double Double Double. Represents double-precision floating-point...Decimal Long Double Double Double. Represents double-precision floating-point...
Decimal Long Double Double Double. Represents double-precision floating-point...
Anwar Patel
 
3.2 insertion sort
3.2 insertion sort3.2 insertion sort
3.2 insertion sort
Krish_ver2
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
AnSHiKa187943
 
Searching Sorting-SELECTION ,BUBBBLE.ppt
Searching  Sorting-SELECTION ,BUBBBLE.pptSearching  Sorting-SELECTION ,BUBBBLE.ppt
Searching Sorting-SELECTION ,BUBBBLE.ppt
kunalpatil5661
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
Kamal Singh Lodhi
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
SajalFayyaz
 
Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data Structure
Tushar Gonawala
 
sorting algorithm graphical method
sorting algorithm graphical method sorting algorithm graphical method
sorting algorithm graphical method
Shantanu Mishra
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
Muhammad Hammad Waseem
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
Weak 11-12 Sorting update.pptxbhjiiuuuuu
Weak 11-12 Sorting update.pptxbhjiiuuuuuWeak 11-12 Sorting update.pptxbhjiiuuuuu
Weak 11-12 Sorting update.pptxbhjiiuuuuu
baloch4551701
 
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structuresSearching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
PRIANKA R
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3
smruti sarangi
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
Flynce Miguel
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
ssuserd602fd
 
Insertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityInsertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexity
Motaleb Hossen Manik
 
Decimal Long Double Double Double. Represents double-precision floating-point...
Decimal Long Double Double Double. Represents double-precision floating-point...Decimal Long Double Double Double. Represents double-precision floating-point...
Decimal Long Double Double Double. Represents double-precision floating-point...
Anwar Patel
 
3.2 insertion sort
3.2 insertion sort3.2 insertion sort
3.2 insertion sort
Krish_ver2
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
AnSHiKa187943
 
Searching Sorting-SELECTION ,BUBBBLE.ppt
Searching  Sorting-SELECTION ,BUBBBLE.pptSearching  Sorting-SELECTION ,BUBBBLE.ppt
Searching Sorting-SELECTION ,BUBBBLE.ppt
kunalpatil5661
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
SajalFayyaz
 
Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data Structure
Tushar Gonawala
 
sorting algorithm graphical method
sorting algorithm graphical method sorting algorithm graphical method
sorting algorithm graphical method
Shantanu Mishra
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
Muhammad Hammad Waseem
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
Weak 11-12 Sorting update.pptxbhjiiuuuuu
Weak 11-12 Sorting update.pptxbhjiiuuuuuWeak 11-12 Sorting update.pptxbhjiiuuuuu
Weak 11-12 Sorting update.pptxbhjiiuuuuu
baloch4551701
 
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structuresSearching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
PRIANKA R
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3
smruti sarangi
 
Ad

More from Abdullah Al-hazmy (7)

Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
Data Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithmsData Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithms
Abdullah Al-hazmy
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis tools
Abdullah Al-hazmy
 
Data Structures- Part9 trees simplified
Data Structures- Part9 trees simplifiedData Structures- Part9 trees simplified
Data Structures- Part9 trees simplified
Abdullah Al-hazmy
 
Data Structures- Part8 stacks and queues
Data Structures- Part8 stacks and queuesData Structures- Part8 stacks and queues
Data Structures- Part8 stacks and queues
Abdullah Al-hazmy
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
Abdullah Al-hazmy
 
Data Structures- Part1 overview and review
Data Structures- Part1 overview and reviewData Structures- Part1 overview and review
Data Structures- Part1 overview and review
Abdullah Al-hazmy
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
Data Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithmsData Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithms
Abdullah Al-hazmy
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis tools
Abdullah Al-hazmy
 
Data Structures- Part9 trees simplified
Data Structures- Part9 trees simplifiedData Structures- Part9 trees simplified
Data Structures- Part9 trees simplified
Abdullah Al-hazmy
 
Data Structures- Part8 stacks and queues
Data Structures- Part8 stacks and queuesData Structures- Part8 stacks and queues
Data Structures- Part8 stacks and queues
Abdullah Al-hazmy
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
Abdullah Al-hazmy
 
Data Structures- Part1 overview and review
Data Structures- Part1 overview and reviewData Structures- Part1 overview and review
Data Structures- Part1 overview and review
Abdullah Al-hazmy
 
Ad

Recently uploaded (20)

Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Real GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for SuccessReal GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for Success
Mark Soia
 
How to Create A Todo List In Todo of Odoo 18
How to Create A Todo List In Todo of Odoo 18How to Create A Todo List In Todo of Odoo 18
How to Create A Todo List In Todo of Odoo 18
Celine George
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Debunking the Myths behind AI - v1, Carl Dalby
Debunking the Myths behind AI -  v1, Carl DalbyDebunking the Myths behind AI -  v1, Carl Dalby
Debunking the Myths behind AI - v1, Carl Dalby
Association for Project Management
 
Contact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: OptometryContact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: Optometry
MushahidRaza8
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
National Information Standards Organization (NISO)
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
TechSoup
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
"Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules""Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules"
rupalinirmalbpharm
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Real GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for SuccessReal GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for Success
Mark Soia
 
How to Create A Todo List In Todo of Odoo 18
How to Create A Todo List In Todo of Odoo 18How to Create A Todo List In Todo of Odoo 18
How to Create A Todo List In Todo of Odoo 18
Celine George
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Contact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: OptometryContact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: Optometry
MushahidRaza8
 
Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
TechSoup
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
"Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules""Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules"
rupalinirmalbpharm
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 

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