Solved
Problems
1. Given an array : 89, 20, 31, 56, 20. Sort this array in ascending order using (1) Bubble Sort (ii) Insertion Sort
i so 20 21 56 20
Answers
The given array 89, 20, 31, 56, 20 using (1) Bubble Sort (ii) Insertion Sort is as follows:
(1) Bubble Sort (Sorting Algorithm)
89, 20, 31, 56, 20
20, 89, 31, 56, 20
20, 31, 89, 56, 20
20, 31, 56, 89, 20
20, 31, 56, 20, 89
20, 31, 20, 56, 89
20, 20, 31, 56, 89
(ii) Insertion Sort (Sorting Algorithm)
89, 20, 31, 56, 20
20, 89, 31, 56, 20
20, 31, 89, 56, 20
20, 31, 56, 89, 20
20, 20, 31, 56, 89
Answer:(1) 20 , 20 , 31 ,56,89
Explanation:
To perform bubble sort, we repeatedly compare adjacent pairs of numbers in the list and swap them if they are in the wrong order. We continue this process until no more swaps are needed, which indicates that the list is sorted.
Here are the steps of bubble sort for the given list of numbers:
Compare the first two numbers (89 and 20). They are in the wrong order, so we swap them.
The list now becomes: 20, 89, 31, 56, 15
Compare the next two numbers (89 and 31). They are also in the wrong order, so we swap them.
The list now becomes: 20, 31, 89, 56, 15
Compare the next two numbers (89 and 56). They are in the wrong order, so we swap them.
The list now becomes: 20, 31, 56, 89, 15
Compare the next two numbers (89 and 15). They are in the wrong order, so we swap them.
The list now becomes: 20, 31, 56, 15, 89
We have completed the first pass of bubble sort. Now, we start the second pass.
Compare the first two numbers (20 and 31). They are in the correct order, so we do not swap them.
The list remains: 20, 31, 56, 15, 89
Compare the next two numbers (31 and 56). They are in the wrong order, so we swap them.
The list now becomes: 20, 56, 31, 15, 89
Compare the next two numbers (56 and 31). They are in the wrong order, so we swap them.
The list now becomes: 20, 31, 56, 15, 89
Compare the next two numbers (56 and 15). They are in the wrong order, so we swap them.
The list now becomes: 20, 31, 15, 56, 89
We have completed the second pass of bubble sort.
Therefore, after the second pass of bubble sort, the list becomes: 20, 31, 15, 56, 89.