C&DS Unit-VI (Searching and Sorting)
C&DS Unit-VI (Searching and Sorting)
C&DS Unit-VI (Searching and Sorting)
Searching
Searching refers to finding the location of a given element in a collection of elements.
The element for which the search is made is sometimes called as search key.
Searching Techniques:
We will now discuss two searching methods and analyze their performance they are
1) Linear Search
2) Binary Search
Algorithm:
Analysis:
• The fewest possible comparisons = 1, if the required item is the first item.
• The maximum comparisons = N when the required item is the last item in the list.
• Thus if the required item is in position I in the list, I comparisons are required.
• Hence the average no of comparisons done by sequential search is
= (1 + 2 + 3 + ---- + I + ---- + N)
N
= N (N + 1)
2*N
= N+1
2
Binary Search:
• The drawbacks of sequential search can be eliminated if it becomes possible to eliminate
large portions of the list from consideration in subsequent iterations.
• The binary search method just that, it halves the size of the list to search in each iteration.
• Binary Search requires sorted data.
Algorithm:
Input : Sorted data of size N, Target value T
Output : Position of T in the list = T
Begin
High = N
Low = 1
Found = false
While (Found is false and low <= High)
Mid = (low + high) / 2
If T == List[mid]
I = mid
Found = True
Else if T < List [mid]
High = mid –1
Else
Low = mid + 1
End.
Analysis:
In general, the binary search method requires no more than [log2N]+1 comparisons.
Note: Please refer Lab Exercise 19 for implementations of Linear and Binary Search
Sorting
Sorting refers to the operation of arranging data in some order, such as increasing or
decreasing with numerical data, or alphabetically (lexicographically) with character data.
A list having a single element is trivially sorted.
Selection Sort:
Selection sort is a simple sorting algorithm in which after each pass the smallest elements
positions are fixed from left to right (assuming ascending order sort). Selection sort is noted for
its simplicity, and also has performance advantages over more complicated algorithms in certain
situations.
Illustration:
64 25 12 22 11
11 25 12 22 64
After Pass 2: Find the second smallest element and fix it in second position.
11 12 25 22 64
After Pass 3:
11 12 22 25 64
After Pass 4:
11 12 22 25 64
11 12 22 25 64
Note:
• To sort a list of n elements, selection sort requires n-1 passes.
• It requires (n − 1) + (n − 2) + ... + 2 + 1 = n(n − 1) / 2 ≈ Θ(n2) comparisons
Note: Selection Sort is implemented as a part of Binary Search in Lab Exercise 19.
Bubble Sort:
Bubble sort is a simple sorting algorithm in which larger elements are pushed to the end
to the end of the list. The algorithm gets its name from the way larger elements "bubble" to the
end of the list.
Steps:
1. Move left right on the list, comparing each pair of adjacent items.
2. If the adjacent items are in proper order, then leave them as it is, if not swap them
3. The pass through the list is repeated until no swaps are needed, which indicates that the
list is sorted.
Illustration:
First Pass: Get the greatest element to the end of the list.
( 5 1 4 2 8 ) ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps them.
( 1 5 4 2 8 ) ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) ( 1 4 2 5 8 ), Now, since these elements are in order (8 > 5), do not swap them.
14258
Note: Bubble sort requires n-1 passes and the complexity is О(n²).
Insertion Sort:
Insertion sort is a simple sorting algorithm in which the sorted array (or list) is built one
entry at a time. The basic idea is to insert an element into a sorted list at appropriate position and
continuing the same for rest of elements of unsorted list.
The idea of insertion sort comes from our daily life experiences. For example, when you play
cards with your friends, you will insert the next card you pick into the sorted cards in your hand.
Illustration:
Pass 1: Insert 10 from the unsorted list into sorted list 32.
32 10 4 8 12 23
10 32 4 8 12 23
10 32 4 8 12 23
4 10 32 8 12 23
4 10 32 8 12 23
4 8 10 32 12 23
4 8 10 32 12 23
4 8 10 12 32 23
Pass 5: Insert 23 from unsorted list into sorted list 4, 8, 10, 12, 32
4 8 10 12 32 23
4 8 10 12 23 32
4 8 10 12 23 32
Note: Insertion sort need n-1 passes and the complexity is O(n2)
Quick Sort:
Quicksort is a well-known sorting algorithm. It works on the principle divide and conquer.
Typically, quicksort is significantly faster in practice than other algorithms, because its inner
loop can be efficiently implemented on most architecture, and in most real-world data.
Basic idea is randomly select an element called pivot, fix it at its position in the final
sorted list and apply the quick sort on two unsorted list before and after it.
Steps:
1. Pick an element, called a pivot, from the list.
2. Reorder the list so that all elements which are less than the pivot come before the pivot and
so that all elements greater than the pivot come after it (equal values can go either way).
After this partitioning, the pivot is in its final position. This is called the partition operation.
3. Recursively sort the sub-list of lesser elements and the sub-list of greater elements.
The base cases (terminating condition) of the recursion are lists of size zero or one, which are
always sorted.
Illustration:
73925
Select 7 as pivot. Now adjust the list such that all small elements than 7 are before 7 and greater
are placed after 7.
73925 start comparing from the last element, since 5 is less than 7 swap.
53927 Now compare from left and keep moving until a greater element is found.
53279 Now again from left, and we find that all elements before 7 are less than it.
This fixes the position of 7 and leaves 2 unsorted lists 5 3 2 and 9. Since list 9 is a singleton set
no need to sort it further.
53279 Start comparing from right. i.e. from 2 of list 5 3 2. Swap 5 and 2.
23579 Now from left, since all elements are less than 5, we can fix it.
Now apply the algorithm on the left out list 2 3. Select 2 as pivot.
23579 Compare from right end to find 3 is rightly placed after 2. Fix 2.
We are now left with the list “3” (rest all are fixed in proper positions). Since it has only one
element we need not sort it.
23579
Merge Sort
The basic idea behind the merge sort is to merge two sorted lists into a single sorted list.
Merging
In effect, we break the list into different lists of one element each and merge those lists
separately to get sorted 2 (or 1) element lists, again merge those get sorted 4 (or 3) element lists
and so on until we get a single sorted list.
Illustration:
85 24 63 45 17 31 96
85 24 63 45 17 31 96
24 85 45 63 17 31 96
We are left with 4 sorted lists; merge 2 pairs to get 2 sorted lists.
24 45 63 85 17 31 96
We now left with only 2 sorted lists, merge them to get the final sorted list.
17 24 31 45 63 85 96
Note: Please refer to Lab Exercise 21 (b) for Merge Sort implementation.