Download Data Structures: Sorting and Searching and more Summaries Data Structures and Algorithms in PDF only on Docsity!
Data Structures
Chapter# 05
Sorting and Searching
Introduction to Sorting
- (^) The process of arranging data in a specified order according to a given criteria is called sorting.
- (^) The numeric type data may be arranged either in ascending or descending order.
- (^) Similarly character type data may be arranged in alphabetical order.
- (^) Sorting is a fundamental operation performed by the computer.
- (^) It is mostly used to arrange records in databases.
Selection Sort
- (^) Suppose an array ‘ABC’ with ‘n’ elements ABC[0], ABC[1].... ABC[N-1] is in memory.
- (^) The selection sort technique for sorting array ‘ABC’ is as follows:
- (^) Select the first element and compare it with the rest of the elements.
- (^) If the next compared element is less than the selected element then swap the selected element with compared element, otherwise compare the selected element with the next element in the array.
- (^) This mechanism will find the smallest element in the list and put it in the first position in the first phase.
- (^) In the second phase, it will find the second smallest element in the list and put it in the second position and so on.
- (^) Thus array ‘ABC’ is sorted after ‘n-1’ passes
Selection Sort Example: 30 49 26 13 55 36 So 13 is the smallest element and placed it in first position
Pass 1:
Pass 3: Pass 4:
Pass 5:
- (^) In selection sort, select first and compare it with ‘N-1’ elements, if found small value interchange it, otherwise compare it with the next element.
- (^) Thus elements are sorted after ‘N-1’ passes.
- (^) Here is as algorithm which demonstrates the selection sort techniques.
Insertion Sort:
- (^) The insertion sort works by inserting each item
into its proper place in the final list.
- (^) Consider an array ‘A’ with ‘n’ elements A[0], A[1],
A[2]..... A[n-1] is in the memory.
- (^) The insertion sort algorithm scans ‘A’ from A[0] to
A[n-1] inserting each element into its proper
position in the previously sorted sub array i.e.
Pass 1: A[0] by itself sorted. Pass 2: A[1] is inserted before or after A[0], so that A[0], A[1] are to be sorted. Pass 3: A[2] is inserted into its proper place in A[0], and A[1] before A[0], between A[0] and A[1], or after A[1], so that A[0], A[1], A[2] are to be sorted. Pass N: A[n-1] is inserted into its proper place in A[0], A[1], A[2]... A[n-1] so that they are to be sorted.
Pass 4: 25 48 57 37 12 92
Pass 5: 25 37 48 57 12 92
Pass 6: 12 25 37 48 57 92
Pay Attention to the Code Example
For Insertion Sort
Pass 1
Pass 2
Pass 3 Pass 4 (^) 17 15 15 17 15 17 35 37 49
Searching
- (^) Computer systems are often used to store large amounts of data from which individual records are retrieved according to some search criterion.
- (^) The process of finding a specific data item or record from a list is called Searching.
- (^) The efficient storage of data to facilitate fast searching is an important task.
- (^) All other operations like inserting, deletion, etc are dependent on this operation.
- (^) Different techniques are used to carry out search operations. The commonly used search methods are: - (^) Sequential Search (Linear Search) - (^) Binary Search
Sequential Search
- (^) The sequential(Linear) search is a simple and straightforward technique to search a specified item in an unordered list.
- (^) The specified value is searched in the list sequentially, i.e. starting from the first element to the last element in the list in a sequence.
- (^) When the required value is found, search operation stops.
- (^) The sequential search is a slow process.
- (^) It is used for small amounts of data.
- (^) This method is not recommended for large amount of data.