SlideShare a Scribd company logo
1 of 23
Sorting Techniques
Amar Jukuntla, Assistant Professor,
VFSTR University
Index
• Introduction
• Selection Sort
• Insertion Sort
• Bubble Sort
• Quick Sort
• Merge Sort
Introduction
•Sorting is a process that organizes a collection of data into
either ascending or descending order.
• An internal sort requires that the collection of data fit
entirely in the computer’s main memory.
• We can use an external sort when the collection of data
cannot fit in the computer’s main memory all at once but must
reside in secondary storage such as on a disk.
• A comparison-based sorting algorithm makes ordering
decisions only on the basis of comparisons.
Internal or External Sort
• In an internal sort, the list of records is small enough to be
maintained entirely in physical memory for the duration of the sort.
• In an external sort, the list of records will not fit entirely
into physical memory at once. In that case, the records are kept in disk
files and only a selection of them are resident in physical memory at
any given time.
Selection Sort
• The list is divided into two sublists, sorted and unsorted, which are
divided by an imaginary wall.
• Find the smallest element from the unsorted sublist and swap
it with the element at the beginning of the unsorted data.
• After each selection and swapping, the imaginary wall
between the two sublists move one element ahead, increasing the number of
sorted elements and decreasing the number of unsorted ones.
• Each time, move one element from the unsorted sublist to the
sorted sublist, we say that we have completed a sort pass.
• A list of n elements requires n-1 passes to completely rearrange the data.
Selection Sort
23 78 45 8 32 56
8 78 45 23 32 56
8 23 45 78 32 56
8 23 32 78 45 56
8 23 32 45 78 56
8 23 32 45 56 78
Original List
After pass 1
After pass 2
After pass 3
After pass 4
After pass 5
Sorted Unsorted
Insertion Sort
• Insertion sort is a simple sorting algorithm that is appropriate for
small inputs.
• Most common sorting technique used by card players.
• The list is divided into two parts: sorted and unsorted.
• In each pass, the first element of the unsorted part is
picked up, transferred to the sorted sublist, and inserted at the
appropriate place.
• A list of n elements will take at most n-1 passes to sort the data.
Insertion Sort
Original List
After pass 1
After pass 2
After pass 3
After pass 4
After pass 5
23 78 45 8 32 56
23 78 45 8 32 56
23 45 78 8 32 56
8 23 45 78 32 56
8 23 32 45 78 56
8 23 32 45 56 78
Sorted Unsorted
Bubble Sort
• The list is divided into two sublists: sortedand unsorted.
• The smallest element is bubbled from the unsorted list and moved
to the sorted sublist.
• After that, the wall moves one element ahead, increasing the number
of sorted elements and decreasing the number of unsorted ones.
• Each time an element moves from the unsorted part to the sorted
part one sort pass is completed.
• Given a list of n elements, bubble sort requires up to n-1 passes to sort the
data.
Quick Sort
• Quicksort is based on the divide-and-conquer paradigm.
• It picks an element as pivot and partitions the given array
around the picked pivot.
• There are many different versions of quickSort that pick pivot in
different ways.
• Always pick first element as pivot.
• Always pick last element as pivot (implemented below)
• Pick a random element as pivot.
• Pick median as pivot.
Quick
PSUEDO
CODE
Example
78 21 14 97 87 62 74 85 76 45 84 22
78 21 14 22 87 62 74 85 76 45 84 97
78 21 14 22 45 62 74 85 76 87 84 97
78 21 14 22 45 62 74 76 85 87 84 97
76 21 14 22 45 62 74 78 85 87 84 97
Merge Sort
• Merge Sort is a Divide and Conquer algorithm.
• It divides input array in two halves, calls itself for the two halves and
then merges the two sorted halves.
Algorithm
Sorting
Sorting

More Related Content

Similar to Sorting

Similar to Sorting (20)

sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
SORTING techniques.pptx
SORTING techniques.pptxSORTING techniques.pptx
SORTING techniques.pptx
 
QUICK SORT_DAA.pptx
QUICK SORT_DAA.pptxQUICK SORT_DAA.pptx
QUICK SORT_DAA.pptx
 
Sorting
SortingSorting
Sorting
 
ds bridge.pptx
ds bridge.pptxds bridge.pptx
ds bridge.pptx
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
1.4 Sorting.pptx
1.4 Sorting.pptx1.4 Sorting.pptx
1.4 Sorting.pptx
 
Data structure and algorithms
Data structure and algorithmsData structure and algorithms
Data structure and algorithms
 
Sorting Algorithm
Sorting AlgorithmSorting Algorithm
Sorting Algorithm
 
Complexity in array
Complexity in arrayComplexity in array
Complexity in array
 
Quick sort algorithm
Quick sort algorithmQuick sort algorithm
Quick sort algorithm
 
Quick sort
Quick sortQuick sort
Quick sort
 
Sorting.pptx
Sorting.pptxSorting.pptx
Sorting.pptx
 
Ch2 Part III-Advanced Sorting algorithms.pptx
Ch2 Part III-Advanced Sorting algorithms.pptxCh2 Part III-Advanced Sorting algorithms.pptx
Ch2 Part III-Advanced Sorting algorithms.pptx
 
Merge sort
Merge sortMerge sort
Merge sort
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
DS Module1 (1).pptx
DS Module1 (1).pptxDS Module1 (1).pptx
DS Module1 (1).pptx
 
SearchAlgorithm.pdf
SearchAlgorithm.pdfSearchAlgorithm.pdf
SearchAlgorithm.pdf
 
Sorting Algorithm
Sorting AlgorithmSorting Algorithm
Sorting Algorithm
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 

More from Amar Jukuntla (19)

Singly linked list
Singly linked listSingly linked list
Singly linked list
 
Types of files
Types of filesTypes of files
Types of files
 
Hashing
HashingHashing
Hashing
 
Planning
Planning Planning
Planning
 
Unit 2
Unit 2Unit 2
Unit 2
 
Problem Solving
Problem Solving Problem Solving
Problem Solving
 
Intelligent Agents
Intelligent Agents Intelligent Agents
Intelligent Agents
 
Introduction
IntroductionIntroduction
Introduction
 
DFS
DFSDFS
DFS
 
Sorting
SortingSorting
Sorting
 
Nature of open source
Nature of open sourceNature of open source
Nature of open source
 
Linux Directory System: Introduction
Linux Directory System: IntroductionLinux Directory System: Introduction
Linux Directory System: Introduction
 
Introduction to Data Structures
Introduction to Data StructuresIntroduction to Data Structures
Introduction to Data Structures
 
Learning
LearningLearning
Learning
 
First Order Logic resolution
First Order Logic resolutionFirst Order Logic resolution
First Order Logic resolution
 
First Order Logic
First Order LogicFirst Order Logic
First Order Logic
 
A*
A*A*
A*
 
Agents1
Agents1Agents1
Agents1
 
Need of object oriented programming
Need of object oriented programmingNeed of object oriented programming
Need of object oriented programming
 

Recently uploaded

Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 

Recently uploaded (20)

Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 

Sorting

  • 1. Sorting Techniques Amar Jukuntla, Assistant Professor, VFSTR University
  • 2. Index • Introduction • Selection Sort • Insertion Sort • Bubble Sort • Quick Sort • Merge Sort
  • 3. Introduction •Sorting is a process that organizes a collection of data into either ascending or descending order. • An internal sort requires that the collection of data fit entirely in the computer’s main memory. • We can use an external sort when the collection of data cannot fit in the computer’s main memory all at once but must reside in secondary storage such as on a disk. • A comparison-based sorting algorithm makes ordering decisions only on the basis of comparisons.
  • 4. Internal or External Sort • In an internal sort, the list of records is small enough to be maintained entirely in physical memory for the duration of the sort. • In an external sort, the list of records will not fit entirely into physical memory at once. In that case, the records are kept in disk files and only a selection of them are resident in physical memory at any given time.
  • 5. Selection Sort • The list is divided into two sublists, sorted and unsorted, which are divided by an imaginary wall. • Find the smallest element from the unsorted sublist and swap it with the element at the beginning of the unsorted data. • After each selection and swapping, the imaginary wall between the two sublists move one element ahead, increasing the number of sorted elements and decreasing the number of unsorted ones. • Each time, move one element from the unsorted sublist to the sorted sublist, we say that we have completed a sort pass. • A list of n elements requires n-1 passes to completely rearrange the data.
  • 7. 23 78 45 8 32 56 8 78 45 23 32 56 8 23 45 78 32 56 8 23 32 78 45 56 8 23 32 45 78 56 8 23 32 45 56 78 Original List After pass 1 After pass 2 After pass 3 After pass 4 After pass 5 Sorted Unsorted
  • 8. Insertion Sort • Insertion sort is a simple sorting algorithm that is appropriate for small inputs. • Most common sorting technique used by card players. • The list is divided into two parts: sorted and unsorted. • In each pass, the first element of the unsorted part is picked up, transferred to the sorted sublist, and inserted at the appropriate place. • A list of n elements will take at most n-1 passes to sort the data.
  • 10. Original List After pass 1 After pass 2 After pass 3 After pass 4 After pass 5 23 78 45 8 32 56 23 78 45 8 32 56 23 45 78 8 32 56 8 23 45 78 32 56 8 23 32 45 78 56 8 23 32 45 56 78 Sorted Unsorted
  • 11. Bubble Sort • The list is divided into two sublists: sortedand unsorted. • The smallest element is bubbled from the unsorted list and moved to the sorted sublist. • After that, the wall moves one element ahead, increasing the number of sorted elements and decreasing the number of unsorted ones. • Each time an element moves from the unsorted part to the sorted part one sort pass is completed. • Given a list of n elements, bubble sort requires up to n-1 passes to sort the data.
  • 12.
  • 13.
  • 14.
  • 15. Quick Sort • Quicksort is based on the divide-and-conquer paradigm. • It picks an element as pivot and partitions the given array around the picked pivot. • There are many different versions of quickSort that pick pivot in different ways. • Always pick first element as pivot. • Always pick last element as pivot (implemented below) • Pick a random element as pivot. • Pick median as pivot.
  • 16. Quick
  • 18.
  • 19. Example 78 21 14 97 87 62 74 85 76 45 84 22 78 21 14 22 87 62 74 85 76 45 84 97 78 21 14 22 45 62 74 85 76 87 84 97 78 21 14 22 45 62 74 76 85 87 84 97 76 21 14 22 45 62 74 78 85 87 84 97
  • 20. Merge Sort • Merge Sort is a Divide and Conquer algorithm. • It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves.