PDS Project Report
PDS Project Report
PDS Project Report
Year 2020
Submitted to
Department of Electrical Engineering
National Institute of Technology Silchar
Submitted by:
GROUP NO. STUDENTS NAME SCHOLAR ID:
ASHISH RAJ 17-13-064
13 PRITESH ABHISHEK 17-13-063
MANSUR AHMED 17-13-053
LASKAR
PRASHANT LODHI 17-13-110
MENTOR’S NAME:
Prof. BADAL SONI
ASSISTANT PROFESSOR
COMPUTER SCIENCE DEPATEMENT
Now considering the condition that, data are not limited for a fixed range, then if there are less
amount of data (i.e., space is not a issue then merge sort would be better because it will sort in O(n log
n) with extra spaces.
Now, if the condition is that number of data is very large i.e., space will also create issue in this case
again we have to check data are nearly sorted or purely random. If the given data is completely
random then quick sort would be better as it will do the work in O (n log n) without any extra space.
Now if the some part of data are nearly sorted then insertion sort would be better one. Because for
sorted items it will not check till last data .
So here, let consider that one have around ten books and have to sort them alphabetically by hand.
There are various sorting algorithm which will fulfil one’s purpose.
Like one can use either Bubble sort, Selection Sort, Insertion sort, Merge or quick sort etc.
From the above quick and merge sort are the fastest due to linear time complexity i.e. (O(n log n)).
Well a possible suitable algorithm would be quicksort. But it has problems.
What if the books are already sorted and because of this reason time complexity is very high also
doing physically this could be difficult as it is prone to human error.
One will do a lot of swaps. Especially if the bookcase is tightly packed, this could make one’s life
miserable when books don't fit where they have to be placed.
Yes, answer will change if one have nearly one thousand books.
Now for this case one can use Quick sort algorithm because below to reasons:
• It is generally the fastest sorting algorithm in one can implement. It only swaps what needs
to be swapped and the Swaps are slow.
• It has the least O (log n) space complexity.
• Its efficient implementation can minimize the probability of requiring quadratic time.
• It also has been used in industry for many years so one can have faith in it.
One will not use Merge sort algorithm because of these two drawbacks:
• Has O(n) space complexity, which is worse than the other Quick sort.
• We have to write all your data into another array and back into your original one. Copying
is usually slower than comparing.
Certain sorts that perform well computationally just do not work in every day life. If we use
Quicksort and heapsort, as they just don't make sense if we consider the addressing difficulties.
The O(N^2) sorts that are often dismissed actually do work rather well, since the N is relatively small
most of the time when sorting book, and the act of linearly searching through the stack of books,
which most of this class of algorithm depends on, takes about the same time as specifically addressing
a sub stack.
Bucket sorts actually works extremely well, since we can still relatively quickly address the 26
English alphabets.
There definitely are some recursive sorts that can still work well when sorting manually. Merge sort
only depends on comparison between two objects at a time, and merging into a single pile, so it works
exceptionally well. However, it requires a large amount of space if one follow the algorithm
rigorously since it copies the data into different subarray.
Finally, a combination of sorting algorithm often works better than just sticking to a single algorithm.
The implementation of sort is a combination of intro sort (which in itself is quicksort + heapsort) and
insertion sort. So in practice, one can found that what works well with large stacks (between 500-1000
books) is to first split the stack into K sub-stacks, where K is a power of two, and each sub-stack has
less than 100 set of books.
Then, bucket each sub-stack alphabetically. Most of the bucket sets should be small, so that selection
or insertion sort can speedily get each bucket in order. Once each sub-stack is sorted, start combining
pairs by implementing merge sort.
We know that quicksort divides the array in half each iteration on a pivot value or the partition. Then,
it compares the partition to each sub-array. It moves the values all the books left that comes before the
pivot/partition and the remaining books to the right side, building an ideally balanced binary tree with
the pivot/partition as the root node and don’t have to compare books on the left with books on the
right.
If the input data is already sorted, then Quicksort formed an unbalanced binary tree with the partition
having only one child or skew tree of height n. When all the nodes (partition) in a binary tree have
only one child, then it is really represents a linked list of array length n.
The effect is n partition through n array elements, or O(n2) O(n2) worst case performance.
ANS 2:
Selection sort is a simple and one of the basic sorting algorithms of array. We start sorting the data
from left side and move towards the right side of the data. We move the selected minimum data to left
side of the data(it’s assigned position) and make a loop for moving the remaining data one by one.
The smallest value is selected from the unsorted array and swapped with the its actual positioned
element, and that part is considered as sorted array part. This process continues until we reach a sorted
array.
This algorithm can’t be considered for large set of data as its average and worst-case complexities is
Ο(n2), where n is number of elements in an array.
EXAMPLE:-
13 32 26 9 34 18 41 43
For the first position in the sorted list, we find the minimum value of the array. We form a loop
and found 10 as the lowest value.
13 32 26 9 34 18 41 43
Hence, we swapped 14 with 10. And after one round 10 is considered as the part of the sorted
array.
For the second position, we again scan for next smallest number and replace it with the
second element of the array.
9 32 26 13 34 18 41 43
After one round, we found 14 as the second lowest element, so we have replace it with
second element that is 33.
9 32 26 13 34 18 41 43
Now, after two round two values are placed at there position and that part of array is called
sorted array.
9 13 26 32 34 18 41 43
Same process is repeated until we obtain a sorted list of array in ascending order.
Following are the process happening, as shown in figure.
9 13 26 32 34 18 41 43
9 13 26 32 34 18 41 43
9 13 18 32 34 26 41 43
9 13 18 32 34 26 41 43
9 13 18 26 34 32 41 43
9 13 18 26 34 32 41 43
9 13 18 26 32 34 41 43
9 13 18 26 32 34 41 43
Algorithm:
CODE:
#include <stdio.h>
#include <stdlib.h>
}
int main()
{
int A[]={13,32,26,9,34,18,41,43};
int l,k,t;
for(k=0;k<6;k++)
{
l=min (A,k,8);
t=A[k];
A[k]=A[l];
A[l]=t;
}
for(k=0;k<=7;k++)
printf("%d\t",A[k]);
getch();
}
Comparison Chart:
definition The data is sorted by inserting the The data is sorted by selecting a
smallest data from remaining data into minimum value in data and placing the
procedure Elements are known before the Position is previously known while
looked.
During loop Insertion sort is live sorting technique It cannot deal with data recently
which can deal with recently supplied supplied; it needs to be present from
Immediate data
data. the starting.
of sorting
Ans 3:
MERGE SORT
Merge sort algorithm was first introduced by John Von Neumann. The algorithm works on
divide and conquer policy. Merge sort algorithm is very much efficient for large arrays. It has an
efficient time complexity of O(nlog(n)) and a space complexity of O(n). Merge sort can be compared
to a large assignment to be done by a group of people. First, the work is divided among the employees
and then the individual work is merged to get the whole work.
The sorting method contains two steps. First, the array is divided into halves till individual
elements are separated from each other. In second step, all the individual elements are rejoined or
merged. The divide and merge process are done in recursive manner.
For disintegrating the array, three parameters are required- The array, LOWER index and
UPPER index of the array. Then the MIDDLE index of the array is found using the formula
(LOWER+UPPER)/2. If there are odd number of elements in array, rounding is done so that the left
portion contains more numbers of element than right part of the array. In next step, two arrays are
divided and their own UPPER and LOWER index are assigned. The process is repeated recursively to
fully disintegrate the array into single element arrays.
Elements are merged and sorted consecutively. Two arrays (which are separated from same
array previously) are merged. The UPPER index of new array is same as that of the right array. The
Step 2: END
Now let us take an array [32 54 9 35 21 91 41] to explain the procedure briefly. Here we
are going to sort the elements in ascending order.
Here total number of elements = 7,
Therefore, LOWER index = 0;
And UPPER index = 6;
So, index of MIDDLE element = (0+6)/2 = 3
As, there are 7 elements, the array is divided into two parts – left array has 4 elements and right array
has 3 elements.
After diving the arrays are shown below:
Now, we have two arrays, if we apply same method to both arrays as in earlier array, we get the below
arrays:
Again, merging is done on the arrays as done in above step, [32 54] & [9 35] array pair are merged
and [21 91] & [41] array pair are also merged. Finally, we get the result as shown below.
Again, merging the arrays as done in above step, both the arrays are merged and sorted in ascending
manner.
[9 21 32 35 41 54 91]
So, we have finally got the sorted array after dividing and merging the given array.
CODE:
Using C
#include <stdio.h>
#define max 10
int b[6]; //auxillary space
int merge(int lower, int middle, int higher, int A[], int B[])
for(index1 = lower, index2 = middle + 1, i = lower; index1 <= middle && index2 <= higher; i++) {
if(A[index1] <= B[index2])
b[i] = A[index1++];
else
b[i] = A[index2++];
}
int main() {
int I, a[] = { 9,21,32,35,41,54,91};
printf("given no.s before sorting\n");
Merge_sort(0, max);
1. In all cases merge sort has time 1. In Insertion Sort, the Worst Case and
complexity 𝑂(𝑛𝑙𝑜𝑔𝑛). average Case is 𝑂(𝑛2 ) and Best Case
is O(n)
3. Merge Sort is preferred for large data 3. Insertion Sort is preferred for small
sets. number of elements