SlideShare a Scribd company logo
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.
Sorting
Sorting
Sorting
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
Sorting
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

PPTX
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
Vivek487417
 
PPTX
Sorting And Type of Sorting
MITULJAMANG
 
PDF
Sorting algorithms bubble sort to merge sort.pdf
AyeshaMazhar21
 
PPTX
Sorting
Ashim Lamichhane
 
ODP
Data operatons & searching and sorting algorithms
Anushdika Jeganathan
 
PPTX
sorting-160810203705.pptx
VarchasvaTiwari2
 
PPTX
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Radhika Talaviya
 
PPTX
Sorting algorithms
Maher Alshammari
 
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
Vivek487417
 
Sorting And Type of Sorting
MITULJAMANG
 
Sorting algorithms bubble sort to merge sort.pdf
AyeshaMazhar21
 
Data operatons & searching and sorting algorithms
Anushdika Jeganathan
 
sorting-160810203705.pptx
VarchasvaTiwari2
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Radhika Talaviya
 
Sorting algorithms
Maher Alshammari
 

Similar to Sorting (20)

PPT
Data Structure (MC501)
Kamal Singh Lodhi
 
PPTX
DFC30233_CHAPTER 6 (1).pptxxxxxxxxxxxxxxxxxxxxxxxx
rajinevitable05
 
PPTX
sorting-160810203705.pptx
AnSHiKa187943
 
PPTX
SORTING techniques.pptx
Dr.Shweta
 
PPTX
Data Structures Unit 2 FINAL presentation.pptx
dilipd20
 
PPTX
ALGORITHMS introduction presentation.pptx
IrfanAli169438
 
PPTX
QUICK SORT_DAA.pptx
TLRTHR
 
PPTX
Sorting Algorithms to arrange data in particular format
itsusamazahid
 
PPTX
Sorting
FahadSaeed39
 
PPTX
sortting 2 sderftgyurvtynurftgyhujse.pptx
IfraLuqman
 
PPTX
Quick-Sort Algorithm and pivot selection
SubhranjaliBehera
 
PPTX
ds bridge.pptx
GOOGLEINTERNETCAFE1
 
PPT
Data Structures 6
Dr.Umadevi V
 
PPTX
1.4 Sorting.pptx
Sujan527908
 
PPTX
2.Problem Solving Techniques and Data Structures.pptx
Ganesh Bhosale
 
PPTX
Data structure and algorithms
technologygyan
 
PPTX
what is sorting algorithm and implementation.pptx
TanaTech
 
PPTX
Data structfghz€zdsrgnhlhlfdshllures.pptx
srahul53094
 
PPTX
Searching searching in in arrays arrays.pptx
Sahar160629
 
ODP
Sorting Algorithm
Abu Shaik saurab
 
Data Structure (MC501)
Kamal Singh Lodhi
 
DFC30233_CHAPTER 6 (1).pptxxxxxxxxxxxxxxxxxxxxxxxx
rajinevitable05
 
sorting-160810203705.pptx
AnSHiKa187943
 
SORTING techniques.pptx
Dr.Shweta
 
Data Structures Unit 2 FINAL presentation.pptx
dilipd20
 
ALGORITHMS introduction presentation.pptx
IrfanAli169438
 
QUICK SORT_DAA.pptx
TLRTHR
 
Sorting Algorithms to arrange data in particular format
itsusamazahid
 
Sorting
FahadSaeed39
 
sortting 2 sderftgyurvtynurftgyhujse.pptx
IfraLuqman
 
Quick-Sort Algorithm and pivot selection
SubhranjaliBehera
 
ds bridge.pptx
GOOGLEINTERNETCAFE1
 
Data Structures 6
Dr.Umadevi V
 
1.4 Sorting.pptx
Sujan527908
 
2.Problem Solving Techniques and Data Structures.pptx
Ganesh Bhosale
 
Data structure and algorithms
technologygyan
 
what is sorting algorithm and implementation.pptx
TanaTech
 
Data structfghz€zdsrgnhlhlfdshllures.pptx
srahul53094
 
Searching searching in in arrays arrays.pptx
Sahar160629
 
Sorting Algorithm
Abu Shaik saurab
 
Ad

More from Amar Jukuntla (19)

PDF
Singly linked list
Amar Jukuntla
 
PPTX
Types of files
Amar Jukuntla
 
PPTX
Hashing
Amar Jukuntla
 
PPTX
Planning
Amar Jukuntla
 
PDF
Unit 2
Amar Jukuntla
 
PDF
Problem Solving
Amar Jukuntla
 
PPTX
Intelligent Agents
Amar Jukuntla
 
PPTX
Introduction
Amar Jukuntla
 
PPTX
DFS
Amar Jukuntla
 
PPTX
Sorting
Amar Jukuntla
 
PPTX
Nature of open source
Amar Jukuntla
 
PPTX
Linux Directory System: Introduction
Amar Jukuntla
 
PPTX
Introduction to Data Structures
Amar Jukuntla
 
PPTX
Learning
Amar Jukuntla
 
PDF
First Order Logic resolution
Amar Jukuntla
 
PDF
First Order Logic
Amar Jukuntla
 
PDF
Agents1
Amar Jukuntla
 
PPTX
Need of object oriented programming
Amar Jukuntla
 
Singly linked list
Amar Jukuntla
 
Types of files
Amar Jukuntla
 
Hashing
Amar Jukuntla
 
Planning
Amar Jukuntla
 
Problem Solving
Amar Jukuntla
 
Intelligent Agents
Amar Jukuntla
 
Introduction
Amar Jukuntla
 
Sorting
Amar Jukuntla
 
Nature of open source
Amar Jukuntla
 
Linux Directory System: Introduction
Amar Jukuntla
 
Introduction to Data Structures
Amar Jukuntla
 
Learning
Amar Jukuntla
 
First Order Logic resolution
Amar Jukuntla
 
First Order Logic
Amar Jukuntla
 
Agents1
Amar Jukuntla
 
Need of object oriented programming
Amar Jukuntla
 
Ad

Recently uploaded (20)

PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PDF
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PPTX
How to Apply for a Job From Odoo 18 Website
Celine George
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
Basics and rules of probability with real-life uses
ravatkaran694
 
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
How to Apply for a Job From Odoo 18 Website
Celine George
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 

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.
  • 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
  • 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.