SlideShare a Scribd company logo
1 of 12
Sorting
 Sorting is an algorithm that arranges the elements of a list in a
certain order [either ascending or descending].
 The efficiency of data handling can be substantially
[significantly] increased if the data are sorted according to some
criteria or order.
 Sorting can significantly reduce the complexity of a problem,
and is often used for database algorithms and searches.
 Implementation example: Telephone directory, Product
catalogues in ecommerce etc.
Categories of Sorting
 The techniques of sorting can be divided into two categories.
These are:
◦ Internal Sorting
◦ External Sorting
 Internal Sorting: If all the data that is to be sorted can be
adjusted at a time in the main memory, the internal sorting
method is being performed.
 External Sorting: When the data that is to be sorted cannot be
accommodated in the memory at the same time and some has to
be kept in auxiliary memory such as hard disk, floppy disk,
magnetic tapes etc, then external sorting methods are performed.
Complexity of sorting algorithm
 The complexity of sorting algorithm calculates the running time
of a function in which 'n' number of items are to be sorted. The
choice for which sorting method is suitable for a problem
depends on several dependency configurations for different
problems. The most noteworthy of these considerations are:
 The length of time spent by the programmer in programming a
specific sorting program
 Amount of machine time necessary for running the program
 The amount of memory necessary for running the program
Efficiency of Sorting Algorithm
 To get the amount of time required to sort an array of 'n'
elements by a particular method, the normal approach is to
analyze the method to find the number of comparisons (or
exchanges) required by it.
 Most of the sorting techniques are data sensitive, and so the
metrics for them depends on the order in which they appear in an
input array.
 Various sorting techniques are analyzed in various cases and
named these cases as follows:
◦ Best case
◦ Worst case
◦ Average case
 Hence, the result of these cases is often a formula giving the
average time required for a particular sort of size 'n.' Most of the
sort methods have time requirements that range from O(nlog n)
to O(n2).
 O(1): This denotes the constant time. 0(1) usually
means that an algorithm will have constant time
regardless of the input size.
 O(log n): This denotes logarithmic time. O(log n)
means to decrease with each instance for the
operations. Binary search trees are the best examples
of logarithmic time.
 O(n): This denotes linear time. O(n) means that the
performance is directly proportional to the input size.
In simple terms, the number of inputs and the time
taken to execute those inputs will be proportional or
the same. Linear search in arrays is the best example
of linear time complexity.
 O(n2): This denotes quadratic time. O(n2) means that
the performance is directly proportional to the square
of the input taken. In simple, the time taken for
execution will take square times the input
size. Nested loops are perfect examples of quadratic
time complexity.
Insertion Sort
 Insertion Sort is a sorting algorithm where the array is sorted by
taking one element at a time.
 The principle behind insertion sort is to take one element, iterate
through the sorted array & find its correct position in the sorted
array.
Complexity of the Insertion Sort Algorithm
 To sort an unsorted list with 'n' number of elements, we need to
make (1+2+3+......+n-1) = (n (n-1))/2 number of comparisions
in the worst case. If the list is already sorted then it
requires 'n' number of comparisions.
◦ Worst Case : O(n2)
◦ Best Case : Ω(n)
◦ Average Case : Θ(n2)
Algorithm for Insertion Sort
Step 1 − If the element is the first one, it is already sorted.
Step 2 – Move to next element
Step 3 − Compare the current element with all elements in the
sorted array
Step 4 – If the element in the sorted array is smaller than the
current element, iterate to the next element. Otherwise, shift all
the greater element in the array by one position towards the right
Step 5 − Insert the value at the correct position
Step 6 − Repeat until the complete list is sorted
How to sort data using insertion sort
Let’s understand how insertion sort is working in the above image.
122, 17, 93, 3, 36
for i = 1(2nd element) to
36 (last element)
i = 1. Since 17 is smaller than 122,
move 122 and insert 17 before 122
17, 122, 93, 3, 36
i = 2. Since 93 is smaller than 122,
move 122 and insert 93 before 122
17, 93,122, 3, 36
i = 3. 3 will move to the beginning
and all other elements from 17 to 122
will move one position ahead of their
current position.
3, 17, 93, 122, 36
i = 4. 36 will move to position after 17, a
nd elements from 93 to 122 will move
one position ahead of their current position.
3, 17, 36, 93 ,122
Now that we have understood how Insertion Sort works,
let us quickly look at a C code to implement insertion sort
Insertion sort function
void insertionSort(int array[], int n)
{
int i, element, j;
for (i = 1; i < n; i++)
{
element = array[i]; j = i - 1; //17///0
/* Move elements of arr[0..i-1], that are greater than key by one
position */
while (j >= 0 && array[j] > element) {
array[j + 1] = array[j];
j = j - 1;
}
array[j + 1] = element;
}
}
 Selection sort is a sorting algorithm that selects the smallest
element from an unsorted list in each iteration and places that
element at the beginning of the unsorted list.
Algorithm
 Step 1 - Select the first element of the list (i.e., Element at first
position in the list).
 Step 2: Compare the selected element with all the other
elements in the list.
 Step 3: In every comparision, if any element is found smaller
than the selected element (for Ascending order), then both are
swapped.
 Step 4: Repeat the same procedure with element in the next
position in the list till the entire list is sorted.
Complexity of the Selection Sort Algorithm
 To sort an unsorted list with 'n' number of elements, we need to make ((n-1)+(n-2)+(n-3)+......+1) =
(n (n-1))/2 number of comparisons in the worst case.
 If the list is already sorted then it requires 'n' number of comparisons.
Worst Case : O(n2)
Best Case : Ω(n2)
Average Case : Θ(n2)
Selection Sort Logic
//Selection sort logic
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
if(list[i] > list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
}

More Related Content

Similar to my docoment

DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxprakashvs7
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingThenmozhiK5
 
Different types of Shoring Algorithms with Animation
Different types of Shoring Algorithms with AnimationDifferent types of Shoring Algorithms with Animation
Different types of Shoring Algorithms with AnimationZakaria Hossain
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048vital vital
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048vital vital
 
Lecture 2 data structures & algorithms - sorting techniques
Lecture 2  data structures & algorithms - sorting techniquesLecture 2  data structures & algorithms - sorting techniques
Lecture 2 data structures & algorithms - sorting techniquesDharmendra Prasad
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)jehan1987
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdfharamaya university
 
Data structure presentation sorting
Data structure presentation sortingData structure presentation sorting
Data structure presentation sortingPranjali Rawat
 
Algorithm By AMT.pptx
Algorithm By AMT.pptxAlgorithm By AMT.pptx
Algorithm By AMT.pptxAungMyintTun3
 
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
 

Similar to my docoment (20)

DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
 
Different types of Shoring Algorithms with Animation
Different types of Shoring Algorithms with AnimationDifferent types of Shoring Algorithms with Animation
Different types of Shoring Algorithms with Animation
 
INDEX SORT
INDEX SORTINDEX SORT
INDEX SORT
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting
SortingSorting
Sorting
 
L1803016468
L1803016468L1803016468
L1803016468
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048
 
Lecture 2 data structures & algorithms - sorting techniques
Lecture 2  data structures & algorithms - sorting techniquesLecture 2  data structures & algorithms - sorting techniques
Lecture 2 data structures & algorithms - sorting techniques
 
Sorting
SortingSorting
Sorting
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
 
Heap, quick and merge sort
Heap, quick and merge sortHeap, quick and merge sort
Heap, quick and merge sort
 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Data structure presentation sorting
Data structure presentation sortingData structure presentation sorting
Data structure presentation sorting
 
Algorithm By AMT.pptx
Algorithm By AMT.pptxAlgorithm By AMT.pptx
Algorithm By AMT.pptx
 
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...
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
 

Recently uploaded

dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxStephen266013
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024thyngster
 
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...Boston Institute of Analytics
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDRafezzaman
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfSocial Samosa
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAbdelrhman abooda
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一F La
 
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理e4aez8ss
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
 
RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.natarajan8993
 
Top 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In QueensTop 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In Queensdataanalyticsqueen03
 

Recently uploaded (20)

dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docx
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
 
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
 
E-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptxE-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptx
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
Call Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort ServiceCall Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort Service
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
 
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
 
RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.
 
Top 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In QueensTop 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In Queens
 

my docoment

  • 2.  Sorting is an algorithm that arranges the elements of a list in a certain order [either ascending or descending].  The efficiency of data handling can be substantially [significantly] increased if the data are sorted according to some criteria or order.  Sorting can significantly reduce the complexity of a problem, and is often used for database algorithms and searches.  Implementation example: Telephone directory, Product catalogues in ecommerce etc.
  • 3. Categories of Sorting  The techniques of sorting can be divided into two categories. These are: ◦ Internal Sorting ◦ External Sorting  Internal Sorting: If all the data that is to be sorted can be adjusted at a time in the main memory, the internal sorting method is being performed.  External Sorting: When the data that is to be sorted cannot be accommodated in the memory at the same time and some has to be kept in auxiliary memory such as hard disk, floppy disk, magnetic tapes etc, then external sorting methods are performed.
  • 4. Complexity of sorting algorithm  The complexity of sorting algorithm calculates the running time of a function in which 'n' number of items are to be sorted. The choice for which sorting method is suitable for a problem depends on several dependency configurations for different problems. The most noteworthy of these considerations are:  The length of time spent by the programmer in programming a specific sorting program  Amount of machine time necessary for running the program  The amount of memory necessary for running the program
  • 5. Efficiency of Sorting Algorithm  To get the amount of time required to sort an array of 'n' elements by a particular method, the normal approach is to analyze the method to find the number of comparisons (or exchanges) required by it.  Most of the sorting techniques are data sensitive, and so the metrics for them depends on the order in which they appear in an input array.  Various sorting techniques are analyzed in various cases and named these cases as follows: ◦ Best case ◦ Worst case ◦ Average case  Hence, the result of these cases is often a formula giving the average time required for a particular sort of size 'n.' Most of the sort methods have time requirements that range from O(nlog n) to O(n2).
  • 6.  O(1): This denotes the constant time. 0(1) usually means that an algorithm will have constant time regardless of the input size.  O(log n): This denotes logarithmic time. O(log n) means to decrease with each instance for the operations. Binary search trees are the best examples of logarithmic time.  O(n): This denotes linear time. O(n) means that the performance is directly proportional to the input size. In simple terms, the number of inputs and the time taken to execute those inputs will be proportional or the same. Linear search in arrays is the best example of linear time complexity.  O(n2): This denotes quadratic time. O(n2) means that the performance is directly proportional to the square of the input taken. In simple, the time taken for execution will take square times the input size. Nested loops are perfect examples of quadratic time complexity.
  • 7. Insertion Sort  Insertion Sort is a sorting algorithm where the array is sorted by taking one element at a time.  The principle behind insertion sort is to take one element, iterate through the sorted array & find its correct position in the sorted array. Complexity of the Insertion Sort Algorithm  To sort an unsorted list with 'n' number of elements, we need to make (1+2+3+......+n-1) = (n (n-1))/2 number of comparisions in the worst case. If the list is already sorted then it requires 'n' number of comparisions. ◦ Worst Case : O(n2) ◦ Best Case : Ω(n) ◦ Average Case : Θ(n2)
  • 8. Algorithm for Insertion Sort Step 1 − If the element is the first one, it is already sorted. Step 2 – Move to next element Step 3 − Compare the current element with all elements in the sorted array Step 4 – If the element in the sorted array is smaller than the current element, iterate to the next element. Otherwise, shift all the greater element in the array by one position towards the right Step 5 − Insert the value at the correct position Step 6 − Repeat until the complete list is sorted
  • 9. How to sort data using insertion sort Let’s understand how insertion sort is working in the above image. 122, 17, 93, 3, 36 for i = 1(2nd element) to 36 (last element) i = 1. Since 17 is smaller than 122, move 122 and insert 17 before 122 17, 122, 93, 3, 36 i = 2. Since 93 is smaller than 122, move 122 and insert 93 before 122 17, 93,122, 3, 36 i = 3. 3 will move to the beginning and all other elements from 17 to 122 will move one position ahead of their current position. 3, 17, 93, 122, 36 i = 4. 36 will move to position after 17, a nd elements from 93 to 122 will move one position ahead of their current position. 3, 17, 36, 93 ,122 Now that we have understood how Insertion Sort works, let us quickly look at a C code to implement insertion sort
  • 10. Insertion sort function void insertionSort(int array[], int n) { int i, element, j; for (i = 1; i < n; i++) { element = array[i]; j = i - 1; //17///0 /* Move elements of arr[0..i-1], that are greater than key by one position */ while (j >= 0 && array[j] > element) { array[j + 1] = array[j]; j = j - 1; } array[j + 1] = element; } }
  • 11.  Selection sort is a sorting algorithm that selects the smallest element from an unsorted list in each iteration and places that element at the beginning of the unsorted list. Algorithm  Step 1 - Select the first element of the list (i.e., Element at first position in the list).  Step 2: Compare the selected element with all the other elements in the list.  Step 3: In every comparision, if any element is found smaller than the selected element (for Ascending order), then both are swapped.  Step 4: Repeat the same procedure with element in the next position in the list till the entire list is sorted.
  • 12. Complexity of the Selection Sort Algorithm  To sort an unsorted list with 'n' number of elements, we need to make ((n-1)+(n-2)+(n-3)+......+1) = (n (n-1))/2 number of comparisons in the worst case.  If the list is already sorted then it requires 'n' number of comparisons. Worst Case : O(n2) Best Case : Ω(n2) Average Case : Θ(n2) Selection Sort Logic //Selection sort logic for(i=0; i<size; i++) { for(j=i+1; j<size; j++) { if(list[i] > list[j]) { temp=list[i]; list[i]=list[j]; list[j]=temp; } } }