Module5 DSA
Module5 DSA
Fourth Pass
Improved Bubble sort algorithm
Bubble_sort(A,N)
• Step 1:Repeat steps 2 for i=0 to N-2
• Step 2 : flag=0
• Step 2: Repeat for j=0 to N-i-2
• Step 3: If A[j] > A[j+1], then
SWAP A[j] and A[j+1]
flag=1
[End of inner loop]
If(flag==0)
break
[End of outer loop]
• Step 4 : Exit
Time and Space complexity
• Time Complexity of Bubble sort
• Best case scenario: The best case scenario occurs when the array is already sorted. Time
complexity in the best case scenario is Ω (n) because it has to traverse through all the
elements once.
• Worst case and Average case scenario: In Bubble Sort, n-1 comparisons are done in the 1st
pass, n-2 in 2nd pass, n-3 in 3rd pass and so on. So, the total number of comparisons will
be:
Sum = (n-1) + (n-2) + (n-3) + ..... + 3 + 2 + 1
Sum = n(n-1)/2
Hence, the worst and average time complexity is of the order n2 or θ(n2)/O(n2).
• Space Complexity of Bubble sort
• The space complexity for the algorithm is O(1), because only a single additional memory
space is required for temporary variable used for swapping and for flag variable.
• It is an in-place sorting algorithm, which modifies the original array's elements to sort the given
array.
Insertion sort
• Algorithm that places an unsorted element Insertion sort(A,N)
at its suitable place in each iteration.
Step 1: repeat steps 2 to 5 for k=1 to N-
• The array to be sorted is divided into two 1
sets. One that stores sorted values and
another that contains unsorted values. Step 2: set key = A[k]
• Step 1 − If it is the first element, it is already Step 3: set j = k-1
sorted. Step 4: repeat while (j>=0 &&key <= A[j])
• Step 2 − Pick next element, name it as key set A[j+1] =A[j]
• Step 3 − Compare key with all elements in
the sorted sub-list
set j=j-1
• Step 4 − Shift all the elements in the sorted [end of inner loop]
sub-list that is greater than the value to be Step 5: set A[j+1] = key
sorted
[end of loop]
• Step 5 − Place the key at appropriate place
Step 6:Exit
• Step 6 − Repeat until list is sorted
Working of insertion sort
Else End of if
Step 4: copy the contents of temp back to arr,set index=0
set temp[k]=arr[j] Step5: repeat while index<k
End of loop
Merge and Mergesort
Time and Space complexity
Time Complexity
Best O(n*log n)
Worst O(n*log n)
Average O(n*log n)
CS314 33
Binary Heap
• Complete binary tree ,all its elements can be stored
sequentially in an array
• Root node = A[0]
• Children of A[i] = A[2i+1], A[2i+2]
• Keep track of current size N (number of nodes)
value 7 5 6 2 44 23 9 5 6
index 0 1 2 3 4 5 6 7 2 44 23 9
N=5
Max heap and min heap
Insertion in a binary heap
• Consider a heap H with n elements. Inserting a new value into the
heap is done in the following steps:
• 1) add a new value at the bottom of H in such a way such that H is still
a complete binary tree but not necessarily a heap
• 2) Let the new value rise to its appropriate place in H so that H now
becomes a heap as well.
Constructing a max heap(heapify)
Step 1:
Step 3:
Deleting elements from a heap(for sorting)
• Consider a max heap H having N elements. An element is always
deleted from the root of the heap.
• Step 1:Replace the root node with the last node’s value so that H is
still a complete binary tree but not necessarily a heap.
• Step 2:Sink down the new node’s value so that H satisfies the heap
property.
swap 50 with 33
heapify
1. Create a complete binary tree from the array
2. Start from the first index of non-leaf node whose index is given by n/2-1.
3. Set current element i as largest
4. The index of left child is given by 2*i+1 and the right child is given by 2*i+2.
5. If left chid is greater than current element set left child index as largest.
6. If right child is greater than element in largest, set right child index as largest
7. Swap largest with current element
8. Repeat steps 2-7 until all subtrees are heapified.
Heapify procedure
Heap sort algorithm Heapify (arr[], n, i)
Heap_Sort (arr[], n)
set largest = i;2
1)Creating the initial Max heap
set left = 2*i + 1; // Left child
repeat for i = n/2 – 1 to 0 //i=2,1,0
set right = 2*i + 2; // Right child
heapify(arr, n, i)
Check if left child exists and is larger than root
If (left < n && arr[left] > arr[largest]):
2) Swapping largest element and repeating
the steps further for sorting purpose Largest = left;
repeat for i = n-1 to 1: Check if right child exists and is larger than larges
3 6 5 1 9 8 4 7 swapping
3 1 5 6 9 8 4 7 No swapping
3 1 5 6 9 8 4 7
No swapping
3 1 5 6 9 8 4 7
swapping
3 1 5 6 4 8 9 7
swapping
3 1 4 6 5 7 9 8 No swapping
third iteration ,gap=n/8=8/8=1 Fourth iteration,gap =n/16
3 1 4 6 5 7 9 8 which is less than 1
swapping
1 3 4 6 5 7 9 8 No swapping
1 3 4 6 5 7 9 8 No swapping
• stop
1 3 4 5 6 7 9 8 swapping
1 3 4 5 6 7 9 8 No swapping
1 3 4 5 6 7 9 8
1 3 4 5 6 7 8 9
swapping
pseudocode
shellSort(int arr[], int num)
int i, j, gap;
for (gap = num / 2; gap > 0; gap = gap / 2)
{
for (j = gap; j < num; j++)
{
for(i = j - gap; i >= 0; i = i - gap)
{
if (arr[i+gap] >= arr[i])
break;
else
{
swap(a[i],a[i+gap])
}}}}
Time and space complexity
Time Complexity
Best O(nlog n)
Worst O(n2)
Average O(nlog n)
O(n.log(n))
Shell Sort O(n2) O(1) Yes