SlideShare una empresa de Scribd logo
1 de 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;
}
}
}

Más contenido relacionado

Similar a 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 a 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
 

Último

Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...amitlee9823
 
Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxJohnnyPlasten
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxolyaivanovalion
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFxolyaivanovalion
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% SecurePooja Nehwal
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysismanisha194592
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAroojKhan71
 
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort ServiceBDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort ServiceDelhi Call girls
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightDelhi Call girls
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Delhi Call girls
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfMarinCaroMartnezBerg
 
Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxolyaivanovalion
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxolyaivanovalion
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxolyaivanovalion
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Valters Lauzums
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxolyaivanovalion
 

Último (20)

Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptx
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptx
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFx
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
 
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort ServiceBDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
 
Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptx
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptx
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFx
 
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
 

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; } } }