SlideShare une entreprise Scribd logo
1  sur  22
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

Contenu connexe

Tendances

Bubble sort
Bubble sortBubble sort
Bubble sortManek Ar
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sortDistrict Administration
 
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
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary searchNisha Soms
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisSNJ Chaudhary
 
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
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-SortTareq Hasan
 
Insertion sort
Insertion sortInsertion sort
Insertion sortMYER301
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queueSenthil Kumar
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary searchZia Ush Shamszaman
 
Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation AhmedAlbutty
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data StructureJeanie Arnoco
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sortingKaushal Shah
 

Tendances (20)

Bubble sort
Bubble sortBubble sort
Bubble sort
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
 
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...
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Quick sort
Quick sortQuick sort
Quick sort
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 
Linked lists
Linked listsLinked lists
Linked lists
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Binary search
Binary searchBinary search
Binary search
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
 
Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Binary Search
Binary SearchBinary Search
Binary Search
 

Similaire à Sorting algorithms

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 problemsSumita Das
 
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 printAbdii Rashid
 
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 analysisRadhika Talaviya
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfAkashSingh625550
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptxDeepakM509554
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Abdul Khan
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sortKrish_ver2
 
Radix and Merge Sort
Radix and Merge SortRadix and Merge Sort
Radix and Merge SortGelo Maribbay
 
A Survey of Adaptive QuickSort Algorithms
A Survey of Adaptive QuickSort AlgorithmsA Survey of Adaptive QuickSort Algorithms
A Survey of Adaptive QuickSort AlgorithmsCSCJournals
 
IRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET Journal
 
Is sort andy-le
Is sort andy-leIs sort andy-le
Is sort andy-leSumedha
 

Similaire à Sorting algorithms (20)

Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Sorting
SortingSorting
Sorting
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
 
daa unit 1.pptx
daa unit 1.pptxdaa unit 1.pptx
daa unit 1.pptx
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
 
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
 
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
 
CSPC/ PPS Sorting methods
CSPC/ PPS Sorting methodsCSPC/ PPS Sorting methods
CSPC/ PPS Sorting methods
 
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
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptx
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 
Radix and Merge Sort
Radix and Merge SortRadix and Merge Sort
Radix and Merge Sort
 
A Survey of Adaptive QuickSort Algorithms
A Survey of Adaptive QuickSort AlgorithmsA Survey of Adaptive QuickSort Algorithms
A Survey of Adaptive QuickSort Algorithms
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
IRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching Algorithms
 
Algorithms - "quicksort"
Algorithms - "quicksort"Algorithms - "quicksort"
Algorithms - "quicksort"
 
Is sort andy-le
Is sort andy-leIs sort andy-le
Is sort andy-le
 

Plus de Trupti Agrawal

Plus de Trupti Agrawal (6)

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

Dernier

Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 

Dernier (20)

Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 

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