Bds Notes For Serching Sorting
Bds Notes For Serching Sorting
Linear search : Linear search, also called as sequential search, is a very simple
method used for searching an array for a particular value.
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
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.
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.
Radix - An item is placed in a space decided by the bases (or radix) of its components
with which it is composed of.
Hashing - Items are hashed, that is, dispersed into a list based on a hash function.