SlideShare a Scribd company logo
2
Most read
8
Most read
21
Most read
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

PPTX
Searching & Sorting Algorithms
PPTX
Sorting Algorithms
PPTX
Artificial Neural Network ANN
PPTX
Quick sort
PDF
Data structure ppt
PDF
Sorting Algorithms
PPTX
sorting and its types
PPTX
Types of attacks
Searching & Sorting Algorithms
Sorting Algorithms
Artificial Neural Network ANN
Quick sort
Data structure ppt
Sorting Algorithms
sorting and its types
Types of attacks

What's hot (20)

PPTX
Insertion sort
PPTX
Insertion sort
PDF
Binary Search - Design & Analysis of Algorithms
PPTX
Quick Sort
PPTX
Quick sort-Data Structure
PPTX
Arrays In C++
PPTX
Merge sort algorithm power point presentation
PPTX
Doubly Linked List
PPTX
Quick sort
PPTX
Selection sort
PPTX
Insertion sort
PPTX
Binary search
PPTX
Quick sort
PPSX
Data Structure (Queue)
PPTX
linked list
PPT
Bubble sort
PPT
Algorithm: Quick-Sort
PPTX
heap Sort Algorithm
PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
Insertion sort
Insertion sort
Binary Search - Design & Analysis of Algorithms
Quick Sort
Quick sort-Data Structure
Arrays In C++
Merge sort algorithm power point presentation
Doubly Linked List
Quick sort
Selection sort
Insertion sort
Binary search
Quick sort
Data Structure (Queue)
linked list
Bubble sort
Algorithm: Quick-Sort
heap Sort Algorithm
Data Structures - Lecture 8 [Sorting Algorithms]
Ad

Similar to Sorting algorithms (20)

PPT
Data Structures 6
PPT
Lecture_4 (Sorting Algorithms) before mids - Copy.ppt
DOCX
Sorting
PPTX
Parallel Sorting Algorithms. Quicksort. Merge sort. List Ranking
PPTX
SORT AND SEARCH ARRAY WITH WITH C++.pptx
PPTX
what is sorting algorithm and implementation.pptx
PPTX
Algorithms and Data Structures for Sorting Numerical Data
PPT
Data Structure (MC501)
PPTX
daa unit 1.pptx
PPTX
Analysis of Algorithm (Bubblesort and Quicksort)
PPT
Presentation on binary search, quick sort, merge sort and problems
PPTX
Searching searching in in arrays arrays.pptx
PDF
Chapter 8 advanced sorting and hashing for print
PDF
1_Algo_lyst1729856407228_lyst1736581584300.pdf
PPTX
Sorting-Algorithms-A-Comprehensive-Guide.pptx
PPTX
CSPC/ PPS Sorting methods
PPTX
Analysis and Design of Algorithms -Sorting Algorithms and analysis
PDF
Class13_Quicksort_Algorithm.pdf
PPT
Searching Sorting-SELECTION ,BUBBBLE.ppt
PPTX
Algorithms and Data Structures - Parahyangan Catholic University Credit Lionov
Data Structures 6
Lecture_4 (Sorting Algorithms) before mids - Copy.ppt
Sorting
Parallel Sorting Algorithms. Quicksort. Merge sort. List Ranking
SORT AND SEARCH ARRAY WITH WITH C++.pptx
what is sorting algorithm and implementation.pptx
Algorithms and Data Structures for Sorting Numerical Data
Data Structure (MC501)
daa unit 1.pptx
Analysis of Algorithm (Bubblesort and Quicksort)
Presentation on binary search, quick sort, merge sort and problems
Searching searching in in arrays arrays.pptx
Chapter 8 advanced sorting and hashing for print
1_Algo_lyst1729856407228_lyst1736581584300.pdf
Sorting-Algorithms-A-Comprehensive-Guide.pptx
CSPC/ PPS Sorting methods
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Class13_Quicksort_Algorithm.pdf
Searching Sorting-SELECTION ,BUBBBLE.ppt
Algorithms and Data Structures - Parahyangan Catholic University Credit Lionov
Ad

More from Trupti Agrawal (7)

PPTX
Trees (data structure)
PPT
Searching algorithms
PPT
Searching algorithms
PPT
Linked list
PPT
Stacks and queues
PPTX
PPTX
Data structure and algorithm
Trees (data structure)
Searching algorithms
Searching algorithms
Linked list
Stacks and queues
Data structure and algorithm

Recently uploaded (20)

PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Pre independence Education in Inndia.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Institutional Correction lecture only . . .
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
Renaissance Architecture: A Journey from Faith to Humanism
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
human mycosis Human fungal infections are called human mycosis..pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Pre independence Education in Inndia.pdf
Cell Types and Its function , kingdom of life
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Abdominal Access Techniques with Prof. Dr. R K Mishra
O7-L3 Supply Chain Operations - ICLT Program
VCE English Exam - Section C Student Revision Booklet
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Institutional Correction lecture only . . .
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
TR - Agricultural Crops Production NC III.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Supply Chain Operations Speaking Notes -ICLT Program

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