SlideShare a Scribd company logo
Agenda
• Sorting
• Definition & terminology
• Bubble sort
• Algorithm of bubble sort
• Function to implement bubble sort
• Advantages & disadvantages of bubble sort
• Efficiency of bubble sort
• Insertion sort
Sorting
• It is the process of arranging elements in
ascending or descending order of the key
values.
• Sorting techniques:
1. Bubble sort
2. Insertion sort
3. Selection sort
4. Quick sort
5. Merge sort
Bubble sort
• Bubble sort is a simple sorting algorithm. This
sorting algorithm is comparison-based
algorithm in which each pair of adjacent
elements is compared and the elements are
swapped if they are not in order.
• This algorithm is not suitable for large data
sets as its average and worst case complexity
are of Ο(n2) where n is the number of items.
Bubble sort
.
10 14 27 33 35
Algorithm for bubble sort
1. Start
2. Pass=1
3. i=0
4. if x[i]>x[i+1]
5. i=i+1
6. If i<=n-1-pass
goto step 4
7. Pass = Pass+1
8. if Pass < n
goto step 3
9. Stop.
Function to implement bubble sort
// A function to implement bubble sort
void bubbleSort (int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
Function to print an array
• /* Function to print an array */
• void printArray(int arr[], int size)
• {
• int i;
• for (i=0; i < size; i++)
• printf("%d ", arr[i]);
• printf("n");
• }
Advantages of bubble sort
• It is simple sorting method.
• No addition data structure is required.
• It is in-place sorting method.
• It is stable sorting method.
Disadvantages of bubble sort
• It is very inefficiency method. O(n2)
• Even of the element are in sorted order,
a[n-1] pass will be done.
Efficiency of bubble sort
Insertion sort
• Insertion sort works similarly as we sort cards in a
card game.
• We assume that the first card is already sorted then,
we select an unsorted card. If the unsorted card is
greater than the card in hand, it is placed on the
right otherwise, to the left. In the same way, other
unsorted cards are taken and put at their right place.
• Insertion sort is a sorting algorithm that places an
unsorted element at its suitable place in each
iteration.
Algorithm for Insertion sort
1. Start
2. j=1 i.e unsorted position starts from 1
3. Key=x[j]
4. i=j-1 i.e. sorted position starts from1
5. While x[i]>key and i>=0
x[i+1]=key i.e. insert key in correct position
6. j=j+1 next unsorted element
7. If j<n
goto step 3
6. Stop.
Efficiency of insertion sort
.
/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
/* Move elements of arr[0..i-1], that are greater than key, to one position
ahead of their current position */
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
Advantages and disadvantages of Insertion sort
Advantages :
• It is simple sorting method.
• No addition data structure is required.
• It is in-place sorting method.
• It is stable sorting method.
• Best case time complexity is o(n)
• Most suitable sorting method is if the element are already
sorted.
Disadvantages:
• worst case time complexity is O(n2)

More Related Content

Similar to Chapter2.1 .pptx

Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
Dr.Umadevi V
 
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
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
Dabbal Singh Mahara
 
Data structures and algorithms - sorting algorithms
Data structures and algorithms - sorting algorithmsData structures and algorithms - sorting algorithms
Data structures and algorithms - sorting algorithms
Abimbola Idowu
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
ParagAhir1
 
CSPC/ PPS Sorting methods
CSPC/ PPS Sorting methodsCSPC/ PPS Sorting methods
CSPC/ PPS Sorting methods
Ankur Srivastava
 
Sorting
SortingSorting
Sorting
Samsil Arefin
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
chouguleamruta24
 
data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptx
Mohammed472103
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
SwatiHans10
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
Kamal Singh Lodhi
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
VarchasvaTiwari2
 
1.4 Sorting.pptx
1.4 Sorting.pptx1.4 Sorting.pptx
1.4 Sorting.pptx
Sujan527908
 
Basic Sorting algorithms csharp
Basic Sorting algorithms csharpBasic Sorting algorithms csharp
Basic Sorting algorithms csharp
Micheal Ogundero
 
Java presentation on insertion sort
Java presentation on insertion sortJava presentation on insertion sort
Java presentation on insertion sort_fahad_shaikh
 
Chapter Two.pdf
Chapter Two.pdfChapter Two.pdf
Chapter Two.pdf
abay golla
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
CHANDAN KUMAR
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
ThenmozhiK5
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs7
 

Similar to Chapter2.1 .pptx (20)

Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
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
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
 
Data structures and algorithms - sorting algorithms
Data structures and algorithms - sorting algorithmsData structures and algorithms - sorting algorithms
Data structures and algorithms - sorting algorithms
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
CSPC/ PPS Sorting methods
CSPC/ PPS Sorting methodsCSPC/ PPS Sorting methods
CSPC/ PPS Sorting methods
 
Sorting
SortingSorting
Sorting
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 
data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptx
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
1.4 Sorting.pptx
1.4 Sorting.pptx1.4 Sorting.pptx
1.4 Sorting.pptx
 
Basic Sorting algorithms csharp
Basic Sorting algorithms csharpBasic Sorting algorithms csharp
Basic Sorting algorithms csharp
 
Java presentation on insertion sort
Java presentation on insertion sortJava presentation on insertion sort
Java presentation on insertion sort
 
Chapter Two.pdf
Chapter Two.pdfChapter Two.pdf
Chapter Two.pdf
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
 

Recently uploaded

"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
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
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
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
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 

Recently uploaded (20)

"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
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
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 

Chapter2.1 .pptx

  • 1. Agenda • Sorting • Definition & terminology • Bubble sort • Algorithm of bubble sort • Function to implement bubble sort • Advantages & disadvantages of bubble sort • Efficiency of bubble sort • Insertion sort
  • 2. Sorting • It is the process of arranging elements in ascending or descending order of the key values. • Sorting techniques: 1. Bubble sort 2. Insertion sort 3. Selection sort 4. Quick sort 5. Merge sort
  • 3. Bubble sort • Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order. • This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2) where n is the number of items.
  • 5. . 10 14 27 33 35
  • 6. Algorithm for bubble sort 1. Start 2. Pass=1 3. i=0 4. if x[i]>x[i+1] 5. i=i+1 6. If i<=n-1-pass goto step 4 7. Pass = Pass+1 8. if Pass < n goto step 3 9. Stop.
  • 7. Function to implement bubble sort // A function to implement bubble sort void bubbleSort (int arr[], int n) { int i, j; for (i = 0; i < n-1; i++) // Last i elements are already in place for (j = 0; j < n-i-1; j++) if (arr[j] > arr[j+1]) swap(&arr[j], &arr[j+1]); }
  • 8. Function to print an array • /* Function to print an array */ • void printArray(int arr[], int size) • { • int i; • for (i=0; i < size; i++) • printf("%d ", arr[i]); • printf("n"); • }
  • 9. Advantages of bubble sort • It is simple sorting method. • No addition data structure is required. • It is in-place sorting method. • It is stable sorting method.
  • 10. Disadvantages of bubble sort • It is very inefficiency method. O(n2) • Even of the element are in sorted order, a[n-1] pass will be done.
  • 12. Insertion sort • Insertion sort works similarly as we sort cards in a card game. • We assume that the first card is already sorted then, we select an unsorted card. If the unsorted card is greater than the card in hand, it is placed on the right otherwise, to the left. In the same way, other unsorted cards are taken and put at their right place. • Insertion sort is a sorting algorithm that places an unsorted element at its suitable place in each iteration.
  • 13.
  • 14.
  • 15. Algorithm for Insertion sort 1. Start 2. j=1 i.e unsorted position starts from 1 3. Key=x[j] 4. i=j-1 i.e. sorted position starts from1 5. While x[i]>key and i>=0 x[i+1]=key i.e. insert key in correct position 6. j=j+1 next unsorted element 7. If j<n goto step 3 6. Stop.
  • 17. . /* Function to sort an array using insertion sort*/ void insertionSort(int arr[], int n) { int i, key, j; for (i = 1; i < n; i++) { key = arr[i]; j = i - 1; /* Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position */ while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = key; } }
  • 18. Advantages and disadvantages of Insertion sort Advantages : • It is simple sorting method. • No addition data structure is required. • It is in-place sorting method. • It is stable sorting method. • Best case time complexity is o(n) • Most suitable sorting method is if the element are already sorted. Disadvantages: • worst case time complexity is O(n2)