Part-A: Searching: Searching Refers To The Operation of Finding Locations of A
Part-A: Searching: Searching Refers To The Operation of Finding Locations of A
Part-A: Searching: Searching Refers To The Operation of Finding Locations of A
Part-A
• Insertion Sort: In this sorting we can read the given elements from
1 to n, inserting each element into its proper position. This sorting
algorithm is frequently used when n is small. The insertion sort
algorithm scans A from A[l] to A[N], inserting each element A[K]
into its proper position in the previously sorted sub array A[l],
A[2], . . . , A[K-1]. Its complexity is of order O (n2).
• Selection Sort: In this sorting we find the smallest element in this
list and put it in the first position. Then find the second smallest
element in the list and put it in the second position. And so on. A
very simple algorithm, to code, and a very simple one to explain,
but a little slow. Note that you can do this using the smallest value,
and swapping it with the first unsorted element. The complexity of
this one is truly O (n2), where best case and worst case are the
same, because even if the list is sorted, the same number of
selections must still be performed.
• Merge Sort: Combing the two lists is called as merging. For
example A is a sorted list with r elements and B is a sorted list with
s elements. The operation that combines the elements of A and B
into a single sorted list C with n = r + s elements is called merging.
Its complexity is O (n log n). The disadvantage of using merge sort
is that it requires two arrays of the same size and type for the
merge phase.
• Radix Sort: Radix sort is the method that many people intuitively
use or alphabetizing a large list of names. Specifically, the list of
names is first sorted according to the begin to use when first letter
of each name. Radix sort is one of the linear sorting algorithms for
integers. It functions by sorting the input numbers on each digit, for
each of the digits in the numbers. However, the process adopted by
this sort method is somewhat counterintuitive, in the sense that the
numbers are sorted on the least-significant digit first, followed by
the second-least significant digit and so on till the most significant
digit. The drawback of radix sort is that one may need d*n memory
locations. This comes from the fact that all the items may be "sent
to the same pocket" during a given pass. This drawback may be
minimized by using linked lists rather than arrays to store the items
during a given pass. However, one will still require 2*n memory
locations. Its complexity is O (n).
• Quick Sort: This is the most widely used internal sorting algorithm.
It is based on divide-and-conquer type i.e. Divide the problem into
sub-problems, until solved sub problems are found. The Quick sort
algorithm uses the O (n log2 n) comparisons.
• Heap Sort: A heap is a complete binary tree, in which each node
satisfies the condition that the key of each node is greater than or
equal to the key in its children. Thus the root node will have the
largest key value. This utilizes, just about the only good use of
heaps that is finding the maximum element, in a max heap (or the
minimum of a min heap). Is in every way as good as the straight
selection sort, but faster. Its complexity is O (n log n).
• Bubble sort: In this sorting algorithm, multiple swapping take place
in one iteration. Smaller elements move or ‘bubble’ up to the top of
the list. In this method, we compare the adjacent members of the
list to be sorted, if the item on top is greater than the item
immediately below it, they are swapped. The Bubble sort algorithm
uses the O (n2) comparisons on average.
Ans:-
Linear search: it is the method of finding the value in the list consist
of checking every element of it, at a time till the desired is found or
the list ends.
Binary Search: it locates the position of an item in sorted list. Binary
search works by comparing an input value to the middle element of
the array. The comparison determines whether the element equals the
input, less than the input or greater. When the element being compared
to equals the input the search stops and typically returns the position
of the element. If the element is not equal to the input then a
comparison is made to determine whether the input is less than or
greater than the element. Depending on which it is the algorithm then
starts over but only searching the top or bottom. If the input is not
located within the array the algorithm will usually output a unique
value indicating this. Binary search algorithms typically halve the
number of items to check with each successive iteration, thus locating
the given item (or determining its absence) in logarithmic time.
Hashing: this algorithm does not depends on the number of elements.
It saves item in the key indexed table. It has no space limitation and
time limitation
Ans:-
Ans:-
Breadth-first search can be used to solve many problems in graph
theory, for example:
Part-B
5. If you have been given a task to do some project and to do the basic
operations like searching and sorting there are many algorithms,
which algorithm you will select in which circumstances and why,
discuss in detail.
Ans:- When the task of searching is assigned to me then I will first
look up at data to be searched if the data is in tabular form then I will
be using hashing and if the data is given in linear format then I will be
using linear or binary search depending upon the data. If the number
of data in the list is less then I will be using linear search and if the
number of data in the list is very large then I will be using binary
search. Further if the data in the list is in a sorted order then also I will
be using the binary search.
Suppose I have been assigned a task of sorting then I will look upon the
data if it is in nearly sorted order then I will be using Insertion sort or
bubble sort. Quick sort is complicated but effective sorting algorithm. It
cannot be applied because of its complications. Implementation of
selection sort is very simple, and a very simple one to explain, but a little
slow. Note that you can do this using the smallest value, and swapping it
with the first unsorted element. Heap sort utilizes, just about the only
good use of heaps that is finding the maximum element, in a max heap
(or the minimum of a min heap). Is in every way as good as the straight
selection sort, but faster. Radix sort is the god of sorting algorithms. It
will search the largest list, with the biggest numbers, and has a
guaranteed O (n) time complexity. And it aren't very complex to
understand or implement. This can be implemented everywhere.
6. Discuss in detail that how the selection of the data structure impacts
the quality of any searching or sorting algorithm.
Ans:-