Searching and Sorting 587
Time Complexity
It is known that the depth of the complete binary tree is (log2w). In worst case, the number of
comparisons in each step would be equal to the depth of the tree. The number of comparisons for
the above observation would be O (n log2n). Thus, the number of comparisons in this method
would be O (n log2 n).
SUMMARY
1) Searching is a technique of finding accurate location of an element in the given item list or set
of the elements of an array, list, or trees.
2) If the given element is present in the collected elements or array then the search process is said
to be successful. The search is said to be unsuccessful if the given element doesn't exist in the
array.
3) The search method in which an element to be searched, is checked in the entire data structure
in a sequential way from starting to end is called linear search.
4) Binary search is quicker than the linear search. However, it cannot be applied on unsorted
data structure. This is the precondition. Before applying binary search, the linear data structure
must be sorted in either ascending or descending order by using one of the sorted methods.
5) Sorting is a process in which records are arranged in ascending or descending order.
6) Exchange sort: The bubble sort is an example of exchange sort. In this method, repetitively
comparison among successive elements of an array or list is performed and appropriately
swapping of elements is done.
7) Insertion sort: In this type elements are compared and element are inserted at appropriate
place in the list according to the sorting order.
8) Quick sort: Partition exchange is also called as quick sort. It is based on partition of elements.
Hence, the method falls under divide and conquer technique. The main list of element is
divided into two sub-lists. The quick sort marks an element in a list called as pivot or key. All
the elements whose value is less than J are shifted towards the left and elements whose value
is grater than J to right of J. Now, the key element divides the main list in to two parts.
9) Tree sort: Inorder and Heap sort methods are elaborated under tree sorting. Inorder tree
sorting method involves arranging the elements as per rules. In this method, firstly the left
element of tree is chosen, then node and right node.
The rule for heap sort prescribes that each parent node should have bigger value than its child
node. The detail rule can be followed from the heap sort example as given above.
EXERCISES
A) Answer the following questions.
1) What is sorting and searching? How they are essential for data base applications.
2) Explain linear and binary search.
3) Differentiate between linear and binary search.
4) Explain types of sorting.
5) Distinguish between selection and exchange sort.
B) Select the appropriate option for each of the following questions.
1) The process of arranging records in an ordered manner is called
a) sorting b) searching
c) indexing d) none of the above