SlideShare a Scribd company logo
Insertion Sort, Quick
Sort And Their
Complexity
Presented by:
1.Niaz Mahmud Roll:1507111
2.M A Muit Sowrav Roll:1507112
3.Tanim Ahmed Roll:1507113
4.Motaleb Hossen Manik Roll:1507114
5.Arafat Mahmud Roll:1507115 1
What is Insertion Sort and How it works ?
 This is a good sorting technique !
 We will implement it using array of elements
 For Insertion sort, we need to consider an array in two parts – Sorted part &
Unsorted part !
 Let us consider an unsorted array of integer with 6 elements : 7 2 4 1 5 3
 This is an unsorted array
 We will have to divide this array into sorted part and unsorted part
 The first element is always sorted
 We will start from left and will continue a process towards right
 So, 7 is in the sorted part and rest of the elements are in the unsorted part
2
What is Insertion Sort and How it works ?
3
What is Insertion Sort and How it works ?
4
What is Insertion Sort and How it works ?
5
What is Insertion Sort and How it works ?
6
What is Insertion Sort and How it works ?
7
What is Insertion Sort and How it works ?
8
What is Insertion Sort and How it works ?
9
What is Insertion Sort and How it works ?
10
What is Insertion Sort and How it works ?
11
What is Insertion Sort and How it works ?
12
What is Insertion Sort and How it works ?
13
What is Insertion Sort and How it works ?
14
What is Insertion Sort and How it works ?
15
What is Insertion Sort and How it works ?
16
What is Insertion Sort and How it works ?
17
What is Insertion Sort and How it works ?
So, this is our sorted array !
18
Now We Will Observe The
Implementation
 To implement this algorithm, we will use function
 The function parameters will be the element number and an array
So, lets see how to implement it…
19
 First of all declare a global variable (hers ‘n’ is the global variable)
 Take an array of integer
 Insert values in the array
 Make a function called “ascending”
 This function’s body will have the rest of the implementation
20
So, this is our array
declaration and
initialization.
Simple !
Continue…
21
 We have passed the
parameters in the
function
 No we will observe
the function and
its body
22
1.As the first element is in the
sorted array, so we will start sorting
from the second element
2. Two variables ‘blank’ and ‘ value’
are initialized in the first loop
3. The ‘value’ is the ‘i th’ element
of the array
4.We will check if blank is greater
than 0 and if the value of blank-1 is
greater than the original value
5. If then condition is true than we
will simply assign the value of
blank-1 in the blank space
23
6. The blank will come forward in
every step as shown in the
algorithm
7. We will continue the inner loop
until the both condition or any one
of this is false
8. When the condition is false, we
will simply come out of the inner
loop and will assign the original
value in the blank space
9. The outer loop will continue till
the last element of the array
24
10. If should be noted than we are
assigning a new value when we are
coming out of the inner loop
11. Finally we will simply print the
array elements
25
 If we insert this elements in our array…
7 2 4 1 5 3
We will get this sorted array…
1 2 3 4 5 7
Thank You
26
Quick sort:
 Quicksort is an algorithm of divide and conquer type . It is an algorithm design
based on multi-branched recursion . It works by recursively breaking down a
problem into two or more sub problems of the same related type
27
 In quick sort we will divide the problems in two sub list.
 And the algorithm will find the final position of one of the
numbers. From this position the value of the left side will be less
than the numbers and the value will be greater than the right
 continue..
From previous..
28
 For example an array of element 12.
 44 33 11 55 77 90 40 60 99 22 88 66
 We will use the first number 44.
 Beginning with the last number 66 we will scan the list from right
to left comparing with 44 and when find less than 44 then it will
be interchanged.
 Above array 22 is less than 44.So we will swap it
 22 33 11 55 77 90 40 60 99 44 88 66
 continue..
From previous
29
 22 33 11 55 77 90 40 60 99 44 88 66
 Now scanning will be from opposite direction.
 We see 55 is greater than 44.So array will be such that
 33 11 44 77 90 40 60 99 55 88 66
Continue..
From previous…
30
 22 33 11 44 77 90 40 60 99 55 88 66
 Following this process recursively .
 Now we get the array such that
 22 33 11 40 77 90 44 60 99 55 88 66
 Repeat this process..
 When we find that 77 is greater than 44
 continue..
From previous
31
 22 33 11 40 77 90 44 60 99 55 88 66
And finally we get the array such that..
 22 33 11 40 44 90 77 60 99 55 88 66
First sub-list Second sub-list
 continue..
From previous
32
 And this is our expected position of 44.
 In this position the value of left is less than 44 and the value of
right side is greater than 44.

 continue..
From previous
33
 And this is our expected position of 44.
 In this position the value of left is less than 44 and the value of right side is
greater than 44.

 continue..
From previous
34
 Now we will finish our rest step using this list
 “Please Always keep me in your prayers”
 Thank You
From previous
35
So our new array is
22 33 11 40 44 90 77 60 99 55 88 66
 Now we need to split it into two part based on 44
 The same reduction steps need to be performed until we get the sorted
array
 We will use stack to process the next steps
 We will use two stack called ‘LOWER’ and ‘UPPER’ to hold the boundary
index of this parts
 We will push the boundary indexes into the two stacks and will perform
the next steps
 Boundary indexes are those indexes adjacent to 44 (that means the index
of 40 and 90) and the first index of this array and the last index of this
array (that means the index of 22 and 66).
36
6
1
12
4
Lower Stack Upper Stack
So, the stacks will be like this
37
 If we pop from the two stacks then our stacks will contain 1 and 4
 Now we will preform the reduction step on the lower boundary 6 and
upper boundary 12
1 4
Lower Stack Upper Stack
38
A[6] A[7] A[8] A[9] A[10] A[11] A[12]
90 77 60 99 55 88 66
66 77 60 99 55 88 90
66 77 60 90 55 88 99
66 77 60 88 55 90 99
First part Second part
Index 6 to 12
39
 We have got two new boundary index: 6 & 10
 We will push the boundary indexes into the two stacks
 So our stack will be like this…
6
1
10
4
Lower Stack Upper Stack
40
 Again we will pop the values from the stacks & will perform the same process
1 4
Lower Stack Upper Stack
41
 We will be doing the same reduction steps until our stacks
become empty
 When our stacks are empty, our task is over
 We will get the complete sorted array !
42
 If we complete this algorithm, we will get this
array
 11 22 33 40 44 55 60 66 77 88 90 99
43
Thank You
44
Complexity Of Insertion Sort And Quick
Sort
 Complexity indicates space complexity and time complexity
 Complexity can be considered in three cases
1. Best case
2. Average case
3. Worse case
45
Complexity Of Insertion Sort (Worst case)
 We will calculate the complexity using function
 First of all the worst case occurs when the array A is in inverse order
Like to sort in ascending order: 8 7 5 3 1
For this example the inner loop must use the maximum number K-1 of
comparison
46
Complexity Of Insertion Sort (Worst case)
 So the function is,
 f(n)=1+2+3+……….(n-2)+(n-1)
 This function is for N elements of the array
 Simplifying this equation we get, f(n) =
𝑛(𝑛−1)
2
=
𝑛2
2
−
𝑛
2
= O(𝑛2)
47
Complexity Of Insertion Sort (Average case)
 For average case there will be approximately (K-1)/2 comparisons in the loop
 So the function will be, f(n)=1/2 + 2/2 +……+ (n-1)/2 =
𝑛(𝑛−1)
4
=
𝑛2
4
−
𝑛
4
= O(𝑛2)
48
Complexity Of Quick Sort (Worst case)
 The worst case occurs when the list is already sorted
 Because for this case the first element requires N comparisons to recognize
that it remains in the first position
 Like : 1 3 4 5 6 7
 For this list the first sub-list is empty and the second sub-list contains (n-1)
elements
 The second element requires n-1 comparison to recognize that it remains in
the second position
49
Complexity Of Quick Sort (Worst case)
 So that the function will be f(n)=n+(n-1)+….+2+1=
𝑛(𝑛+1)
2
=
𝑛2
2
+
𝑛
2
= O(𝑛2)
50
Complexity Of Quick Sort (Average case)
 On the Average case each reduction step of the algorithm produces two sub-lists .
Accordingly:
 (1) Reducing the initial list places 1 element and produces two sub-lists.
 (2) Reducing the two sub-lists places 2 elements and produces four sub-lists.
 (3) Reducing the four sub-lists places 4 elements and produces eight sub-lists.
 This process continues until the list is fully sorted.
 There will be approximately 𝑙𝑜𝑔2n levels of reduction steps.
51
Complexity Of Quick Sort (Average case)
 Furthermore , each level uses at most n comparisons.
 So,
 f(n)=O(n log n)
 In fact mathematical analysis and empirical evidence have both shown that
 f(n)≈1.4floor function of(n log n)
52
Thank You All !
53

More Related Content

What's hot (20)

PPTX
single linked list
Sathasivam Rangasamy
 
PPTX
Data structure by Digvijay
Digvijay Singh Karakoti
 
PDF
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
PPT
Merge sort
Vidushi Pathak
 
PPTX
linked list in data structure
shameen khan
 
PPTX
Queue
Raj Sarode
 
PPT
skip list
iammutex
 
PPTX
Stack project
Amr Aboelgood
 
PPTX
Quick sort-Data Structure
Jeanie Arnoco
 
PPTX
Binary Tree Traversal
Dhrumil Panchal
 
PDF
Linear search algorithm
NeoClassical
 
PPTX
Quicksort Presentation
irdginfo
 
PPTX
Insertion sort
almaqboli
 
PPTX
Analysis of Algorithm (Bubblesort and Quicksort)
Flynce Miguel
 
PPTX
Divide and conquer - Quick sort
Madhu Bala
 
PDF
Array data structure
maamir farooq
 
PPTX
Priority Queue in Data Structure
Meghaj Mallick
 
PPTX
Insertion sort
Monalisa Patel
 
PPTX
Graph traversals in Data Structures
Anandhasilambarasan D
 
PDF
Binary tree
Rajendran
 
single linked list
Sathasivam Rangasamy
 
Data structure by Digvijay
Digvijay Singh Karakoti
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
Merge sort
Vidushi Pathak
 
linked list in data structure
shameen khan
 
Queue
Raj Sarode
 
skip list
iammutex
 
Stack project
Amr Aboelgood
 
Quick sort-Data Structure
Jeanie Arnoco
 
Binary Tree Traversal
Dhrumil Panchal
 
Linear search algorithm
NeoClassical
 
Quicksort Presentation
irdginfo
 
Insertion sort
almaqboli
 
Analysis of Algorithm (Bubblesort and Quicksort)
Flynce Miguel
 
Divide and conquer - Quick sort
Madhu Bala
 
Array data structure
maamir farooq
 
Priority Queue in Data Structure
Meghaj Mallick
 
Insertion sort
Monalisa Patel
 
Graph traversals in Data Structures
Anandhasilambarasan D
 
Binary tree
Rajendran
 

Similar to Insertion Sort, Quick Sort And Their complexity (20)

PPTX
Selection sort and insertion sort
May Ann Mendoza
 
PPTX
Selection Sort and Insertion Sort
tamayaoraquel
 
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
VISWANATHAN R V
 
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
kncetaruna
 
PPTX
Sorting Algorithms
Afaq Mansoor Khan
 
PPTX
sorting-160810203705.pptx
AnSHiKa187943
 
PPTX
sorting-160810203705.pptx
VarchasvaTiwari2
 
PPTX
Data Structure and algorithms for software
ManishShukla712917
 
PPT
358 33 powerpoint-slides_14-sorting_chapter-14
sumitbardhan
 
PPTX
my docoment
NeeshanYonzan
 
PPT
MergesortQuickSort.ppt
AliAhmad38278
 
PPT
presentation_mergesortquicksort_1458716068_193111.ppt
ajiths82
 
PPTX
1.4 Sorting.pptx
Sujan527908
 
PPT
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
RafishaikIT02044
 
PPT
03_sorting and it's types with example .ppt
vanshii9976
 
PPT
03_sorting123456789454545454545444543.ppt
ssuser7b9bda1
 
PDF
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
PDF
Sorting algorithms bubble sort to merge sort.pdf
AyeshaMazhar21
 
PPT
Insert Sort & Merge Sort Using C Programming
chandankumar364348
 
PPTX
Sorting Algorithms to arrange data in particular format
itsusamazahid
 
Selection sort and insertion sort
May Ann Mendoza
 
Selection Sort and Insertion Sort
tamayaoraquel
 
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
VISWANATHAN R V
 
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
kncetaruna
 
Sorting Algorithms
Afaq Mansoor Khan
 
sorting-160810203705.pptx
AnSHiKa187943
 
sorting-160810203705.pptx
VarchasvaTiwari2
 
Data Structure and algorithms for software
ManishShukla712917
 
358 33 powerpoint-slides_14-sorting_chapter-14
sumitbardhan
 
my docoment
NeeshanYonzan
 
MergesortQuickSort.ppt
AliAhmad38278
 
presentation_mergesortquicksort_1458716068_193111.ppt
ajiths82
 
1.4 Sorting.pptx
Sujan527908
 
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
RafishaikIT02044
 
03_sorting and it's types with example .ppt
vanshii9976
 
03_sorting123456789454545454545444543.ppt
ssuser7b9bda1
 
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
Sorting algorithms bubble sort to merge sort.pdf
AyeshaMazhar21
 
Insert Sort & Merge Sort Using C Programming
chandankumar364348
 
Sorting Algorithms to arrange data in particular format
itsusamazahid
 
Ad

Recently uploaded (20)

PPTX
Presentation 2.pptx AI-powered home security systems Secure-by-design IoT fr...
SoundaryaBC2
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PPT
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PPTX
Product Development & DevelopmentLecture02.pptx
zeeshanwazir2
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PPTX
Thermal runway and thermal stability.pptx
godow93766
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPTX
Big Data and Data Science hype .pptx
SUNEEL37
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Presentation 2.pptx AI-powered home security systems Secure-by-design IoT fr...
SoundaryaBC2
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
Product Development & DevelopmentLecture02.pptx
zeeshanwazir2
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Thermal runway and thermal stability.pptx
godow93766
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Big Data and Data Science hype .pptx
SUNEEL37
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Ad

Insertion Sort, Quick Sort And Their complexity

  • 1. Insertion Sort, Quick Sort And Their Complexity Presented by: 1.Niaz Mahmud Roll:1507111 2.M A Muit Sowrav Roll:1507112 3.Tanim Ahmed Roll:1507113 4.Motaleb Hossen Manik Roll:1507114 5.Arafat Mahmud Roll:1507115 1
  • 2. What is Insertion Sort and How it works ?  This is a good sorting technique !  We will implement it using array of elements  For Insertion sort, we need to consider an array in two parts – Sorted part & Unsorted part !  Let us consider an unsorted array of integer with 6 elements : 7 2 4 1 5 3  This is an unsorted array  We will have to divide this array into sorted part and unsorted part  The first element is always sorted  We will start from left and will continue a process towards right  So, 7 is in the sorted part and rest of the elements are in the unsorted part 2
  • 3. What is Insertion Sort and How it works ? 3
  • 4. What is Insertion Sort and How it works ? 4
  • 5. What is Insertion Sort and How it works ? 5
  • 6. What is Insertion Sort and How it works ? 6
  • 7. What is Insertion Sort and How it works ? 7
  • 8. What is Insertion Sort and How it works ? 8
  • 9. What is Insertion Sort and How it works ? 9
  • 10. What is Insertion Sort and How it works ? 10
  • 11. What is Insertion Sort and How it works ? 11
  • 12. What is Insertion Sort and How it works ? 12
  • 13. What is Insertion Sort and How it works ? 13
  • 14. What is Insertion Sort and How it works ? 14
  • 15. What is Insertion Sort and How it works ? 15
  • 16. What is Insertion Sort and How it works ? 16
  • 17. What is Insertion Sort and How it works ? 17
  • 18. What is Insertion Sort and How it works ? So, this is our sorted array ! 18
  • 19. Now We Will Observe The Implementation  To implement this algorithm, we will use function  The function parameters will be the element number and an array So, lets see how to implement it… 19
  • 20.  First of all declare a global variable (hers ‘n’ is the global variable)  Take an array of integer  Insert values in the array  Make a function called “ascending”  This function’s body will have the rest of the implementation 20
  • 21. So, this is our array declaration and initialization. Simple ! Continue… 21
  • 22.  We have passed the parameters in the function  No we will observe the function and its body 22
  • 23. 1.As the first element is in the sorted array, so we will start sorting from the second element 2. Two variables ‘blank’ and ‘ value’ are initialized in the first loop 3. The ‘value’ is the ‘i th’ element of the array 4.We will check if blank is greater than 0 and if the value of blank-1 is greater than the original value 5. If then condition is true than we will simply assign the value of blank-1 in the blank space 23
  • 24. 6. The blank will come forward in every step as shown in the algorithm 7. We will continue the inner loop until the both condition or any one of this is false 8. When the condition is false, we will simply come out of the inner loop and will assign the original value in the blank space 9. The outer loop will continue till the last element of the array 24
  • 25. 10. If should be noted than we are assigning a new value when we are coming out of the inner loop 11. Finally we will simply print the array elements 25
  • 26.  If we insert this elements in our array… 7 2 4 1 5 3 We will get this sorted array… 1 2 3 4 5 7 Thank You 26
  • 27. Quick sort:  Quicksort is an algorithm of divide and conquer type . It is an algorithm design based on multi-branched recursion . It works by recursively breaking down a problem into two or more sub problems of the same related type 27
  • 28.  In quick sort we will divide the problems in two sub list.  And the algorithm will find the final position of one of the numbers. From this position the value of the left side will be less than the numbers and the value will be greater than the right  continue.. From previous.. 28
  • 29.  For example an array of element 12.  44 33 11 55 77 90 40 60 99 22 88 66  We will use the first number 44.  Beginning with the last number 66 we will scan the list from right to left comparing with 44 and when find less than 44 then it will be interchanged.  Above array 22 is less than 44.So we will swap it  22 33 11 55 77 90 40 60 99 44 88 66  continue.. From previous 29
  • 30.  22 33 11 55 77 90 40 60 99 44 88 66  Now scanning will be from opposite direction.  We see 55 is greater than 44.So array will be such that  33 11 44 77 90 40 60 99 55 88 66 Continue.. From previous… 30
  • 31.  22 33 11 44 77 90 40 60 99 55 88 66  Following this process recursively .  Now we get the array such that  22 33 11 40 77 90 44 60 99 55 88 66  Repeat this process..  When we find that 77 is greater than 44  continue.. From previous 31
  • 32.  22 33 11 40 77 90 44 60 99 55 88 66 And finally we get the array such that..  22 33 11 40 44 90 77 60 99 55 88 66 First sub-list Second sub-list  continue.. From previous 32
  • 33.  And this is our expected position of 44.  In this position the value of left is less than 44 and the value of right side is greater than 44.   continue.. From previous 33
  • 34.  And this is our expected position of 44.  In this position the value of left is less than 44 and the value of right side is greater than 44.   continue.. From previous 34
  • 35.  Now we will finish our rest step using this list  “Please Always keep me in your prayers”  Thank You From previous 35
  • 36. So our new array is 22 33 11 40 44 90 77 60 99 55 88 66  Now we need to split it into two part based on 44  The same reduction steps need to be performed until we get the sorted array  We will use stack to process the next steps  We will use two stack called ‘LOWER’ and ‘UPPER’ to hold the boundary index of this parts  We will push the boundary indexes into the two stacks and will perform the next steps  Boundary indexes are those indexes adjacent to 44 (that means the index of 40 and 90) and the first index of this array and the last index of this array (that means the index of 22 and 66). 36
  • 37. 6 1 12 4 Lower Stack Upper Stack So, the stacks will be like this 37
  • 38.  If we pop from the two stacks then our stacks will contain 1 and 4  Now we will preform the reduction step on the lower boundary 6 and upper boundary 12 1 4 Lower Stack Upper Stack 38
  • 39. A[6] A[7] A[8] A[9] A[10] A[11] A[12] 90 77 60 99 55 88 66 66 77 60 99 55 88 90 66 77 60 90 55 88 99 66 77 60 88 55 90 99 First part Second part Index 6 to 12 39
  • 40.  We have got two new boundary index: 6 & 10  We will push the boundary indexes into the two stacks  So our stack will be like this… 6 1 10 4 Lower Stack Upper Stack 40
  • 41.  Again we will pop the values from the stacks & will perform the same process 1 4 Lower Stack Upper Stack 41
  • 42.  We will be doing the same reduction steps until our stacks become empty  When our stacks are empty, our task is over  We will get the complete sorted array ! 42
  • 43.  If we complete this algorithm, we will get this array  11 22 33 40 44 55 60 66 77 88 90 99 43
  • 45. Complexity Of Insertion Sort And Quick Sort  Complexity indicates space complexity and time complexity  Complexity can be considered in three cases 1. Best case 2. Average case 3. Worse case 45
  • 46. Complexity Of Insertion Sort (Worst case)  We will calculate the complexity using function  First of all the worst case occurs when the array A is in inverse order Like to sort in ascending order: 8 7 5 3 1 For this example the inner loop must use the maximum number K-1 of comparison 46
  • 47. Complexity Of Insertion Sort (Worst case)  So the function is,  f(n)=1+2+3+……….(n-2)+(n-1)  This function is for N elements of the array  Simplifying this equation we get, f(n) = 𝑛(𝑛−1) 2 = 𝑛2 2 − 𝑛 2 = O(𝑛2) 47
  • 48. Complexity Of Insertion Sort (Average case)  For average case there will be approximately (K-1)/2 comparisons in the loop  So the function will be, f(n)=1/2 + 2/2 +……+ (n-1)/2 = 𝑛(𝑛−1) 4 = 𝑛2 4 − 𝑛 4 = O(𝑛2) 48
  • 49. Complexity Of Quick Sort (Worst case)  The worst case occurs when the list is already sorted  Because for this case the first element requires N comparisons to recognize that it remains in the first position  Like : 1 3 4 5 6 7  For this list the first sub-list is empty and the second sub-list contains (n-1) elements  The second element requires n-1 comparison to recognize that it remains in the second position 49
  • 50. Complexity Of Quick Sort (Worst case)  So that the function will be f(n)=n+(n-1)+….+2+1= 𝑛(𝑛+1) 2 = 𝑛2 2 + 𝑛 2 = O(𝑛2) 50
  • 51. Complexity Of Quick Sort (Average case)  On the Average case each reduction step of the algorithm produces two sub-lists . Accordingly:  (1) Reducing the initial list places 1 element and produces two sub-lists.  (2) Reducing the two sub-lists places 2 elements and produces four sub-lists.  (3) Reducing the four sub-lists places 4 elements and produces eight sub-lists.  This process continues until the list is fully sorted.  There will be approximately 𝑙𝑜𝑔2n levels of reduction steps. 51
  • 52. Complexity Of Quick Sort (Average case)  Furthermore , each level uses at most n comparisons.  So,  f(n)=O(n log n)  In fact mathematical analysis and empirical evidence have both shown that  f(n)≈1.4floor function of(n log n) 52