SlideShare a Scribd company logo
1 of 26
Anshika Das
14400121029
CSE
PCC-CS 301
Data Structure &
Algorithm
2022-23(odd sem)
SORTING
Introduction
3
• Sorting is among the most basic problems in algorithm design.
• We are given a sequence of items, each associated with a given key
value. And the problem is to rearrange the items so that they are in an
increasing(or decreasing) order by key.
• The methods of sorting can be divided into two categories:
• Internal Sorting
• External Sorting
4
• Internal Sorting
If all the data that is to be sorted can be adjusted at a time in main memory,
then internal sorting methods are used
• External Sorting
When the data to be sorted can’t be accommodated in the memory at the
same time and some has to be kept in auxiliary memory, then external sorting
methods are used.
NOTE: We will only consider internal sorting
Stable and Not Stable Sorting
5
• If a sorting algorithm, after sorting the contents, does not change the
sequence of similar content in which they appear, it is called stable
sorting.
• If a sorting algorithm, after sorting the contents, changes the
sequence of similar content in which they appear, it is called unstable
sorting.
Efficiency of Sorting Algorithm
6
• The complexity of a sorting algorithm measures the running time of a
function in which n number of items are to be sorted.
• The choice of sorting method depends on efficiency considerations for
different problems.
• Tree most important of these considerations are:
• The length of time spent by programmer in coding a particular sorting program
• Amount of machine time necessary for running the program
• The amount of memory necessary for running the program
Things to remember
7
• An ideal sort is an in-place sort where the algorithm uses no additional
array storage, and hence it is possible to sort very large lists without
the need to allocate additional working storage.
• A sorting algorithm is said to be stable if two objects with equal keys
appear in the same order in sorted output as they appear in the input
unsorted array.
• Some sorting algorithms are stable by nature like Insertion sort, Merge
Sort, Bubble Sort, etc. And some sorting algorithms are not, like Heap
Sort, Quick Sort, etc.
BUBBLE SORT
8
• In bubble sort, each element is compared with its adjacent element.
• We begin with the 0th element and compare it with the 1st element.
• If it is found to be greater than the 1st element, then they are interchanged.
• In this way all the elements are compared (excluding last) with their next
element and are interchanged if required
• On completing the first iteration, largest element gets placed at the last
position. Similarly in second iteration second largest element gets placed at
the second last position and so on.
14
Algorithm
15
TIME COMPLEXITY
17
• Best Case
• sorting a sorted array by bubble sort algorithm
• In best case outer loop will terminate after one iteration, i.e it involves performing
one pass which requires n-1 comparison
f (n) = O(n2)
• Worst Case
• Suppose an array [5,4,3,2,1], we need to move first element to end of an array
• n-1 times the swapping procedure is to be called
f (n) = O(n2)
• Average Case
• Difficult to analyse than the other cases
• Random inputs, so in general
f (n) = O(n2)
• Space Complexity
• O(n)
SELECTION SORT
18
• Find the least( or greatest) value in the array, swap it into the leftmost(or
rightmost) component, and then forget the leftmost component, Do this
repeatedly.
• Let a[n] be a linear array of n elements. The selection sort works as follows:
• Pass 1: Find the location loc of the smallest element in the list of n elements a[0],
a[1], a[2], a[3], .........,a[n-1] and then interchange a[loc] and a[0].
• Pass 2: Find the location loc of the smallest element int the sub-list of n-1
elements a[1], a[2], a[3], .........,a[n-1] and then interchange a[loc] and a[1] such
that a[0], a[1] are sorted.
• Then we will get the sorted list
a[0]<=a[2]<=a[3]…...<=a[n-1]
19
20
Time Complexity
21
• Inner loop executes (n-1) times when i=0, (n-2) times when i=1 and so
on:
• Time complexity = (n-1) + (n-2) + (n-3) + …....... +2+1
= O(n2)
Space Complexity
• Since no extra space beside n variables is needed for sorting so
• O(n)
Insertion Sort
22
• Like sorting a hand of playing cards start with an empty hand and the
cards facing down the table.
• Pick one card at a time from the table, and insert it into the correct
position in the left hand.
• Compare it with each of the cards already in the hand, from right to
left
• The cards held in the left hand are sorted.
23
24
Insertion Sort
25
• Suppose an array a[n] with n elements. The insertion sort works as follows:
Pass 1: a[0] by itself is trivially sorted.
Pass 2: a[1] is inserted either before or after a[0] so that a[0], a[1] is sorted.
Pass 3: a[2] is inserted into its proper place in a[0],a[1] that is before a[0],
between a[0] and a[1], or after a[1] so that a[0],a[1],a[2] is sorted.
pass N: a[n-1] is inserted into its proper place in a[0],a[1],a[2],........,a[n-2] so
that a[0],a[1],a[2],............,a[n-1] is sorted with n elements.
26
7 2 4 5 1 3
2 7 4 5 1 3
2 4 7 5 1 3
2 4 5 7 1 3
1 2 4 5 7 3
1 2 3 4 5 7
Algorithm
27
InsertionSort(){
for (i=1;i<n;i++){
value=C[ i ];
hole= i ;
while(hole>0 && C[hole-1]>value){
C[hole]=C[hole-1];
hole=hole-1;
}
C[hole]=value;
}
}
i value hole
1 2 1
1 2 0
7 2 4 1 5 3
7 7 4 1 5 3
1st Pass
2 7 4 1 5 3
Time Complexity
28
• Best Case:
• If the array is all but sorted then
• Inner Loop wont execute so only some constant time the statements will run
• So Time complexity= O(n)
• Worst Case:
• Array element in reverse sorted order
• Time complexity=O(n2)
• Space Complexity
• Since no extra space beside n variables is needed for sorting so
• Space Complexity = O(n)
Divide and conquer algorithms
29
• The sorting algorithms we’ve seen so far have worst-case running
times of O(n2)
• When the size of the input array is large, these algorithms can take a
long time to run.
• Now we will discuss two sorting algorithms whose running times are
better
• Merge Sort
• Quick Sort
30
Divide-and-conquer
31
• Divide-and-conquer, breaks a problem into sub problems that are
similar to the original problem, recursively solves the sub problems,
and finally combines the solutions to the sub problems to solve the
original problem.
• Think of a divide-and-conquer algorithm as having three parts:
• Divide the problem into a number of subproblems that are smaller instances of
the same problem.
• Conquer the subproblems by solving them recursively. If they are small
enough, solve the subproblems as base cases.
• Combine the solutions to the subproblems into the solution for the original
problem.
Divide-and-conquer
32

More Related Content

Similar to Anshika Das' Data Structure & Algorithm Notes on Sorting Algorithms

Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting AlgorithmsPranay Neema
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing conceptsLJ Projects
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing conceptsLJ Projects
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptxDeepakM509554
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptxchouguleamruta24
 
Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureTushar Gonawala
 
Sorting Algorithms.
Sorting Algorithms.Sorting Algorithms.
Sorting Algorithms.Saket Kumar
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfAkashSingh625550
 
Basic Sorting algorithms csharp
Basic Sorting algorithms csharpBasic Sorting algorithms csharp
Basic Sorting algorithms csharpMicheal Ogundero
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdfharamaya university
 
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
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14sumitbardhan
 

Similar to Anshika Das' Data Structure & Algorithm Notes on Sorting Algorithms (20)

Complexity in array
Complexity in arrayComplexity in array
Complexity in array
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Lec 03 - Sorting.pptx
Lec 03 - Sorting.pptxLec 03 - Sorting.pptx
Lec 03 - Sorting.pptx
 
Sorting
SortingSorting
Sorting
 
CSPC/ PPS Sorting methods
CSPC/ PPS Sorting methodsCSPC/ PPS Sorting methods
CSPC/ PPS Sorting methods
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptx
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 
Data Structures 7
Data Structures 7Data Structures 7
Data Structures 7
 
Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data Structure
 
Sorting Algorithms.
Sorting Algorithms.Sorting Algorithms.
Sorting Algorithms.
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
 
my docoment
my docomentmy docoment
my docoment
 
Basic Sorting algorithms csharp
Basic Sorting algorithms csharpBasic Sorting algorithms csharp
Basic Sorting algorithms csharp
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
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...
 
queue.pptx
queue.pptxqueue.pptx
queue.pptx
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
 

More from AnSHiKa187943

databasesecurit-phpapp01.pdf
databasesecurit-phpapp01.pdfdatabasesecurit-phpapp01.pdf
databasesecurit-phpapp01.pdfAnSHiKa187943
 
wepik-enhancing-visual-data-exploring-arithmetic-and-logic-operations-in-imag...
wepik-enhancing-visual-data-exploring-arithmetic-and-logic-operations-in-imag...wepik-enhancing-visual-data-exploring-arithmetic-and-logic-operations-in-imag...
wepik-enhancing-visual-data-exploring-arithmetic-and-logic-operations-in-imag...AnSHiKa187943
 
11100121024_Asif Ikbal_CA1_OS...pdf
11100121024_Asif Ikbal_CA1_OS...pdf11100121024_Asif Ikbal_CA1_OS...pdf
11100121024_Asif Ikbal_CA1_OS...pdfAnSHiKa187943
 
CA-1_14400121029_HSMC_301.pdf
CA-1_14400121029_HSMC_301.pdfCA-1_14400121029_HSMC_301.pdf
CA-1_14400121029_HSMC_301.pdfAnSHiKa187943
 
14400121029_Anshika Das_Software Engineering.pdf
14400121029_Anshika Das_Software Engineering.pdf14400121029_Anshika Das_Software Engineering.pdf
14400121029_Anshika Das_Software Engineering.pdfAnSHiKa187943
 
time-value-of-money.pptx
time-value-of-money.pptxtime-value-of-money.pptx
time-value-of-money.pptxAnSHiKa187943
 
divisionoflabour-170216153446-2.pptx
divisionoflabour-170216153446-2.pptxdivisionoflabour-170216153446-2.pptx
divisionoflabour-170216153446-2.pptxAnSHiKa187943
 
pumping-lemma-181011153118.pptx
pumping-lemma-181011153118.pptxpumping-lemma-181011153118.pptx
pumping-lemma-181011153118.pptxAnSHiKa187943
 
_86c448dfa47cdab170075f16cd25c650_PeerReviewforUpload.pptx
_86c448dfa47cdab170075f16cd25c650_PeerReviewforUpload.pptx_86c448dfa47cdab170075f16cd25c650_PeerReviewforUpload.pptx
_86c448dfa47cdab170075f16cd25c650_PeerReviewforUpload.pptxAnSHiKa187943
 
26-170918023441 (1).pptx
26-170918023441 (1).pptx26-170918023441 (1).pptx
26-170918023441 (1).pptxAnSHiKa187943
 
9pL7F2E8XI0alXYT280.pptx
9pL7F2E8XI0alXYT280.pptx9pL7F2E8XI0alXYT280.pptx
9pL7F2E8XI0alXYT280.pptxAnSHiKa187943
 

More from AnSHiKa187943 (17)

databasesecurit-phpapp01.pdf
databasesecurit-phpapp01.pdfdatabasesecurit-phpapp01.pdf
databasesecurit-phpapp01.pdf
 
wepik-enhancing-visual-data-exploring-arithmetic-and-logic-operations-in-imag...
wepik-enhancing-visual-data-exploring-arithmetic-and-logic-operations-in-imag...wepik-enhancing-visual-data-exploring-arithmetic-and-logic-operations-in-imag...
wepik-enhancing-visual-data-exploring-arithmetic-and-logic-operations-in-imag...
 
dd presentation.pdf
dd presentation.pdfdd presentation.pdf
dd presentation.pdf
 
11100121024_Asif Ikbal_CA1_OS...pdf
11100121024_Asif Ikbal_CA1_OS...pdf11100121024_Asif Ikbal_CA1_OS...pdf
11100121024_Asif Ikbal_CA1_OS...pdf
 
CA-1_14400121029_HSMC_301.pdf
CA-1_14400121029_HSMC_301.pdfCA-1_14400121029_HSMC_301.pdf
CA-1_14400121029_HSMC_301.pdf
 
14400121029_Anshika Das_Software Engineering.pdf
14400121029_Anshika Das_Software Engineering.pdf14400121029_Anshika Das_Software Engineering.pdf
14400121029_Anshika Das_Software Engineering.pdf
 
time-value-of-money.pptx
time-value-of-money.pptxtime-value-of-money.pptx
time-value-of-money.pptx
 
divisionoflabour-170216153446-2.pptx
divisionoflabour-170216153446-2.pptxdivisionoflabour-170216153446-2.pptx
divisionoflabour-170216153446-2.pptx
 
pumping-lemma-181011153118.pptx
pumping-lemma-181011153118.pptxpumping-lemma-181011153118.pptx
pumping-lemma-181011153118.pptx
 
LECT9.ppt
LECT9.pptLECT9.ppt
LECT9.ppt
 
_86c448dfa47cdab170075f16cd25c650_PeerReviewforUpload.pptx
_86c448dfa47cdab170075f16cd25c650_PeerReviewforUpload.pptx_86c448dfa47cdab170075f16cd25c650_PeerReviewforUpload.pptx
_86c448dfa47cdab170075f16cd25c650_PeerReviewforUpload.pptx
 
ChodonKumar.pptx
ChodonKumar.pptxChodonKumar.pptx
ChodonKumar.pptx
 
SubhamDas.pptx
SubhamDas.pptxSubhamDas.pptx
SubhamDas.pptx
 
26-170918023441 (1).pptx
26-170918023441 (1).pptx26-170918023441 (1).pptx
26-170918023441 (1).pptx
 
BhamD.pptx
BhamD.pptxBhamD.pptx
BhamD.pptx
 
Anshika 1111.pptx
Anshika 1111.pptxAnshika 1111.pptx
Anshika 1111.pptx
 
9pL7F2E8XI0alXYT280.pptx
9pL7F2E8XI0alXYT280.pptx9pL7F2E8XI0alXYT280.pptx
9pL7F2E8XI0alXYT280.pptx
 

Recently uploaded

power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 

Recently uploaded (20)

power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 

Anshika Das' Data Structure & Algorithm Notes on Sorting Algorithms

  • 1. Anshika Das 14400121029 CSE PCC-CS 301 Data Structure & Algorithm 2022-23(odd sem)
  • 3. Introduction 3 • Sorting is among the most basic problems in algorithm design. • We are given a sequence of items, each associated with a given key value. And the problem is to rearrange the items so that they are in an increasing(or decreasing) order by key. • The methods of sorting can be divided into two categories: • Internal Sorting • External Sorting
  • 4. 4 • Internal Sorting If all the data that is to be sorted can be adjusted at a time in main memory, then internal sorting methods are used • External Sorting When the data to be sorted can’t be accommodated in the memory at the same time and some has to be kept in auxiliary memory, then external sorting methods are used. NOTE: We will only consider internal sorting
  • 5. Stable and Not Stable Sorting 5 • If a sorting algorithm, after sorting the contents, does not change the sequence of similar content in which they appear, it is called stable sorting. • If a sorting algorithm, after sorting the contents, changes the sequence of similar content in which they appear, it is called unstable sorting.
  • 6. Efficiency of Sorting Algorithm 6 • The complexity of a sorting algorithm measures the running time of a function in which n number of items are to be sorted. • The choice of sorting method depends on efficiency considerations for different problems. • Tree most important of these considerations are: • The length of time spent by programmer in coding a particular sorting program • Amount of machine time necessary for running the program • The amount of memory necessary for running the program
  • 7. Things to remember 7 • An ideal sort is an in-place sort where the algorithm uses no additional array storage, and hence it is possible to sort very large lists without the need to allocate additional working storage. • A sorting algorithm is said to be stable if two objects with equal keys appear in the same order in sorted output as they appear in the input unsorted array. • Some sorting algorithms are stable by nature like Insertion sort, Merge Sort, Bubble Sort, etc. And some sorting algorithms are not, like Heap Sort, Quick Sort, etc.
  • 8. BUBBLE SORT 8 • In bubble sort, each element is compared with its adjacent element. • We begin with the 0th element and compare it with the 1st element. • If it is found to be greater than the 1st element, then they are interchanged. • In this way all the elements are compared (excluding last) with their next element and are interchanged if required • On completing the first iteration, largest element gets placed at the last position. Similarly in second iteration second largest element gets placed at the second last position and so on.
  • 9. 14
  • 11. TIME COMPLEXITY 17 • Best Case • sorting a sorted array by bubble sort algorithm • In best case outer loop will terminate after one iteration, i.e it involves performing one pass which requires n-1 comparison f (n) = O(n2) • Worst Case • Suppose an array [5,4,3,2,1], we need to move first element to end of an array • n-1 times the swapping procedure is to be called f (n) = O(n2) • Average Case • Difficult to analyse than the other cases • Random inputs, so in general f (n) = O(n2) • Space Complexity • O(n)
  • 12. SELECTION SORT 18 • Find the least( or greatest) value in the array, swap it into the leftmost(or rightmost) component, and then forget the leftmost component, Do this repeatedly. • Let a[n] be a linear array of n elements. The selection sort works as follows: • Pass 1: Find the location loc of the smallest element in the list of n elements a[0], a[1], a[2], a[3], .........,a[n-1] and then interchange a[loc] and a[0]. • Pass 2: Find the location loc of the smallest element int the sub-list of n-1 elements a[1], a[2], a[3], .........,a[n-1] and then interchange a[loc] and a[1] such that a[0], a[1] are sorted. • Then we will get the sorted list a[0]<=a[2]<=a[3]…...<=a[n-1]
  • 13. 19
  • 14. 20
  • 15. Time Complexity 21 • Inner loop executes (n-1) times when i=0, (n-2) times when i=1 and so on: • Time complexity = (n-1) + (n-2) + (n-3) + …....... +2+1 = O(n2) Space Complexity • Since no extra space beside n variables is needed for sorting so • O(n)
  • 16. Insertion Sort 22 • Like sorting a hand of playing cards start with an empty hand and the cards facing down the table. • Pick one card at a time from the table, and insert it into the correct position in the left hand. • Compare it with each of the cards already in the hand, from right to left • The cards held in the left hand are sorted.
  • 17. 23
  • 18. 24
  • 19. Insertion Sort 25 • Suppose an array a[n] with n elements. The insertion sort works as follows: Pass 1: a[0] by itself is trivially sorted. Pass 2: a[1] is inserted either before or after a[0] so that a[0], a[1] is sorted. Pass 3: a[2] is inserted into its proper place in a[0],a[1] that is before a[0], between a[0] and a[1], or after a[1] so that a[0],a[1],a[2] is sorted. pass N: a[n-1] is inserted into its proper place in a[0],a[1],a[2],........,a[n-2] so that a[0],a[1],a[2],............,a[n-1] is sorted with n elements.
  • 20. 26 7 2 4 5 1 3 2 7 4 5 1 3 2 4 7 5 1 3 2 4 5 7 1 3 1 2 4 5 7 3 1 2 3 4 5 7
  • 21. Algorithm 27 InsertionSort(){ for (i=1;i<n;i++){ value=C[ i ]; hole= i ; while(hole>0 && C[hole-1]>value){ C[hole]=C[hole-1]; hole=hole-1; } C[hole]=value; } } i value hole 1 2 1 1 2 0 7 2 4 1 5 3 7 7 4 1 5 3 1st Pass 2 7 4 1 5 3
  • 22. Time Complexity 28 • Best Case: • If the array is all but sorted then • Inner Loop wont execute so only some constant time the statements will run • So Time complexity= O(n) • Worst Case: • Array element in reverse sorted order • Time complexity=O(n2) • Space Complexity • Since no extra space beside n variables is needed for sorting so • Space Complexity = O(n)
  • 23. Divide and conquer algorithms 29 • The sorting algorithms we’ve seen so far have worst-case running times of O(n2) • When the size of the input array is large, these algorithms can take a long time to run. • Now we will discuss two sorting algorithms whose running times are better • Merge Sort • Quick Sort
  • 24. 30
  • 25. Divide-and-conquer 31 • Divide-and-conquer, breaks a problem into sub problems that are similar to the original problem, recursively solves the sub problems, and finally combines the solutions to the sub problems to solve the original problem. • Think of a divide-and-conquer algorithm as having three parts: • Divide the problem into a number of subproblems that are smaller instances of the same problem. • Conquer the subproblems by solving them recursively. If they are small enough, solve the subproblems as base cases. • Combine the solutions to the subproblems into the solution for the original problem.