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

More Related Content

What's hot

Insertion sort
Insertion sortInsertion sort
Insertion sort
almaqboli
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
Shakila Mahjabin
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
Tareq Hasan
 
Quick sort
Quick sortQuick sort
Quick sort
Dhruv Sabalpara
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
Mohamed Loey
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Muhammad Hammad Waseem
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Dr Shashikant Athawale
 
Quick sort
Quick sortQuick sort
Quick sort
Jehat Hassan
 
Merge Sort
Merge SortMerge Sort
Merge Sort
Nikhil Sonkamble
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
Daffodil International University
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithmMohd Arif
 
Selection sort
Selection sortSelection sort
Selection sort
stella D
 
Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point
University of Science and Technology Chitttagong
 
Quick Sort
Quick SortQuick Sort
Quick Sort
Shweta Sahu
 
Shell sort
Shell sortShell sort
Shell sort
Rajendran
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
Anand Ingle
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
FarihaHabib123
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
SNJ Chaudhary
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithmsmultimedia9
 

What's hot (20)

Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Quick sort
Quick sortQuick sort
Quick sort
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Quick sort
Quick sortQuick sort
Quick sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Selection sort
Selection sortSelection sort
Selection sort
 
Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Shell sort
Shell sortShell sort
Shell sort
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 

Viewers also liked

Sorting algorithms v01
Sorting algorithms v01Sorting algorithms v01
Sorting algorithms v01
Dusan Vuckovic
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Vicente García Díaz
 
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
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
Kaushal Shah
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 

Viewers also liked (6)

Sorting algorithms v01
Sorting algorithms v01Sorting algorithms v01
Sorting algorithms v01
 
Sorting
SortingSorting
Sorting
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 

Similar to Sorting algorithms

Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
Dr.Umadevi V
 
Sorting
SortingSorting
Sorting
BHARATH KUMAR
 
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
 
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
 
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
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptx
DeepakM509554
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
LegesseSamuel
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Abdul Khan
 
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
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
Krish_ver2
 
Radix and Merge Sort
Radix and Merge SortRadix and Merge Sort
Radix and Merge Sort
Gelo Maribbay
 
A Survey of Adaptive QuickSort Algorithms
A Survey of Adaptive QuickSort AlgorithmsA Survey of Adaptive QuickSort Algorithms
A Survey of Adaptive QuickSort Algorithms
CSCJournals
 
Insertion sort
Insertion sortInsertion 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
IRJET Journal
 
Algorithms - "quicksort"
Algorithms - "quicksort"Algorithms - "quicksort"
Algorithms - "quicksort"
Ra'Fat Al-Msie'deen
 

Similar to 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
 
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
 
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"
 

More from Trupti Agrawal

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
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
Trupti Agrawal
 

More from Trupti Agrawal (7)

Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
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
 

Recently uploaded

Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 

Recently uploaded (20)

Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible 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