ASSIGNMENT WEEK – 10
Name – HIMANSHU RAJ
Register No. - EA2331201010152
Program - B.C.A.-Data Science
Subject – Data Structure & Algorithm (DSA)
Semester – II Semester
EA2331201010152
1. Illustrate the insertion sort algorithm for the following array. 3 2 4 1 8 7 5
Ans. Insertion Sort
Insertion sort iterates through the array, gradually building a sorted sub-array at the
beginning. For each element, it compares it with the elements in the sorted sub-array and
inserts it in its correct position.
Array: 3 2 4 1 8 7 5
Steps:
• Pass 1 (considering the first element, 3, already sorted):
o We treat the first element (3) as the sorted sub-array.
• Pass 2:
o Consider the next element, 2. Since 2 is less than the element in the sorted sub-
array (3), we swap them. Now the sorted sub-array becomes {2, 3}.
• Pass 3:
o Consider the next element, 4. As 4 is greater than 2 but less than 3, we insert it
at its correct position between 2 and 3. The sorted sub-array becomes {2, 3, 4}.
• Pass 4:
o Consider the next element, 1. It's the smallest element encountered so far. We
shift the elements in the sorted sub-array (2 and 3) one position to the right and
insert 1 at the beginning. The sorted sub-array becomes {1, 2, 3, 4}.
• Pass 5:
o The next element is 8. Since it's larger than all elements in the sorted sub-array,
we insert it at the end. The sorted sub-array becomes {1, 2, 3, 4, 8}.
• Pass 6:
o Consider 7. It's greater than 1, 2, 3, and 4, but less than 8. We swap it with 4 to
place it in its correct position. The sorted sub-array becomes {1, 2, 3, 7, 8}.
• Pass 7:
o The last element is 5. We compare it with elements in the sorted sub-array. It's
less than 7 and 8, but greater than 1, 2, and 3. We shift elements (3 and 7) to the
right and insert 5 between 3 and 7. The final sorted array becomes {1, 2, 3, 5, 7,
8}.
2. Illustrate the bubble sort algorithm for the following array. 5 1 4 2 8 7 3
Ans. Bubble Sort
Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares
adjacent elements, and swaps them if they are in the wrong order. The pass through the list is
repeated until no swaps are needed, which indicates that the list is sorted. In each pass, the
largest element "bubbles" up to its correct position at the end of the list.
Array: 5 1 4 2 8 7 3
EA2331201010152
Steps:
• Pass 1:
o Compare the first two elements (5 and 1). Since 5 is greater than 1, swap them.
o Continue comparing and swapping adjacent elements throughout the unsorted
portion of the list (marked with 'X' below). In each comparison, the larger
element is swapped to the right.
Array: 1 5 4 2 8 7 3 (X)
• Pass 2:
o Repeat the comparison and swapping process within the unsorted portion,
excluding the elements already fixed in pass 1. With each pass, the unsorted
portion shrinks as elements move to their correct positions.
Array: 1 4 5 2 8 7 3 (X)
• Pass 3:
o Comparisons and swaps continue within the shrinking unsorted portion. As
more elements reach their correct positions, the unsorted portion becomes
smaller.
Array: 1 2 4 5 8 7 3 (X)
• Pass 4:
o At this point, only one comparison is needed (between the second and third
elements from the end) as the largest element (8) has already bubbled up to the
end. If the elements are in the correct order (no swap required), this pass
indicates that the list is sorted.
Array: 1 2 3 4 5 7 8 (Sorted)
• Pass 5 and subsequent passes:
o Since the array is already sorted (no swaps in pass 4), no further passes are
required. The sorting process is complete.
EA2331201010152