SlideShare a Scribd company logo
1 of 57
Sorting
Sorting is any process of arranging items 
systematically. 
ascending :A to Z, 0 to 9
descending order: Z to A, 9 to 0
For dates and times, ascending means that earlier values 
precede later ones e.g. 1/1/2000 will sort ahead of 
1/1/2001
Sorting
Four algorithms are described
1. Selection sort
2. Bubble sort
3. Insertion Sort
4. Merge Sort (Assignment)
Selection Sort
 In the first pass, each item in the list is
compared with the first item in the list
 If the first item in the list is bigger then the
item being compared then they are swapped.
Selection Sort
7 5 9 6 1 8 2 0 3 4
1st Comparison
Swap
Selection Sort
5 7 9 6 1 8 2 0 3 4
Selection Sort
5 7 9 6 1 8 2 0 3 4
2nd Comparison
Selection Sort
5 7 9 6 1 8 2 0 3 4
3rd Comparison
Selection Sort
5 7 9 6 1 8 2 0 3 4
4th Comparison
Swap
Selection Sort
1 7 9 6 5 8 2 0 3 4
5th Comparison
Selection Sort
1 7 9 6 5 8 2 0 3 4
6th Comparison
Selection Sort
1 7 9 6 5 8 2 0 3 4
7th Comparison
Swap
Selection Sort
0 7 9 6 5 8 2 1 3 4
Selection Sort
0 7 9 6 5 8 2 1 3 4
8th Comparison
Selection Sort
0 7 9 6 5 8 2 1 3 4
9th Comparison
Selection Sort
0 7 9 6 5 8 2 1 3 4
1st
Comparison
Selection Sort
0 7 9 6 5 8 2 1 3 4
2nd Comparison
Swap
Selection Sort
0 6 9 7 5 8 2 1 3 4
Selection Sort
0 6 9 7 5 8 2 1 3 4
3rd Comparison
Swap
Selection Sort
0 5 9 7 6 8 2 1 3 4
Selection Sort
0 5 9 7 6 8 2 1 3 4
4th Comparison
Selection Sort
0 5 9 7 6 8 2 1 3 4
5th Comparison
Swap
Selection Sort
0 2 9 7 6 8 5 1 3 4
Selection Sort
0 2 9 7 6 8 5 1 3 4
And so on…
Selection Sort
until…
0 1 2 3 4 5 6 7 8 9
Selection Sort
Must make N-1 passes through list even when
fully sorted or partially sorted
Selection Sort Algorithm
Step 1. [Starting Selection Loop]
Repeat step 2 For S=1 to N-1 by 1
Step 2. [Starting Selection Loop]
Repeat step 3 For C=S+1 to N by 1
Step 3. [Compare Element]
If(A[s]> A[c]) Then
Temp=A[S]
A[S]=A[C]
A[S]=Temp
Exit
Bubble Sort
 Bubble sort is a simple algorithm that
repeatedly steps through the list, compares
adjacent pairs and swap them if they are in
wrong order. The pass through the list is
repeated until list is sorted.
Bubble sort Example
7 5 9 6 1 8 2 0 3 4
First Comparison
Swap
Bubble sort
5 7 9 6 1 8 2 0 3 4
Bubble sort
5 7 9 6 1 8 2 0 3 4
Second Comparison
Bubble sort
5 7 9 6 1 8 2 0 3 4
Third Comparison
Swap
Bubble sort
5 7 6 9 1 8 2 0 3 4
Bubble sort
5 7 6 9 1 8 2 0 3 4
Fourth Comparison
Swap
Bubble sort
5 7 6 1 9 8 2 0 3 4
Bubble sort
5 7 6 1 9 8 2 0 3 4
Fifth Comparison
Swap
Bubble sort
5 7 6 1 8 9 2 0 3 4
Bubble sort
5 7 6 1 8 9 2 0 3 4
Sixth Comparison
Swap
Bubble sort
5 7 6 1 8 2 9 0 3 4
Bubble sort
5 7 6 1 8 2 9 0 3 4
Seventh Comparison
Swap
Bubble sort
5 7 6 1 8 2 0 9 3 4
Bubble sort
5 7 6 1 8 2 0 9 3 4
8th Comparison
Swap
Bubble sort
5 7 6 1 8 2 0 3 9 4
Bubble sort
5 7 6 1 8 2 0 3 9 4
9th Comparison
Swap
Bubble sort
5 7 6 1 8 2 0 3 4 9
Notice… we are sorting list into an ascending list. The largest number is now
at the end of the list…where it should be!
This completes the first pass through the list.
Bubble sort
5 7 6 1 8 2 0 3 4 9
The process begins again.
1st Comparison Second Pass
Bubble sort
5 7 6 1 8 2 0 3 4 9
2nd Comparison
Swap
Second Pass
Bubble sort
5 6 7 1 8 2 0 3 4 9
Second Pass
Bubble sort
5 6 7 1 8 2 0 3 4 9
3rd
Comparison
Swap
Second Pass
Bubble sort
5 6 1 7 8 2 0 3 4 9
Second Pass
Bubble sort
5 6 1 7 8 2 0 3 4 9
4th Comparison Second Pass
Bubble sort
5 6 1 7 8 2 0 3 4 9
5th Comparison
Swap
Second Pass
Bubble Sort
Write an algorithm to sort an array A consisting of
N elements in ascending order using bubble sort
Suppose variable U represents the control variable
for upper loop to control the number of iteration .
Similarly , variable I represent the control variable
for inner loop that scans the array starting from
element A[1] to A[U]
Cont.……
1. Set U=N
2. [Upper loop]
Repeat step 3 to 7 while (U>=1)
3. Set I=1
4. [Inner Loop]
Repeat step 5 to 6 while (I<=U)
5. IF A[I]>A[I+1]
T=A[I]
A[I]=A[I+1]
A[I+1]=T END IF
6. I=I+1
7. U=U-1
8. EXIT
Insertion Sort
 In insertion sort algorithm, compare the value
until all the previous value is lesser than
compared value. Insertion sort is more
efficient than bubble sort because in insertion
sort the elements comparisons are lesser as
compared to bubble sort. Insertion sort is
suitable for small data.
Insertion Sort Example
Algorithm for insertion sort
Write an algorithm to sort an array A
consisting of N elements in ascending order
using Insertion sort method
Suppose variable U represents the control
variable for upper loop to control the number
of iteration . Similarly , variable I represent
the control variable for inner loop that scans
the array starting from element A[1] to A[U]
CONT.….
1. REPEAT STEP 2 TO 6 FOR C=1 TO N
2. TEMP=A[C]
3. L=C
4. REPEAT STEP 3 to 7 WHILE (L>0 AND TEMP<A[L-1])
5. A[L]=A[L-1] [END OF STEP 4 INNER LOOP]
6. A[L]=TEMP [INSERT VALUE]
[END OF STEP 1 UPPER LOOP]
7. EXIT

More Related Content

Similar to Sorting algorithm

Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsAakash deep Singhal
 
Sorting Data structure And Algorithm.pptx
Sorting Data structure And Algorithm.pptxSorting Data structure And Algorithm.pptx
Sorting Data structure And Algorithm.pptxsubhanalichand514
 
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
 
Bubble sorting lab manual
Bubble sorting lab manualBubble sorting lab manual
Bubble sorting lab manualmaamir farooq
 
one main advantage of bubble sort as compared to others
one main advantage of bubble sort as compared to othersone main advantage of bubble sort as compared to others
one main advantage of bubble sort as compared to othersAjay Chimmani
 
366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)Neil Soliven
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithmsZaid Hameed
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & AlgorithumsAin-ul-Moiz Khawaja
 
Sorting and Searching - Data Structure - Notes
Sorting and Searching - Data Structure - NotesSorting and Searching - Data Structure - Notes
Sorting and Searching - Data Structure - NotesOmprakash Chauhan
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingEduardo Bergavera
 

Similar to Sorting algorithm (20)

Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
 
Data Structures and Algorithms
Data Structures and AlgorithmsData Structures and Algorithms
Data Structures and Algorithms
 
Sorting
SortingSorting
Sorting
 
Sorting Data structure And Algorithm.pptx
Sorting Data structure And Algorithm.pptxSorting Data structure And Algorithm.pptx
Sorting Data structure And Algorithm.pptx
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Bubble sorting lab manual
Bubble sorting lab manualBubble sorting lab manual
Bubble sorting lab manual
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
 
one main advantage of bubble sort as compared to others
one main advantage of bubble sort as compared to othersone main advantage of bubble sort as compared to others
one main advantage of bubble sort as compared to others
 
Sorting algos
Sorting algosSorting algos
Sorting algos
 
366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Algorithm and Programming (Sorting)
Algorithm and Programming (Sorting)Algorithm and Programming (Sorting)
Algorithm and Programming (Sorting)
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
 
Presentation about Bubble Sort
Presentation about Bubble SortPresentation about Bubble Sort
Presentation about Bubble Sort
 
sorting.pptx
sorting.pptxsorting.pptx
sorting.pptx
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
 
Sorting and Searching - Data Structure - Notes
Sorting and Searching - Data Structure - NotesSorting and Searching - Data Structure - Notes
Sorting and Searching - Data Structure - Notes
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 

Recently uploaded

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
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
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
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
 
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
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
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
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
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
 

Recently uploaded (20)

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
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
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
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
 
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
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
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
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
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
 

Sorting algorithm