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

PPTX
Bubble sort | Data structure |
PPT
Selection sort
PPTX
Doubly Linked List
PPT
Binary Search
PPT
Insertion sort bubble sort selection sort
PPTX
Binary search
PPTX
stack & queue
PPTX
Searching and sorting
Bubble sort | Data structure |
Selection sort
Doubly Linked List
Binary Search
Insertion sort bubble sort selection sort
Binary search
stack & queue
Searching and sorting

What's hot (20)

PPTX
PPT
Bubble sort
PPT
K Map Simplification
PDF
Array linear data_structure_2 (1)
PPTX
Ppt bubble sort
PPTX
Chapter 5 boolean algebra
PPTX
Arithmetic Expression
PPTX
Stacks IN DATA STRUCTURES
PDF
Data representation in computers
PPTX
PDF
linear search and binary search
PPTX
Radix and Shell sort
PPTX
STACKS IN DATASTRUCTURE
PPTX
single linked list
PPTX
Selection sort
PPT
Priority queues
PPTX
Linear Search
PDF
Binary codes
PPT
BackTracking Algorithm: Technique and Examples
PPTX
Subtractor (1)
Bubble sort
K Map Simplification
Array linear data_structure_2 (1)
Ppt bubble sort
Chapter 5 boolean algebra
Arithmetic Expression
Stacks IN DATA STRUCTURES
Data representation in computers
linear search and binary search
Radix and Shell sort
STACKS IN DATASTRUCTURE
single linked list
Selection sort
Priority queues
Linear Search
Binary codes
BackTracking Algorithm: Technique and Examples
Subtractor (1)
Ad

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

PPTX
Selection sort and insertion sort
PPTX
Selection Sort and Insertion Sort
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
PPT
Lecture_4 (Sorting Algorithms) before mids - Copy.ppt
PPTX
Sorting Algorithms
PPTX
sorting-160810203705.pptx
PPTX
sorting-160810203705.pptx
PPTX
Data Structure and algorithms for software
PPT
358 33 powerpoint-slides_14-sorting_chapter-14
PPTX
my docoment
PPT
MergesortQuickSort.ppt
PPT
presentation_mergesortquicksort_1458716068_193111.ppt
PPTX
1.4 Sorting.pptx
PPT
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
PPT
03_sorting123456789454545454545444543.ppt
PPT
03_sorting and it's types with example .ppt
PDF
Class13_Quicksort_Algorithm.pdf
PDF
Sorting algorithms bubble sort to merge sort.pdf
PPT
Insert Sort & Merge Sort Using C Programming
Selection sort and insertion sort
Selection Sort and Insertion Sort
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
Lecture_4 (Sorting Algorithms) before mids - Copy.ppt
Sorting Algorithms
sorting-160810203705.pptx
sorting-160810203705.pptx
Data Structure and algorithms for software
358 33 powerpoint-slides_14-sorting_chapter-14
my docoment
MergesortQuickSort.ppt
presentation_mergesortquicksort_1458716068_193111.ppt
1.4 Sorting.pptx
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
03_sorting123456789454545454545444543.ppt
03_sorting and it's types with example .ppt
Class13_Quicksort_Algorithm.pdf
Sorting algorithms bubble sort to merge sort.pdf
Insert Sort & Merge Sort Using C Programming
Ad

Recently uploaded (20)

DOCX
573137875-Attendance-Management-System-original
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Digital Logic Computer Design lecture notes
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
Geodesy 1.pptx...............................................
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
573137875-Attendance-Management-System-original
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Mechanical Engineering MATERIALS Selection
Internet of Things (IOT) - A guide to understanding
Digital Logic Computer Design lecture notes
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Automation-in-Manufacturing-Chapter-Introduction.pdf
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Operating System & Kernel Study Guide-1 - converted.pdf
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Geodesy 1.pptx...............................................
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx

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