Introduction to IEEE STANDARDS and its different types.pptx
Insertion Quick Sort 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
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
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