BCSL33 Assignment
BCSL33 Assignment
BCSL33 Assignment
solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define
how a work is to be executed upon in order to get the expected results.
1
Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping
the adjacent elements if they are in wrong order.
Example:
First Pass:
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements,
and swaps since 5 > 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8
> 5), algorithm does not swap them.
Second Pass:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is
completed. The algorithm needs one whole pass without any swap to know it is
sorted.
Third Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Bubble Sort is a simple algorithm which is used to sort a given set of n elements provided in form of an
array with n number of elements. Bubble Sort compares all the element one by one and sort them
based on their values.
If the given array has to be sorted in ascending order, then bubble sort will start by comparing the first
element of the array with the second element, if the first element is greater than the second element, it
will swap both the elements, and then move on to compare the second and the third element, and so
on.
2
If we have total n elements, then we need to repeat this process for n-1 times.
It is known as bubble sort, because with every complete iteration the largest element in the given array,
bubbles up towards the last place or the highest index, just like a water bubble rises up to the water
surface.
Sorting takes place by stepping through all the elements one-by-one and comparing it with the adjacent
element and swapping them if required.
3. If the current element is less than the next element, move to the
next element. Repeat Step 1.
3
So as we can see in the representation above, after the first iteration, 6 is placed at the last index, which
is the correct position for it.
Similarly after the second iteration, 5 will be at the second last index, and so on.
4
5
6
Output: 2 3 6 10 12 34 89 99 105
Sum = n(n-1)/2
i.e O(n2)
Following are the Time and Space complexity for the Bubble Sort
algorithm.
Space Complexity: O(1)
Now that we have learned Bubble sort algorithm, you can check out
these sorting algorithms and their applications as well:
7
Q2.
Ans : Polynomial
A polynomial is an algebraic expression consisting of variables and coefficients and
operations like addition, subtraction, multiplication and non-negative exponentiation of
variables.
E.g., P(x) = x² − 5x + 9
An algorithm is a set of well-defined steps or rules that you need to follow to obtain a pre-
determined result. For instance, when we talk about algorithms in computer programming, we
8
already have our input and we know the expected output. Now, an algorithm would be all the
defined steps to follow on the input to get the desired output.
Therefore, the entire algorithm flowchart would have three major components:
solution is to one by one consider every term of first polynomial and multiply it with
every term of second polynomial. Following is algorithm of this simple method.
multiply(A[0..m-1], B[0..n01])
...(3.a) Traverse array B[] and do following for every element B[j]
4) Return prod[].
9
The following is the implementation of above algorithm.
10
11
Time complexity of the above solution is O(mn). If size of two polynomials
same, then time complexity is O(n 2).
Auxiliary Space: O(m + n)
Can we do better?
There are methods to do multiplication faster than O(n 2) time. These methods
are mainly based on divide and conquer. Following is one simple method that
divides the given polynomial (of degree n) into two polynomials one containing
lower degree terms(lower than n/2) and other containing higher degree terms
(higher than or equal to n/2)
Let the two given polynomials be A and B.
For simplicity, Let us assume that the given two polynomials are of
same degree and have degree in powers of 2, i.e., n = 2 i
12
The polynomial 'A' can be written as A0 + A1*x n/2
The polynomial 'B' can be written as B0 + B1*x n/2
13
Hence, the latter expression has only three multiplications.
So the time taken by this algorithm is T(n) = 3T(n/2) + O(n)
The solution of above recurrence is O(n Lg3) which is better than O(n 2)
14