You are on page 1of 20

DESIGN AND ANALYSIS OF ALGORITHM

IPS – 2

NAME : HARSH AGARWAL


REGISTARTION NUMBER : 21BAI1481
1. Q2). Given n 2-dimensional points (x1,y1),(x2,y2),...,(xn,yn),
Design an al-gorithm that follows the ‘Divide-Conquer-Combine’
strategy to(a) arrange then−pointsin an increasing order of
thexcoordinates(b) arrange then−pointsin an increasing order of
theycoordinates(c) arrange then−pointsin decreasing order of the value
(x−coordinate+y−coordinate)/2) coordinates1
For each of the above algorithms, execute the algorithm with an appro-
priate code. Computet(n) for each of the algorithm.

LOGIC:
➢ Firstly we will be taking the points as an input.
➢ Now, we will make separate arrays for sorting according to
the x and y co-ordinates.
➢ Now we sort the array according to the x co-ordinate and
store in an array in ascending order.
➢ And now we will be sorting according to the y co-ordinate
and store in an array in ascending order.
➢ Now we will be creating array in which we will be soring
the value of ((x+y)/2) for each point in decreasing order

UNDERSTANDING OF PROBLEM:
➢ To sort the the array along x co-ordinates, y co-ordinates and also
the ((x+y/2) values for each point.

ALGORITHM:
➢ Sorting the points based on their x-coordinates using a Merge Sort
algorithm.
➢ Divide the sorted list of points into two halves.
➢ Recursively sorting the two halves based on their y-coordinates
using the same Merge Sort algorithm.
➢ Combining the two sorted halves by merging them into a single
sorted list based on their y-coordinates.
➢ Divide the sorted list of points into two halves again.
➢ Recursively sort the two halves based on the value (x+y)/2 using the
same Merge Sort algorithm.
➢ Combine the two sorted halves by merging them into a single
sorted list based on the value (x+y)/2.

PSEUDOCODE:
OUTPUT:

ILLUSTRTATION:
➢ Suppose we have 5 points (3, 2), (1, 4), (5, 1), (2, 6), (4, 7).
➢ We want to arrange these points in the following orders:
1. Increasing order of x-coordinates:
(1, 4), (2, 6), (3, 2), (4, 7), (5, 1)

2. Increasing order of y-coordinates:


(5, 1), (3, 2), (1, 4), (2, 6), (4, 7)

3. Decreasing order of the value (x-coordinate+y-


coordinate)/2) coordinates:
(4, 7), (2, 6), (1, 4), (3, 2), (5, 1)
2.Q7). In the merge-sort Alogrithm, an array is divided into two
subarrays when-ever the ‘divison’ is applied. Modify the merge-sort
algorithm in such away that every divison leads to three subarrays.
Write the code for the modified algorithm and compare the efficiency
of the modified algorithm with that of the original algorithm.

LOGIC:
➢ Dividing the given array into three subarrays instead of two as in
the original method.
➢ Recursively sorting the left and right subarrays using the modified
merge-sort algorithm that is we divided into 3 subarrays.
➢ Recursively sorting the middle subarray using the modified merge-
sort algorithm that is we divided into 3 subarrays.
➢ Merging the three sorted subarrays together and now comparing
the efficiency of the modified and the original one.

UNDERSTANDINF OF THE PROBLEM:


➢ We have take the array and have to modify the merge sort in such
a ways that its different form the original one. As in the original one
we used to divide the array into 2 parts but we will modify and
divide into 3 parts.

ALGORITHM:
➢ If the length of the array is less than or equal to 1, return the array.
➢ Divide the array into three approximately equal-sized subarrays to
modifiy.
➢ Recursively sorting each subarray using the same original merge
sort algorithm.
➢ Merge the sorted subarrays using a three-way merge algorithm
which is also the modified merge sort algorithm.
PSEUDOCODE:
ORIGINAL:
OUTPUT:
MODIFIED:
OUTPUT:

ILLUSTRATION:
➢ Suppose we are given with an array [23,67,87,45,60,32,77,98,76]
➢ Now using the modified merge sort algorithm we get as:
1. [23,67,87][45,60,32][77,98,76]
2. [23],[67],[87],[45],[60],[32],[77],[98],[76]
3. [23,67,87][32,45,60][76,77,98]
4. [23,32,45,60,67,76,77,87,98]
➢ Hence the array is sorted.
COMPARISION:
➢ Time complexity comparison: The time complexity of Orginal
merge sort algorithm is O(nLogn) with base 2 whereas in has a
time complexity of O(nLogn) with base 3 thus reducing the time
taken. Hence method 2 takes less time and is the better
approach.
3.Q8.) Modify the merge-sort algorithm in such a way that the left
subarray (got as a result of the first divison) is divided further till we
get a subarray of size 1 and the right-subarray (got as a result of the first
division) is not sub-jected to any division. For sorting the right
subarray, apply insertion-sort algorithm. Write the modified algorithm
A′ to sort the n-given numbers.

LOGIC:
➢ We are given with an array and we have to sort that array.
➢ Now we have to sort based on the following condition:
1. On first division, take the left subarray and sort that using
divison technique till we reach the array of size 1 and
then sort and merge them.
2. On first division, take the right subarray and sort that
using insertion sort. Now merge both of the array.

UNDERSTANDING OF THE PROBLEM:


➢ We have to modify the merge sort algorithm in such a way that
the left subarray should be sorted using the merge sort algorithm
and the right subarray should be sort using the insertion sort.

ALGORITHM:
➢ Take the array as an input. If the input array has only one
element then just simply return it.
➢ Divide the input array into two halves: left and right. If the size of
the array is odd, make the left subarray smaller by one element
than the right subarray.
➢ Recursively call step 2 on the left subarray until it has size 1.
➢ Apply insertion-sort algorithm on the right subarray to obtain a
sorted array.
➢ Merge the left and right subarrays using the merge-sort algorithm
to obtain the final sorted array.

PSEUDOCODE:
OUTPUT:

ILLUSTARTION:
➢ Suppose the given array is [54,10,15,19,20,23,12]
➢ Now we will dive the array into two parts.
➢ Left part will be sorted using the merge sort.
➢ Right part will be sorted using the insertion sort
1. [54,10,15,19][20,23,12]
2. [54,10,15,19] →Sort using merge sort
3. [54,10][15,19]
4. [54],[10],[15],[19]
5. [10,54][15,19]
6. [10,15,19,54]→Sorted
7. [20,23,12]→Sort using insertion sort
8. [20,23,12]
9. [20, , 23]
10. [12,20,23]→Sorted
11. Now final sorted array [10,12,15,19,20,23,54]

You might also like