Chapter 5 searching and sorting
Chapter 5 searching and sorting
Introduction:
● searching is an operation which finds the place of a given element in the
list/array.
every element within the input array is traversed and compared with the key
Step 1: start
Step 2: set i to 0
step 6: go to step 3
step 9: exit
Binary Search:
● Binary search is a search algorithm used to find the position of a target
value within a sorted array.
● It works by repeatedly dividing the search interval in half until the target
value is found or the interval is empty.
● The search interval is halved by comparing the target element with the
middle value of the search space.
Algorithm for Binary Search:
Step 1: Initialize low = 0, high=n-1
step 2: while low < = high
step 3: mid = low + high /2
step 4: if a[mid] = = item
step 5: set pos = mid
step 6: break and jump to step 10
step 7: else if item < a[mid]
step 8: high = mid - 1
step 9: else low = mid + 1
step 10: if pos < 0
step 11: print “ element is not found”
step 12: else print pos
Interpolation Search:
● Interpolation Search algorithm is used to search for a value in an ordered,
uniformly distributed array of elements.
● Interpolation Search will go to different locations to start with, depending if
the searched value is closer to the end or the start of the array, unlike Binary
Search that is always looking for the middle.
● This technique is trying to find the exact location of the value, not the
middle, using the interpolation formula.
4. Divide the list using probing formula and find the new middle.
Sorting Concepts:
Example: 30 36 50 49 22
Ascending order: 22 30 36 49 50
Descending order: 50 49 36 30 22
Sorting Techniques with Time complexity:
🞆 Sorting is a technique to rearrange the elements in ascending or
descending order, which can be numerical or any user defined
order.
🞆 Example: consider a telephone directory which consists of four
fields; phone number, name, address, pin code.
🞆 The sorting algorithm are divided into two categories:
⚫ Internal sorts: The method uses only the primary memory
during process. if all the data that is to be sorted can be
accommodated at a time in memory is called internal sorting.
⚫ Example: Bubble sort, Insertion sort, Quick sort.
excluding pivot
7. If both step 5 and step 6 does not match swap left and right