0% found this document useful (0 votes)
18 views

Bds Notes For Serching Sorting

The document discusses different sorting algorithms like linear search, binary search, bubble sort, insertion sort and their time complexities. It also talks about different categories of sorting algorithms like sorting by comparison, distribution, enumeration and examples of algorithms that fall under each category.

Uploaded by

sparenilesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Bds Notes For Serching Sorting

The document discusses different sorting algorithms like linear search, binary search, bubble sort, insertion sort and their time complexities. It also talks about different categories of sorting algorithms like sorting by comparison, distribution, enumeration and examples of algorithms that fall under each category.

Uploaded by

sparenilesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1

Linear search : Linear search, also called as sequential search, is a very simple
method used for searching an array for a particular value.

Complexity of Linear Search Algorithm


Linear search executes in O(n) time where n is the number of elements in the
array.
 best case of linear search is when VAL is equal to the first element of the
array.
 worst case will happen when either VAL is not present in the array or it is
equal to the last element of the array.
 the performance of the linear search algorithm can be improved by using
a sorted array.

Binary Search : Binary search is a searching algorithm that works efficiently


with a sorted list. The mechanism of binary search can be better understood by
 an analogy of a telephone directory.
 another analogy. How do we find words in a dictionary
2

Complexity of Binary Search Algorithm

The complexity of the algorithm is calculated depending on the number of


comparisons that are made. In the binary search algorithm, we see that with
each comparison, the size of the segment where search has to be made is
reduced to half.

Introduction to Sorting
Sorting means arranging the elements of an array so that they are placed in
some relevant order which may be either ascending or descending.

Bubble Sort :
In bubble sorting, consecutive adjacent pairs of elements in the array are
compared with each other. If the element at the lower index is greater than the
element at the higher index, the two elements are interchanged so that the
element is placed before the bigger one. This process will continue till the list of
unsorted elements exhausts.

Note : If the elements are to be sorted in descending order, then in first pass the
smallest element is moved to the highest index of the array.
3

Example code for bubble sort:

Complexity of Optimized Bubble Sort Algorithm :

In the best case, when the array is already sorted, the optimized bubble sort will
take O(n) time. In the worst case, when all the passes are performed, the
algorithm will perform slower than the original algorithm. In average case also,
the performance will see an improvement. Compare it with the complexity of
original bubble sort algorithm which takes O(n2) in all the cases.
4

Insertion sort
 The idea behind the insertion sort is that first take one element, iterate it through
the sorted array.
 Although it is simple to use, it is not appropriate for large data sets as the time
complexity of insertion sort in the average case and worst case is O(n2), where n
is the number of items.
 Insertion sort is less efficient than the other sorting algorithms like heap sort,
quick sort, merge sort, etc.

Complexity of Insertion Sort


For insertion sort, the best case occurs when the array is already sorted. In this case, the
running time of the algorithm has a linear running time (i.e., O(n)).
Similarly, the worst case of the insertion sort algorithm occurs when the array is sorted in the
reverse order.
Therefore, in the worst case, insertion sort has a quadratic running time (i.e., O(n2)).
The average case also has a quadratic running time.
5
6
7
8

Sorting by comparison – Insertion:


From a given list of items, one item is considered at a time. The item chosen is then inserted
into an appropriate position relative to the previously sorted items. The item can be inserted
into the same list or to a different list.
e.g.: Insertion sort
Sorting by comparison – Selection:
First the smallest (or largest) item is located and it is separated from the rest; then the next
smallest (or next largest) is selected and so on until all item are separated.
e.g.: Selection sort, Heap sort
Sorting by comparison – Exchange:
If two items are found to be out of order, they are interchanged. The process is repeated until
no more exchange is required.
e.g.: Bubble sort, Shell Sort, Quick Sort
Sorting by comparison – Enumeration:
Two or more input lists are merged into an output list and while merging the items, an input
list is chosen following the required sorting order.
e.g.: Merge sort

Sorting by Distribution
 No key comparison takes place

 All items under sorting are distributed over an auxiliary storage space based on the
constituent element in each and then grouped them together to get the sorted list.

 Distributions of items based on the following choices


9

 Radix - An item is placed in a space decided by the bases (or radix) of its components
with which it is composed of.

 Counting - Items are sorted based on their relative counts.

 Hashing - Items are hashed, that is, dispersed into a list based on a hash function.

You might also like