Professional Documents
Culture Documents
DS Week 4 Lecture by DR Gaurav
DS Week 4 Lecture by DR Gaurav
Week 4 Lecture
by
Dr Gaurav Kumar
Asst. Prof, Bennett University
Quick Recap of Last Week’s Learnings
Correct Answer is c
General Assessment Time
10
9
8
7
6
5
4
3
2
1
What is the Time Complexity of this
algorithm?
a) f(n)= O(n)
Algorithm print(n)
{ b) f(n)= O(n2)
for ( i=n; i>0; i=i/2)
c) f(n)= O(logn)
{
Which one is the printf(“This is number: %d”, i); d) I am confused
correct answer? }
}
Correct Answer is c
General Assessment Time
10
9
8
7
6
5
4
3
2
1
What is the Time Complexity of deleting
an element from beginning of the array?
a) f(n)= O(1)
b) f(n)= O(n2)
d) I am confused
correct answer?
Correct Answer is c
General Assessment Time
10
9
8
7
6
5
4
3
2
1
What is the Time Complexity of inserting
an element at the end of the array?
a) f(n)= O(1)
b) f(n)= O(n2)
d) I am confused
correct answer?
Correct Answer is A
General Assessment Time
10
9
8
7
6
5
4
3
2
1
What is the Worst Case Time Complexity
of Bubble Sort Algorithm?
a) f(n)= O(1)
b) f(n)= O(n2)
d) I am confused
correct answer?
Correct Answer is B
General Assessment Time
10
9
8
7
6
5
4
3
2
1
Number of Swap Operations is performed in
the Optimized Bubble Sort Algorithm?
a) n
b) n2
d) 0
correct answer?
Correct Answer is D
Sorting Algorithms
• Bubble Sort
• Selection Sort
• Insertion Sort
• Shell Sort
• Merge Sort
• Counting Sort
Understanding Bubble Sort
*Movement of array elements is just like the movement of air bubbles in the water
Analyzing Time Complexity of Bubble Sort
1 2 3 4 5 6 void bubbleSort (int array[], int size) If there is no swap in the first
{ pass, and if I come out from the
second loop without going
for (int pass = 1; pass < size; ++pass) further then I can improve the
Pass #Comp #Swap { time
for (int i = 0; i <= size - pass; ++i)
1 5 0 {
if (array[i] > array[i + 1])
2 4 0 {
int temp = array[i];
3 3 0 array[i] = array[i + 1];
array[i + 1] = temp;
4 2 0 }
}
5 1 0 }
}
3 3 0
List is already sorted 4 2 0
Note: While looking at No Swap Execution (Swap=0) in the first pass, we can not say
that list is completely sorted. Hence, by introducing flag concept (as similar to bubble
sort) in the selection sorting algorithm, we can not improve the time complexity.
(Readers can make the correction in the previous week (Week 3) lecture slide)
Can TC further improve ?
Step3 - Now, compare the key with all elements in the sorted
array.
key element, then do nothing and key will point to the next
the right.
Step 4 - If the element in the sorted array is smaller than the key 3
element, then do nothing and key will point to the next element 4 1 10 5
Key
else, shift greater elements in the array towards the right. Key Sorted Un-Sorted
array. 5
1 3 4 10 5
Step 4 - If the element in the sorted array is smaller than
the key element, then key will point to the next element else 1 3 4 5 10
shift greater elements in the array towards the right.
Sorted Key
Un-Sorted
4 3 1 10 5
Step 1 - If the element is the first element, assume that it is
j
already sorted. Return 1.
void insertionSort(int arr[], int n)
Step2 - Pick the next element and store it separately in a key. {
int i, key, j;
Step3 - Now, compare the key with all elements in the sorted for (i = 1; i < n; i++)
{
array. key = arr[i];
j = i - 1;
Step 4 - If the element in the sorted array is smaller than the
while (arr[j] > key && j >= 0 )
key element, then key will point to the next element else {
arr[j + 1] = arr[j]; //Shifting to Right
shift greater elements in the array towards the right.
j = j - 1;
}
Step 5 - Insert the value.
arr[j + 1] = key;
Step 6 - Repeat until the array is sorted. }
}
Analyzing Time Complexity of Insertion Sort
Sorted Key
Un-Sorted
1 1
n-1 n-1
1 0
5 4 3 2 1 2 3 4 5 1
Gap of 4
• It first sorts elements that are far apart from each other
and successively reduces the interval between the
elements to be sorted.
9 5
8 6
• There is a concept of Gap or Interval, that can be
4
3
determined by following ways:
7 1
Gap of 4
We will use very simple technique of N/2, N/4, ...1
to calculate the gaps in our algorithm.
5
9
In Pass-1,
if the array size is N = 8 then,
8 6
• Then 1st element is compared with the 5th
4
3
element. 1
7
In Pass-2, Gap of N/4 = 8/4 = 2 is taken and again the elements lying at these gaps are sorted.
Sort the Elements
Imagination
Gap of 2
List 1- 5, 3, 9, 4
List 2- 6, 1, 8, 7
5 3 9 4
Apply Insertion Sort
6 1 8 7
Understanding Shell Sort Algorithms
In Pass-2, Gap of N/4 = 8/4 = 2 is taken and again the elements lying at these gaps are sorted.
0 1 2 3 4 5 6 7
5 3
6 1
Understanding Shell Sort Algorithms
In Pass-2, Gap of N/4 = 8/4 = 2 is taken and again the elements lying at these gaps are sorted.
0 1 2 3 4 5 6 7
5 3 9 4
6 1 8 7
Understanding Shell Sort Algorithms
In Pass 3, Finally, when the gap is N/8 = 8/8 =1 then the array
elements lying at the gap of 1 are sorted.
3 1 4 6 5 7 9 8
0 1 2 3 4 5 6 7
• Merge Sort
• Count Sort
• Quick Assessments
Counting sort is a stable algorithm because the relative order of similar elements are not
Any Queries?
Office MCub311
Discussion Time: 3-5 PM
Mob: +91-8586968801