Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

BCSL33 Assignment

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

The word Algorithm means “a process or set of rules to be followed in calculations or other problem-

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.

Implementing Bubble Sort Algorithm

Following are the steps involved in bubble sort(for sorting a given


array in ascending order):

1. Starting with the first element(index = 0), compare the current


element with the next element of the array.

2. If the current element is greater than the next element of the


array, swap them.

3. If the current element is less than the next element, move to the
next element. Repeat Step 1.

Let's consider an array with values {5, 1, 6, 2, 4, 3}

Below, we have a pictorial representation of how bubble sort will sort


the given array.

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.

the code for bubble sort:

4
5
6
Output: 2 3 6 10 12 34 89 99 105

Complexity Analysis of Bubble Sort

In Bubble Sort, n-1 comparisons will be 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,

(n-1) + (n-2) + (n-3) + ..... + 3 + 2 + 1

Sum = n(n-1)/2

i.e O(n2)

Hence the time complexity of Bubble Sort is O(n2).

The main advantage of Bubble Sort is the simplicity of the algorithm.

The space complexity for Bubble Sort is O(1), because only a single


additional memory space is required i.e. for temp variable.

Also, the best case time complexity will be O(n), it is when the list is


already sorted.

Following are the Time and Space complexity for the Bubble Sort
algorithm.

 Worst Case Time Complexity [ Big-O ]: O(n2)

 Best Case Time Complexity [Big-omega]: O(n)

 Average Time Complexity [Big-theta]: O(n2)

 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 array is a collection of items stored at contiguous memory locations. The idea is to


store multiple items of the same type together. This makes it easier to calculate the
position of each element by simply adding an offset to a base value, i.e., the memory
location of the first element of the array (generally denoted by the name of the array).
The base value is index 0 and the difference between the two indexes is the offset.

Given two polynomials represented by two arrays, write a function that


multiplies given two polynomials. 

Input: A[] = {5, 0, 10, 6}


B[] = {1, 2, 4}
Output: prod[] = {5, 10, 30, 26, 52, 24}

The first input array represents "5 + 0x^1 + 10x^2 + 6x^3"


The second array represents "1 + 2x^1 + 4x^2"
And Output is "5 + 10x^1 + 30x^2 + 26x^3 + 52x^4 + 24x^5"

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:

1. Input: What we already know or the things we have to begin with.


2. Algorithm: A set of sequenced steps that we need to follow one by one.
3. Output: The expected results we need to achieve in the end.

Ploynomial Multiplication Algorithm

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])

1) Create a product array prod[] of size m+n-1.

2) Initialize all entries in prod[] as 0.

3) Traverse array A[] and do following for every element A[i]

...(3.a) Traverse array B[] and do following for every element B[j]

prod[i+j] = prod[i+j] + A[i] * 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

For example 1 + 10x + 6x 2 - 4x3 + 5x4 can be


written as (1 + 10x) + (6 - 4x + 5x 2)*x2

A * B = (A0 + A1*x n/2) * (B0 + B1*xn/2)


= A0*B0 + A0*B1*x n/2 + A1*B0*xn/2 + A1*B1*xn
= A0*B0 + (A0*B1 + A1*B0)x n/2 + A1*B1*xn
So the above divide and conquer approach requires 4 multiplications and O(n)
time to add all 4 results. Therefore the time complexity is T(n) = 4T(n/2) + O(n).
The solution of the recurrence is O(n 2) which is same as the above simple
solution.
The idea is to reduce number of multiplications to 3 and make the recurrence as
T(n) = 3T(n/2) + O(n) 
How to reduce number of multiplications? 
This requires a little trick similar to Strassen’s Matrix Multiplication.  We do
following 3 multiplications. 
X = (A0 + A1)*(B0 + B1) // First Multiplication
Y = A0B0 // Second
Z = A1B1 // Third

The missing middle term in above multiplication equation A0*B0 +


(A0*B1 +
A1*B0)xn/2 + A1*B1*xn can obtained using below.
A0B1 + A1B0 = X - Y - Z
In Depth Explanation 
Conventional polynomial multiplication uses 4 coefficient multiplications:
 
(ax + b)(cx + d) = acx 2 + (ad + bc)x + bd
However, notice the following relation:
 
(a + b)(c + d) = ad + bc + ac + bd
The rest of the two components are exactly the middle coefficient for product of
two polynomials. Therefore, the product can be computed as:
 
(ax + b)(cx + d) = acx 2 +
((a + b)(c + d) - ac - bd )x + bd

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

You might also like