SlideShare a Scribd company logo
Sorting Algorithms 
Trupti Agrawal 1
Sorting Algorithms 
• A sorting algorithm is an algorithm that 
puts elements of a list in a certain order. 
• Efficient sorting is important for optimizing 
the use of other algorithms (such 
as search and merge algorithms) which 
require input data to be in sorted lists 
Trupti Agrawal 2
Why Sorting? 
 “When in doubt, sort” – one of the principles of 
algorithm design. Sorting used as a subroutine in 
many of the algorithms: 
 Searching in databases: we can do binary search on 
sorted data 
 A large number of computer graphics and 
computational geometry problems 
 Closest pair, element uniqueness, frequency distribution 
Trupti Agrawal 3
Why Sorting? (2) 
 A large number of algorithms developed representing 
different algorithm design techniques. 
 A lower bound for sorting W(n log n) is used to prove 
lower bounds of other problems 
Trupti Agrawal 4
Sorting Algorithms so far 
 Insertion sort, selection sort, bubble sort 
 Worst-case running time Q(n2); in-place 
 Merge sort 
 Worst-case running time Q(n log n); but requires 
additional memory 
Trupti Agrawal 5
Insertion sort 
• Card players all know how to sort … 
– First card is already sorted 
– With all the rest, 
Scan back from the end until you find the first card larger than 
the new one, 
Move all the lower ones up one slot 
insert it 
 
Q 
 
2 
 
9 
 
A 
 
K 
 
10 
 
J 
 
2 
 
2 
 
9 
 
 
 
Trupti Agrawal 6
Insertion Sort 
To insert 12, we need to 
make room for it by moving 
first 36 and then 24. 
Trupti Agrawal 7
8 
Insertion Sort 
Trupti Agrawal
Insertion Sort 
Trupti Agrawal 9
Insertion Sort 
input array 
5 2 4 6 1 3 
at each iteration, the array is divided in two sub-arrays: 
left sub-array right sub-array 
sorted unsorted 
Trupti Agrawal 10
Insertion Sort 
Trupti Agrawal 11
Insertion Sort - Summary 
• Advantages 
– Good running time for “almost sorted” arrays 
Q(n) 
• Disadvantages 
– Q(n2) running time in worst and average case 
–  n2/2 comparisons and exchanges 
Trupti Agrawal 12
Selection Sort 
• Idea: 
– Find the smallest element in the array 
– Exchange it with the element in the first 
position 
– Find the second smallest element and exchange 
it with the element in the second position 
– Continue until the array is sorted 
• Disadvantage: 
– Running time depends only slightly on the 
amount of order in the file 
Trupti Agrawal 13
Example-Selection Sort 
8 4 6 9 2 3 1 
1 4 6 9 2 3 8 
1 2 6 9 4 3 8 
1 2 3 9 4 6 8 
1 2 3 4 9 6 8 
1 2 3 4 6 9 8 
1 2 3 4 6 8 9 
1 2 3 4 6 8 9 
Trupti Agrawal 14
Bubble Sort 
• Idea: 
– Repeatedly pass through the array 
– Swaps adjacent elements that are out of order 
i 
1 2 3 n 
8 4 6 9 2 3 1 
j 
• Easier to implement, but slower than Insertion 
sort 
Trupti Agrawal 15
Example - Bubble Sort 
Trupti Agrawal 16
Quicksort 
 Efficient sorting algorithm 
 Discovered by C.A.R. Hoare 
 Example of Divide and Conquer algorithm 
 Two phases 
 Partition phase 
 Divides the work into half 
 Sort phase 
 Conquers the halves! 
Trupti Agrawal 17
Quicksort  Partition / Divide 
 Choose a pivot 
 Find the position for the pivot so that 
 all elements to the left are less 
 all elements to the right are greater 
< pivot pivot > pivot 
Trupti Agrawal 18
Quicksort 
 Conquer 
 Apply the same algorithm to each half 
< pivot > pivot 
< p’ p’ > p’ pivot < p” p” > p” 
Trupti Agrawal 19
Quicksort - Example 
Trupti Agrawal 20
Review of Algorithms 
 Selection Sort 
 An algorithm which orders items by repeatedly looking through remaining items to 
find the least one and moving it to a final location 
 Bubble Sort 
 Sort by comparing each adjacent pair of items in a list in turn, swapping the items if necessary, and 
repeating the pass through the list until no swaps are done 
 Insertion Sort 
 Sort by repeatedly taking the next item and inserting it into the final data structure in its proper order with 
respect to items already inserted. 
 Merge Sort 
 An algorithm which splits the items to be sorted into two groups, recursively sorts each group, and merges 
them into a final, sorted sequence 
 Quick Sort 
 An in-place sort algorithm that uses the divide and conquer paradigm. It picks an element from the array 
(the pivot), partitions the remaining elements into those greater than and less than this pivot, and 
recursively sorts the partitions. 
Trupti Agrawal 21
THANK YOU….. !!! 
Trupti Agrawal 22
Ad

More Related Content

What's hot (20)

Insertion sort
Insertion sortInsertion sort
Insertion sort
MYER301
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
FarihaHabib123
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
SHAKOOR AB
 
Merge sort algorithm power point presentation
Merge sort algorithm power point presentationMerge sort algorithm power point presentation
Merge sort algorithm power point presentation
University of Science and Technology Chitttagong
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
Nisha Soms
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
Tareq Hasan
 
Shell sorting
Shell sortingShell sorting
Shell sorting
TUC
 
Merge sort
Merge sortMerge sort
Merge sort
Vidushi Pathak
 
Quick Sort
Quick SortQuick Sort
Quick Sort
Shweta Sahu
 
Selection sort
Selection sortSelection sort
Selection sort
smlagustin
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
almaqboli
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
Shakila Mahjabin
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
Mohamed Loey
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
Shubham Dwivedi
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
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
 
Bubble sort | Data structure |
Bubble sort | Data structure |Bubble sort | Data structure |
Bubble sort | Data structure |
MdSaiful14
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
03446940736
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
MYER301
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
SHAKOOR AB
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
Nisha Soms
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
Tareq Hasan
 
Shell sorting
Shell sortingShell sorting
Shell sorting
TUC
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
almaqboli
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
Mohamed Loey
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
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
 
Bubble sort | Data structure |
Bubble sort | Data structure |Bubble sort | Data structure |
Bubble sort | Data structure |
MdSaiful14
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
03446940736
 

Similar to Sorting algorithms (20)

Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
Dr.Umadevi V
 
Sorting
SortingSorting
Sorting
BHARATH KUMAR
 
Parallel Sorting Algorithms. Quicksort. Merge sort. List Ranking
Parallel Sorting Algorithms. Quicksort. Merge sort. List RankingParallel Sorting Algorithms. Quicksort. Merge sort. List Ranking
Parallel Sorting Algorithms. Quicksort. Merge sort. List Ranking
SukhrobAtoev2
 
SORT AND SEARCH ARRAY WITH WITH C++.pptx
SORT AND SEARCH ARRAY WITH WITH C++.pptxSORT AND SEARCH ARRAY WITH WITH C++.pptx
SORT AND SEARCH ARRAY WITH WITH C++.pptx
narifmsit18seecs
 
what is sorting algorithm and implementation.pptx
what is sorting algorithm and implementation.pptxwhat is sorting algorithm and implementation.pptx
what is sorting algorithm and implementation.pptx
TanaTech
 
Algorithms and Data Structures for Sorting Numerical Data
Algorithms and Data Structures for Sorting Numerical DataAlgorithms and Data Structures for Sorting Numerical Data
Algorithms and Data Structures for Sorting Numerical Data
Pratik Parmar
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
Kamal Singh Lodhi
 
daa unit 1.pptx
daa unit 1.pptxdaa unit 1.pptx
daa unit 1.pptx
LakshayYadav46
 
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
 
Presentation on binary search, quick sort, merge sort and problems
Presentation on binary search, quick sort, merge sort  and problemsPresentation on binary search, quick sort, merge sort  and problems
Presentation on binary search, quick sort, merge sort and problems
Sumita Das
 
Searching searching in in arrays arrays.pptx
Searching searching in in arrays arrays.pptxSearching searching in in arrays arrays.pptx
Searching searching in in arrays arrays.pptx
Sahar160629
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
Abdii Rashid
 
Sorting-Algorithms-A-Comprehensive-Guide.pptx
Sorting-Algorithms-A-Comprehensive-Guide.pptxSorting-Algorithms-A-Comprehensive-Guide.pptx
Sorting-Algorithms-A-Comprehensive-Guide.pptx
ReemEmad26
 
CSPC/ PPS Sorting methods
CSPC/ PPS Sorting methodsCSPC/ PPS Sorting methods
CSPC/ PPS Sorting methods
Ankur Srivastava
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisAnalysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Radhika Talaviya
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
Searching Sorting-SELECTION ,BUBBBLE.ppt
Searching  Sorting-SELECTION ,BUBBBLE.pptSearching  Sorting-SELECTION ,BUBBBLE.ppt
Searching Sorting-SELECTION ,BUBBBLE.ppt
kunalpatil5661
 
Algorithms and Data Structures - Parahyangan Catholic University Credit Lionov
Algorithms and Data Structures - Parahyangan Catholic University Credit LionovAlgorithms and Data Structures - Parahyangan Catholic University Credit Lionov
Algorithms and Data Structures - Parahyangan Catholic University Credit Lionov
Pratik Parmar
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptx
DeepakM509554
 
09 QUICK SORT Design and Analysis of algorithms
09 QUICK SORT Design and Analysis of algorithms09 QUICK SORT Design and Analysis of algorithms
09 QUICK SORT Design and Analysis of algorithms
syamalamaganti
 
Parallel Sorting Algorithms. Quicksort. Merge sort. List Ranking
Parallel Sorting Algorithms. Quicksort. Merge sort. List RankingParallel Sorting Algorithms. Quicksort. Merge sort. List Ranking
Parallel Sorting Algorithms. Quicksort. Merge sort. List Ranking
SukhrobAtoev2
 
SORT AND SEARCH ARRAY WITH WITH C++.pptx
SORT AND SEARCH ARRAY WITH WITH C++.pptxSORT AND SEARCH ARRAY WITH WITH C++.pptx
SORT AND SEARCH ARRAY WITH WITH C++.pptx
narifmsit18seecs
 
what is sorting algorithm and implementation.pptx
what is sorting algorithm and implementation.pptxwhat is sorting algorithm and implementation.pptx
what is sorting algorithm and implementation.pptx
TanaTech
 
Algorithms and Data Structures for Sorting Numerical Data
Algorithms and Data Structures for Sorting Numerical DataAlgorithms and Data Structures for Sorting Numerical Data
Algorithms and Data Structures for Sorting Numerical Data
Pratik Parmar
 
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
 
Presentation on binary search, quick sort, merge sort and problems
Presentation on binary search, quick sort, merge sort  and problemsPresentation on binary search, quick sort, merge sort  and problems
Presentation on binary search, quick sort, merge sort and problems
Sumita Das
 
Searching searching in in arrays arrays.pptx
Searching searching in in arrays arrays.pptxSearching searching in in arrays arrays.pptx
Searching searching in in arrays arrays.pptx
Sahar160629
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
Abdii Rashid
 
Sorting-Algorithms-A-Comprehensive-Guide.pptx
Sorting-Algorithms-A-Comprehensive-Guide.pptxSorting-Algorithms-A-Comprehensive-Guide.pptx
Sorting-Algorithms-A-Comprehensive-Guide.pptx
ReemEmad26
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisAnalysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Radhika Talaviya
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
Searching Sorting-SELECTION ,BUBBBLE.ppt
Searching  Sorting-SELECTION ,BUBBBLE.pptSearching  Sorting-SELECTION ,BUBBBLE.ppt
Searching Sorting-SELECTION ,BUBBBLE.ppt
kunalpatil5661
 
Algorithms and Data Structures - Parahyangan Catholic University Credit Lionov
Algorithms and Data Structures - Parahyangan Catholic University Credit LionovAlgorithms and Data Structures - Parahyangan Catholic University Credit Lionov
Algorithms and Data Structures - Parahyangan Catholic University Credit Lionov
Pratik Parmar
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptx
DeepakM509554
 
09 QUICK SORT Design and Analysis of algorithms
09 QUICK SORT Design and Analysis of algorithms09 QUICK SORT Design and Analysis of algorithms
09 QUICK SORT Design and Analysis of algorithms
syamalamaganti
 
Ad

More from Trupti Agrawal (7)

Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
Trupti Agrawal
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
Trupti Agrawal
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
Trupti Agrawal
 
Linked list
Linked listLinked list
Linked list
Trupti Agrawal
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queues
Trupti Agrawal
 
Arrays
ArraysArrays
Arrays
Trupti Agrawal
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
Trupti Agrawal
 
Ad

Recently uploaded (20)

MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 

Sorting algorithms

  • 2. Sorting Algorithms • A sorting algorithm is an algorithm that puts elements of a list in a certain order. • Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists Trupti Agrawal 2
  • 3. Why Sorting?  “When in doubt, sort” – one of the principles of algorithm design. Sorting used as a subroutine in many of the algorithms:  Searching in databases: we can do binary search on sorted data  A large number of computer graphics and computational geometry problems  Closest pair, element uniqueness, frequency distribution Trupti Agrawal 3
  • 4. Why Sorting? (2)  A large number of algorithms developed representing different algorithm design techniques.  A lower bound for sorting W(n log n) is used to prove lower bounds of other problems Trupti Agrawal 4
  • 5. Sorting Algorithms so far  Insertion sort, selection sort, bubble sort  Worst-case running time Q(n2); in-place  Merge sort  Worst-case running time Q(n log n); but requires additional memory Trupti Agrawal 5
  • 6. Insertion sort • Card players all know how to sort … – First card is already sorted – With all the rest, Scan back from the end until you find the first card larger than the new one, Move all the lower ones up one slot insert it  Q  2  9  A  K  10  J  2  2  9    Trupti Agrawal 6
  • 7. Insertion Sort To insert 12, we need to make room for it by moving first 36 and then 24. Trupti Agrawal 7
  • 8. 8 Insertion Sort Trupti Agrawal
  • 10. Insertion Sort input array 5 2 4 6 1 3 at each iteration, the array is divided in two sub-arrays: left sub-array right sub-array sorted unsorted Trupti Agrawal 10
  • 11. Insertion Sort Trupti Agrawal 11
  • 12. Insertion Sort - Summary • Advantages – Good running time for “almost sorted” arrays Q(n) • Disadvantages – Q(n2) running time in worst and average case –  n2/2 comparisons and exchanges Trupti Agrawal 12
  • 13. Selection Sort • Idea: – Find the smallest element in the array – Exchange it with the element in the first position – Find the second smallest element and exchange it with the element in the second position – Continue until the array is sorted • Disadvantage: – Running time depends only slightly on the amount of order in the file Trupti Agrawal 13
  • 14. Example-Selection Sort 8 4 6 9 2 3 1 1 4 6 9 2 3 8 1 2 6 9 4 3 8 1 2 3 9 4 6 8 1 2 3 4 9 6 8 1 2 3 4 6 9 8 1 2 3 4 6 8 9 1 2 3 4 6 8 9 Trupti Agrawal 14
  • 15. Bubble Sort • Idea: – Repeatedly pass through the array – Swaps adjacent elements that are out of order i 1 2 3 n 8 4 6 9 2 3 1 j • Easier to implement, but slower than Insertion sort Trupti Agrawal 15
  • 16. Example - Bubble Sort Trupti Agrawal 16
  • 17. Quicksort  Efficient sorting algorithm  Discovered by C.A.R. Hoare  Example of Divide and Conquer algorithm  Two phases  Partition phase  Divides the work into half  Sort phase  Conquers the halves! Trupti Agrawal 17
  • 18. Quicksort  Partition / Divide  Choose a pivot  Find the position for the pivot so that  all elements to the left are less  all elements to the right are greater < pivot pivot > pivot Trupti Agrawal 18
  • 19. Quicksort  Conquer  Apply the same algorithm to each half < pivot > pivot < p’ p’ > p’ pivot < p” p” > p” Trupti Agrawal 19
  • 20. Quicksort - Example Trupti Agrawal 20
  • 21. Review of Algorithms  Selection Sort  An algorithm which orders items by repeatedly looking through remaining items to find the least one and moving it to a final location  Bubble Sort  Sort by comparing each adjacent pair of items in a list in turn, swapping the items if necessary, and repeating the pass through the list until no swaps are done  Insertion Sort  Sort by repeatedly taking the next item and inserting it into the final data structure in its proper order with respect to items already inserted.  Merge Sort  An algorithm which splits the items to be sorted into two groups, recursively sorts each group, and merges them into a final, sorted sequence  Quick Sort  An in-place sort algorithm that uses the divide and conquer paradigm. It picks an element from the array (the pivot), partitions the remaining elements into those greater than and less than this pivot, and recursively sorts the partitions. Trupti Agrawal 21
  • 22. THANK YOU….. !!! Trupti Agrawal 22
  翻译: