SlideShare a Scribd company logo
1 of 48
Download to read offline
Sorting
Sorting
 Sorting is the process of placing elements from a
collection in some kind of order.
Types
 Bubble Sort.
 Insertion Sort.
 Selection Sort.
 Quick Sort.
 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.
How Bubble sort works
Bubble Sort Algorithm
Insertion Sort
Insertion sort is a sorting algorithm in which the elements are
transferred one at a time to the right position by comparison
but with left values.
This is an in-place comparison-based sorting
Algorithm.
Insertion Sort passes
11 2 1 9 6
Array[0] Array[1] Array[2] Array[3] Array[4]
11 2 1 9 6
Array[0] Array[1] Array[2] Array[3] Array[4]
2 11 1 9 6
Array[0] Array[1] Array[2] Array[3] Array[4]
2 11 1 9 6
Array[0] Array[1] Array[2] Array[3] Array[4]
2 1 11 9 6
Array[0] Array[1] Array[2] Array[3] Array[4]
2 1 11 9 6
Array[0] Array[1] Array[2] Array[3] Array[4]
1 2 11 9 6
Array[0] Array[1] Array[2] Array[3] Array[4]
1 2 9 11 6
Array[0] Array[1] Array[2] Array[3] Array[4]
1 2 11 9 6
Array[0] Array[1] Array[2] Array[3] Array[4]
1 2 9 11 6
Array[0] Array[1] Array[2] Array[3] Array[4]
1 2 9 6 11
Array[0] Array[1] Array[2] Array[3] Array[4]
1 2 9 6 11
Array[0] Array[1] Array[2] Array[3] Array[4]
1 2 6 9 11
Array[0] Array[1] Array[2] Array[3] Array[4]
Insertion Sort Algorithm & Program
Class assignment to write algorithm for this.
Selection Sort
 In selection sort, the smallest element is selected from the
unsorted array and swapped with the leftmost element,
and that element becomes a part of the sorted array. This
process continues moving unsorted array boundary by one
element to the right.
 Step 1 − Set MIN to location 0.
 Step 2 − Search the minimum element in the list.
 Step 3 − Swap with value at location MIN.
 Step 4 − Increment MIN to point to next element.
 Step 5 − Repeat until list is sorted.
Selection Sort Passes
Selection Sort Algorithm & Program
 Class assignment to write Code for this.
Merge Sort
 Merge sort is a sorting technique based on divide and
conquer technique.
 Merge sort first divides the array into equal halves and
then combines them in a sorted manner.
Merge Sort Passes
void MergeSort(int *a, int low, int high)
{
int mid;
if (low < high)
{
mid=(low+high)/2;
MergeSort(a, low, mid);
MergeSort(a, mid+1, high);
Merge(a, low, high, mid);
}
}
Low = 1,high= 8,1<8,mid = 4
Stack and PC
MergeSort(a, mid+1, high); [array,5,8]
Merge(a, low, high, mid); [array,1,8,4]
void MergeSort(int *a, int low, int high)
{
int mid;
if (low < high)
{
mid=(low+high)/2;
MergeSort(a, low, mid);
MergeSort(a, mid+1, high);
Merge(a, low, high, mid);
}
}
Low = 1,high= 4,1<4,mid = 2
Stack and PC
MergeSort(a, mid+1, high); [array,3,4]
Merge(a, low, high, mid); [array,1,4,2]
MergeSort(a, mid+1, high); [array,5,8]
Merge(a, low, high, mid); [array,1,8,4]
MergeSort(a, low, mid);
Array,low= 1Mid=4 => low=1 and high =4
MergeSort(a, low, mid);
Array,low= 1Mid=2 => low=1 and high =2
void MergeSort(int *a, int low, int high)
{
int mid;
if (low < high)
{
mid=(low+high)/2;
MergeSort(a, low, mid);
MergeSort(a, mid+1, high);
Merge(a, low, high, mid);
}
}
Low = 1,high= 2,1<2,mid = 1
Stack and PC
MergeSort(a, mid+1, high); [array,2,2]
Merge(a, low, high, mid); [array,1,2,1]
MergeSort(a, mid+1, high); [array,3,4]
Merge(a, low, high, mid); [array,1,4,2]
MergeSort(a, mid+1, high); [array,5,8]
Merge(a, low, high, mid); [array,1,8,4]
void MergeSort(int *a, int low, int high)
{
int mid;
if (low < high)
{
mid=(low+high)/2;
MergeSort(a, low, mid);
MergeSort(a, mid+1, high);
Merge(a, low, high, mid);
}
}
Low = 1,high= 1,1<1(false)
MergeSort(a, low, mid);
Array,low= 1Mid=1 => low=1 and high =1
Stack and PC
MergeSort(a, mid+1, high); [array,2,2]
Merge(a, low, high, mid); [array,1,2,1]
MergeSort(a, mid+1, high); [array,3,4]
Merge(a, low, high, mid); [array,1,4,2]
MergeSort(a, mid+1, high); [array,5,8]
Merge(a, low, high, mid); [array,1,8,4]
void MergeSort(int *a, int low, int high)
{
int mid;
if (low < high)
{
mid=(low+high)/2;
MergeSort(a, low, mid);
MergeSort(a, mid+1, high);
Merge(a, low, high, mid);
}
}
Low = 2,high= 2,<2,false
MergeSort(a, mid+1, high);
Array,Mid=2,high=2 => low=2 and high =2
Merge(a, low, high, mid); [array,1,2,1]
void MergeSort(int *a, int low, int high)
{
int mid;
if (low < high)
{
mid=(low+high)/2;
MergeSort(a, low, mid);
MergeSort(a, mid+1, high);
Merge(a, low, high, mid);
}
}
low =3 ,high= 4,3<4,mid = 3
MergeSort(a, mid+1, high);
Array,mid+1[3] ,high 4 => low=3 and high =4
Merge(a, low, high, mid); [array,1,2,1]
void MergeSort(int *a, int low, int high)
{
int mid;
if (low < high)
{
mid=(low+high)/2;
MergeSort(a, low, mid);
MergeSort(a, mid+1, high);
Merge(a, low, high, mid);
}
}
low =3 ,high= 4,3<4,mid = 3
MergeSort(a, mid+1, high);
Array,mid+1[3] ,high 4 => low=3 and high =4
Stack and PC
MergeSort(a, mid+1, high); [array,4,4]
Merge(a, low, high, mid); [array,3,4,3]
Merge(a, low, high, mid); [array,1,4,2]
MergeSort(a, mid+1, high); [array,5,8]
Merge(a, low, high, mid); [array,1,8,4]
void MergeSort(int *a, int low, int high)
{
int mid;
if (low < high)
{
mid=(low+high)/2;
MergeSort(a, low, mid);
MergeSort(a, mid+1, high);
Merge(a, low, high, mid);
}
}
MergeSort(a, low, mid);
Array,low=3,Mid=3 => low=3 and high =3
low =3 ,high= 3,3<3,false
Stack and PC
MergeSort(a, mid+1, high); [array,4,4]
Merge(a, low, high, mid); [array,3,4,3]
Merge(a, low, high, mid); [array,1,4,2]
MergeSort(a, mid+1, high); [array,5,8]
Merge(a, low, high, mid); [array,1,8,4]
Merge(a, low, high, mid); [array,1,2,1]
void MergeSort(int *a, int low, int high)
{
int mid;
if (low < high)
{
mid=(low+high)/2;
MergeSort(a, low, mid);
MergeSort(a, mid+1, high);
Merge(a, low, high, mid);
}
}
Low = 4,high= 2,4<4,false
MergeSort(a, mid+1, high);
Array,Mid=3+1[4] high=4 => low=4 and high =4
Merge(a, low, high, mid); [array,3,4,3]
Merge(a, low, high, mid); [array,1,4,2]
void Merge(int *a, int low, int high, int mid)
{// We have low to mid and mid+1 to high already
sorted.
int i, j, k, temp[high-low+1];
i = low;
k = 0;
j = mid + 1;
// Merge the two parts into temp[].
while (i <= mid && j <= high)
{ if (a[i] < a[j])
{ temp[k] = a[i];
k++; i++; }
else
{ temp[k] = a[j];
k++; j++; }}
// Insert all the remaining values from i to mid
into temp[].
while (i <= mid)
{ temp[k] = a[i];
k++; i++; }
// Insert all the remaining values from j to high
into temp[].
while (j <= high)
{ temp[k] = a[j];
k++;j++;} // Assign sorted data stored in temp[] to
a[].
for (i = low; i <= high; i++)
{ a[i] = temp[i-low]; } }
Merge(a, low, high, mid); [array,1,2,1]
i = low=1; k = 0; j = mid + 1=2;
// Merge the two parts into temp[].
while (i <= mid && j <= high)
{ if (a[i] < a[j])
{ temp[k] = a[i];
k++; i++; }
else
{ temp[k] = a[j];
k++; j++; }}
while (i <= mid)
{ temp[k] = a[i];
k++; i++; }
// Insert all the remaining values from j to
high into temp[].
while (j <= high)
{ temp[k] = a[j];
k++;j++;}
while (j <= high)
{ temp[k] = a[j];
k++;j++;}
// Assign sorted data stored in temp[] to a[].
for (i = low; i <= high; i++)
{ a[i] = temp[i-low]; } }
Merge(a, low, high, mid); [array,3,4,3]
i = low=3; k = 0; j = mid + 1=3;
// Merge the two parts into temp[].
while (i <= mid && j <= high)
{ if (a[i] < a[j])
{ temp[k] = a[i];
k++; i++; }
else
{ temp[k] = a[j];
k++; j++; }}
while (i <= mid)
{ temp[k] = a[i];
k++; i++; }
// Insert all the remaining values from j to
high into temp[].
while (j <= high)
{ temp[k] = a[j];
k++;j++;}
while (j <= high)
{ temp[k] = a[j];
k++;j++;}
// Assign sorted data stored in temp[] to a[].
for (i = low; i <= high; i++)
{ a[i] = temp[i-low]; } }
Merge(a, low, high, mid); [array,1,4,2]
i = low=1; k = 0; j = mid + 1=3;
// Merge the two parts into temp[].
while (i <= mid && j <= high)
{ if (a[i] < a[j])
{ temp[k] = a[i];
k++; i++; }
else
{ temp[k] = a[j];
k++; j++; }}
while (i <= mid)
{ temp[k] = a[i];
k++; i++; }
// Insert all the remaining values from j to
high into temp[].
while (j <= high)
{ temp[k] = a[j];
k++;j++;}
while (j <= high)
{ temp[k] = a[j];
k++;j++;}
// Assign sorted data stored in temp[] to a[].
for (i = low; i <= high; i++)
{ a[i] = temp[i-low]; } }
#include <iostream>
using namespace std;
// A function to merge the two half into a sorted data.
void Merge(int *a, int low, int high, int mid)
{
// We have low to mid and mid+1 to high already sorted.
int i, j, k, temp[high-low+1];
i = low;
k = 0;
j = mid + 1;
// Merge the two parts into temp[].
while (i <= mid && j <= high)
{
if (a[i] < a[j])
{
temp[k] = a[i];
k++;
i++;
}
else
{
temp[k] = a[j];
k++;
j++;
}
}
// Insert all the remaining values from i to mid into temp[].
while (i <= mid)
{
temp[k] = a[i];
k++;
i++;
}
// Insert all the remaining values from j to high into temp[].
while (j <= high)
{
temp[k] = a[j];
k++;
j++;
}
// Assign sorted data stored in temp[] to a[].
for (i = low; i <= high; i++)
{
a[i] = temp[i-low];
}
}
After sorting of remaining elements we will get
Quick Sort Algorithm & Program
• Assignment
Submission time Friday till 12:00AM

More Related Content

What's hot

3.8 quicksort 04
3.8 quicksort 043.8 quicksort 04
3.8 quicksort 04Krish_ver2
 
Fdocuments.in sugeno style-fuzzy-inference
Fdocuments.in sugeno style-fuzzy-inferenceFdocuments.in sugeno style-fuzzy-inference
Fdocuments.in sugeno style-fuzzy-inferenceSudhansuPanda15
 
Data structures
Data structuresData structures
Data structuresNur Saleha
 
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
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...Tosin Amuda
 
Chapter 14 Searching and Sorting
Chapter 14 Searching and SortingChapter 14 Searching and Sorting
Chapter 14 Searching and SortingMuhammadBakri13
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sortingguest2cb109
 
Array ADT(Abstract Data Type)|Data Structure
Array ADT(Abstract Data Type)|Data StructureArray ADT(Abstract Data Type)|Data Structure
Array ADT(Abstract Data Type)|Data StructureAkash Gaur
 
Ap Power Point Chpt9
Ap Power Point Chpt9Ap Power Point Chpt9
Ap Power Point Chpt9dplunkett
 
Collections Api - Java
Collections Api - JavaCollections Api - Java
Collections Api - JavaDrishti Bhalla
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsAakash deep Singhal
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing conceptsLJ Projects
 
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)Lovelyn Rose
 

What's hot (20)

Unit 2 algorithm
Unit   2 algorithmUnit   2 algorithm
Unit 2 algorithm
 
6.queue
6.queue6.queue
6.queue
 
0-1 knapsack problem
0-1 knapsack problem0-1 knapsack problem
0-1 knapsack problem
 
3.8 quicksort 04
3.8 quicksort 043.8 quicksort 04
3.8 quicksort 04
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
 
Fdocuments.in sugeno style-fuzzy-inference
Fdocuments.in sugeno style-fuzzy-inferenceFdocuments.in sugeno style-fuzzy-inference
Fdocuments.in sugeno style-fuzzy-inference
 
arrays
arraysarrays
arrays
 
Data structures
Data structuresData structures
Data structures
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
 
Ds
DsDs
Ds
 
Unit 4 tree
Unit 4   treeUnit 4   tree
Unit 4 tree
 
Chapter 14 Searching and Sorting
Chapter 14 Searching and SortingChapter 14 Searching and Sorting
Chapter 14 Searching and Sorting
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
 
Array ADT(Abstract Data Type)|Data Structure
Array ADT(Abstract Data Type)|Data StructureArray ADT(Abstract Data Type)|Data Structure
Array ADT(Abstract Data Type)|Data Structure
 
Ap Power Point Chpt9
Ap Power Point Chpt9Ap Power Point Chpt9
Ap Power Point Chpt9
 
Collections Api - Java
Collections Api - JavaCollections Api - Java
Collections Api - Java
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
 
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
 

Similar to Ds sorting

DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024RUHULAMINHAZARIKA
 
Chapter 10.ppt
Chapter 10.pptChapter 10.ppt
Chapter 10.pptMithuBose3
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory MappingQundeel
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptxchouguleamruta24
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortShanmuganathan C
 
Merge Sort java with a few details, please include comments if possi.pdf
Merge Sort java with a few details, please include comments if possi.pdfMerge Sort java with a few details, please include comments if possi.pdf
Merge Sort java with a few details, please include comments if possi.pdffeelinggifts
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfRahul04August
 
Problem 1 Show the comparison of runtime of linear search and binar.pdf
Problem 1 Show the comparison of runtime of linear search and binar.pdfProblem 1 Show the comparison of runtime of linear search and binar.pdf
Problem 1 Show the comparison of runtime of linear search and binar.pdfebrahimbadushata00
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).pptyasser3omr
 

Similar to Ds sorting (20)

Sorting techniques
Sorting techniques Sorting techniques
Sorting techniques
 
DAA Lab Work.docx
DAA Lab Work.docxDAA Lab Work.docx
DAA Lab Work.docx
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
 
Unit6 C
Unit6 C Unit6 C
Unit6 C
 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
 
Chapter 10.ppt
Chapter 10.pptChapter 10.ppt
Chapter 10.ppt
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory Mapping
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 
quick_sort.ppt
quick_sort.pptquick_sort.ppt
quick_sort.ppt
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
 
Merge Sort java with a few details, please include comments if possi.pdf
Merge Sort java with a few details, please include comments if possi.pdfMerge Sort java with a few details, please include comments if possi.pdf
Merge Sort java with a few details, please include comments if possi.pdf
 
Code
CodeCode
Code
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
 
Problem 1 Show the comparison of runtime of linear search and binar.pdf
Problem 1 Show the comparison of runtime of linear search and binar.pdfProblem 1 Show the comparison of runtime of linear search and binar.pdf
Problem 1 Show the comparison of runtime of linear search and binar.pdf
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 

Recently uploaded

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
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
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 

Recently uploaded (20)

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
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
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
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
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
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
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 

Ds sorting

  • 1.
  • 3. Sorting  Sorting is the process of placing elements from a collection in some kind of order. Types  Bubble Sort.  Insertion Sort.  Selection Sort.  Quick Sort.  Merge Sort.
  • 4. 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.
  • 7. Insertion Sort Insertion sort is a sorting algorithm in which the elements are transferred one at a time to the right position by comparison but with left values. This is an in-place comparison-based sorting Algorithm.
  • 8. Insertion Sort passes 11 2 1 9 6 Array[0] Array[1] Array[2] Array[3] Array[4] 11 2 1 9 6 Array[0] Array[1] Array[2] Array[3] Array[4] 2 11 1 9 6 Array[0] Array[1] Array[2] Array[3] Array[4]
  • 9. 2 11 1 9 6 Array[0] Array[1] Array[2] Array[3] Array[4] 2 1 11 9 6 Array[0] Array[1] Array[2] Array[3] Array[4] 2 1 11 9 6 Array[0] Array[1] Array[2] Array[3] Array[4]
  • 10. 1 2 11 9 6 Array[0] Array[1] Array[2] Array[3] Array[4] 1 2 9 11 6 Array[0] Array[1] Array[2] Array[3] Array[4] 1 2 11 9 6 Array[0] Array[1] Array[2] Array[3] Array[4] 1 2 9 11 6 Array[0] Array[1] Array[2] Array[3] Array[4]
  • 11. 1 2 9 6 11 Array[0] Array[1] Array[2] Array[3] Array[4] 1 2 9 6 11 Array[0] Array[1] Array[2] Array[3] Array[4] 1 2 6 9 11 Array[0] Array[1] Array[2] Array[3] Array[4]
  • 12. Insertion Sort Algorithm & Program Class assignment to write algorithm for this.
  • 13. Selection Sort  In selection sort, the smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array. This process continues moving unsorted array boundary by one element to the right.  Step 1 − Set MIN to location 0.  Step 2 − Search the minimum element in the list.  Step 3 − Swap with value at location MIN.  Step 4 − Increment MIN to point to next element.  Step 5 − Repeat until list is sorted.
  • 15. Selection Sort Algorithm & Program  Class assignment to write Code for this.
  • 16. Merge Sort  Merge sort is a sorting technique based on divide and conquer technique.  Merge sort first divides the array into equal halves and then combines them in a sorted manner.
  • 18. void MergeSort(int *a, int low, int high) { int mid; if (low < high) { mid=(low+high)/2; MergeSort(a, low, mid); MergeSort(a, mid+1, high); Merge(a, low, high, mid); } } Low = 1,high= 8,1<8,mid = 4 Stack and PC MergeSort(a, mid+1, high); [array,5,8] Merge(a, low, high, mid); [array,1,8,4] void MergeSort(int *a, int low, int high) { int mid; if (low < high) { mid=(low+high)/2; MergeSort(a, low, mid); MergeSort(a, mid+1, high); Merge(a, low, high, mid); } } Low = 1,high= 4,1<4,mid = 2 Stack and PC MergeSort(a, mid+1, high); [array,3,4] Merge(a, low, high, mid); [array,1,4,2] MergeSort(a, mid+1, high); [array,5,8] Merge(a, low, high, mid); [array,1,8,4] MergeSort(a, low, mid); Array,low= 1Mid=4 => low=1 and high =4 MergeSort(a, low, mid); Array,low= 1Mid=2 => low=1 and high =2
  • 19. void MergeSort(int *a, int low, int high) { int mid; if (low < high) { mid=(low+high)/2; MergeSort(a, low, mid); MergeSort(a, mid+1, high); Merge(a, low, high, mid); } } Low = 1,high= 2,1<2,mid = 1 Stack and PC MergeSort(a, mid+1, high); [array,2,2] Merge(a, low, high, mid); [array,1,2,1] MergeSort(a, mid+1, high); [array,3,4] Merge(a, low, high, mid); [array,1,4,2] MergeSort(a, mid+1, high); [array,5,8] Merge(a, low, high, mid); [array,1,8,4] void MergeSort(int *a, int low, int high) { int mid; if (low < high) { mid=(low+high)/2; MergeSort(a, low, mid); MergeSort(a, mid+1, high); Merge(a, low, high, mid); } } Low = 1,high= 1,1<1(false) MergeSort(a, low, mid); Array,low= 1Mid=1 => low=1 and high =1
  • 20. Stack and PC MergeSort(a, mid+1, high); [array,2,2] Merge(a, low, high, mid); [array,1,2,1] MergeSort(a, mid+1, high); [array,3,4] Merge(a, low, high, mid); [array,1,4,2] MergeSort(a, mid+1, high); [array,5,8] Merge(a, low, high, mid); [array,1,8,4] void MergeSort(int *a, int low, int high) { int mid; if (low < high) { mid=(low+high)/2; MergeSort(a, low, mid); MergeSort(a, mid+1, high); Merge(a, low, high, mid); } } Low = 2,high= 2,<2,false MergeSort(a, mid+1, high); Array,Mid=2,high=2 => low=2 and high =2 Merge(a, low, high, mid); [array,1,2,1] void MergeSort(int *a, int low, int high) { int mid; if (low < high) { mid=(low+high)/2; MergeSort(a, low, mid); MergeSort(a, mid+1, high); Merge(a, low, high, mid); } } low =3 ,high= 4,3<4,mid = 3 MergeSort(a, mid+1, high); Array,mid+1[3] ,high 4 => low=3 and high =4
  • 21. Merge(a, low, high, mid); [array,1,2,1] void MergeSort(int *a, int low, int high) { int mid; if (low < high) { mid=(low+high)/2; MergeSort(a, low, mid); MergeSort(a, mid+1, high); Merge(a, low, high, mid); } } low =3 ,high= 4,3<4,mid = 3 MergeSort(a, mid+1, high); Array,mid+1[3] ,high 4 => low=3 and high =4 Stack and PC MergeSort(a, mid+1, high); [array,4,4] Merge(a, low, high, mid); [array,3,4,3] Merge(a, low, high, mid); [array,1,4,2] MergeSort(a, mid+1, high); [array,5,8] Merge(a, low, high, mid); [array,1,8,4] void MergeSort(int *a, int low, int high) { int mid; if (low < high) { mid=(low+high)/2; MergeSort(a, low, mid); MergeSort(a, mid+1, high); Merge(a, low, high, mid); } } MergeSort(a, low, mid); Array,low=3,Mid=3 => low=3 and high =3 low =3 ,high= 3,3<3,false
  • 22. Stack and PC MergeSort(a, mid+1, high); [array,4,4] Merge(a, low, high, mid); [array,3,4,3] Merge(a, low, high, mid); [array,1,4,2] MergeSort(a, mid+1, high); [array,5,8] Merge(a, low, high, mid); [array,1,8,4] Merge(a, low, high, mid); [array,1,2,1] void MergeSort(int *a, int low, int high) { int mid; if (low < high) { mid=(low+high)/2; MergeSort(a, low, mid); MergeSort(a, mid+1, high); Merge(a, low, high, mid); } } Low = 4,high= 2,4<4,false MergeSort(a, mid+1, high); Array,Mid=3+1[4] high=4 => low=4 and high =4 Merge(a, low, high, mid); [array,3,4,3] Merge(a, low, high, mid); [array,1,4,2]
  • 23. void Merge(int *a, int low, int high, int mid) {// We have low to mid and mid+1 to high already sorted. int i, j, k, temp[high-low+1]; i = low; k = 0; j = mid + 1; // Merge the two parts into temp[]. while (i <= mid && j <= high) { if (a[i] < a[j]) { temp[k] = a[i]; k++; i++; } else { temp[k] = a[j]; k++; j++; }} // Insert all the remaining values from i to mid into temp[]. while (i <= mid) { temp[k] = a[i]; k++; i++; } // Insert all the remaining values from j to high into temp[]. while (j <= high) { temp[k] = a[j]; k++;j++;} // Assign sorted data stored in temp[] to a[]. for (i = low; i <= high; i++) { a[i] = temp[i-low]; } } Merge(a, low, high, mid); [array,1,2,1] i = low=1; k = 0; j = mid + 1=2; // Merge the two parts into temp[]. while (i <= mid && j <= high) { if (a[i] < a[j]) { temp[k] = a[i]; k++; i++; } else { temp[k] = a[j]; k++; j++; }} while (i <= mid) { temp[k] = a[i]; k++; i++; } // Insert all the remaining values from j to high into temp[]. while (j <= high) { temp[k] = a[j]; k++;j++;} while (j <= high) { temp[k] = a[j]; k++;j++;} // Assign sorted data stored in temp[] to a[]. for (i = low; i <= high; i++) { a[i] = temp[i-low]; } } Merge(a, low, high, mid); [array,3,4,3] i = low=3; k = 0; j = mid + 1=3; // Merge the two parts into temp[]. while (i <= mid && j <= high) { if (a[i] < a[j]) { temp[k] = a[i]; k++; i++; } else { temp[k] = a[j]; k++; j++; }} while (i <= mid) { temp[k] = a[i]; k++; i++; } // Insert all the remaining values from j to high into temp[]. while (j <= high) { temp[k] = a[j]; k++;j++;} while (j <= high) { temp[k] = a[j]; k++;j++;} // Assign sorted data stored in temp[] to a[]. for (i = low; i <= high; i++) { a[i] = temp[i-low]; } } Merge(a, low, high, mid); [array,1,4,2] i = low=1; k = 0; j = mid + 1=3; // Merge the two parts into temp[]. while (i <= mid && j <= high) { if (a[i] < a[j]) { temp[k] = a[i]; k++; i++; } else { temp[k] = a[j]; k++; j++; }} while (i <= mid) { temp[k] = a[i]; k++; i++; } // Insert all the remaining values from j to high into temp[]. while (j <= high) { temp[k] = a[j]; k++;j++;} while (j <= high) { temp[k] = a[j]; k++;j++;} // Assign sorted data stored in temp[] to a[]. for (i = low; i <= high; i++) { a[i] = temp[i-low]; } }
  • 24. #include <iostream> using namespace std; // A function to merge the two half into a sorted data. void Merge(int *a, int low, int high, int mid) { // We have low to mid and mid+1 to high already sorted. int i, j, k, temp[high-low+1]; i = low; k = 0; j = mid + 1; // Merge the two parts into temp[]. while (i <= mid && j <= high) { if (a[i] < a[j]) { temp[k] = a[i]; k++; i++; } else { temp[k] = a[j]; k++; j++; } } // Insert all the remaining values from i to mid into temp[]. while (i <= mid) { temp[k] = a[i]; k++; i++; } // Insert all the remaining values from j to high into temp[]. while (j <= high) { temp[k] = a[j]; k++; j++; } // Assign sorted data stored in temp[] to a[]. for (i = low; i <= high; i++) { a[i] = temp[i-low]; } }
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47. After sorting of remaining elements we will get
  • 48. Quick Sort Algorithm & Program • Assignment Submission time Friday till 12:00AM