Professional Documents
Culture Documents
Searching & Sorting Complete
Searching & Sorting Complete
SLIDESMANIA.COM
Musarrat Ahmed
Linear Search
● When you’re at a restaurant and deciding what to have for lunch, you may be
looking around the menu chaotically until something catches your eye.
● Alternatively, you can take a more systematic approach by scanning the menu from
top to bottom and scrutinizing every item in a sequence.
● That’s linear/sequential search in a nutshell.
● The approach loops over a collection of elements in a predefined and consistent
order.
● It stops when the element is found, or when there are no more elements to check.
● This strategy guarantees that no element is visited more than once because you’re
SLIDESMANIA.COM
● The linear search algorithm may be a good choice for smaller datasets, because
it doesn’t require preprocessing the data but not when the dataset is large.
Case Analysis
● Worst Case
➔ When the element to be searched is either the last element of the
collection or it is not present in it.
tries.
● Binary search is based on dividing a collection of elements into two halves and
throwing away one of them at each step of the algorithm.
● This can dramatically reduce the number of comparisons required to find an
element.
● But there’s a catch —elements in the collection must be sorted first.
● In the page finding example, the page numbers that restrict the range of pages to
search through are known as the lower bound and the upper bound.
● You commonly start with the first page as the lower bound and the last page as
the upper bound. You must update both bounds as you go.
● Binary search is a great example of a divide-and-conquer technique, which
partitions one problem into a bunch of smaller problems of the same kind.
● Click here for a visualization of binary search -
Binary and Linear Search Visualization
SLIDESMANIA.COM
● Worst Case
➔ When the element to be searched is found in the end when the list is
narrowed down to a single item.
SLIDESMANIA.COM
● When we split the list enough times, we end up with a list that has just one
item. Either that is the item we are looking for or it is not. Either way, we
are done.
● The number of comparisons necessary to get to this point is i where
● Unlike other search algorithms, binary search can be used beyond just
searching.
● For example, it allows for set membership testing, finding the largest or
smallest value, finding the nearest neighbor of the target value, performing
range queries, and more.
● Even though a binary search is generally better than a sequential search, it is
important to note that for small values of n, the additional cost of sorting is
probably not worth it.
SLIDESMANIA.COM
SLIDESMANIA.COM
SLIDESMANIA.COM
Bubble Sort
● One of the most straightforward sorting algorithms.
● Its name comes from the way the algorithm works: With every new pass, the
largest element in the list “bubbles up” toward its correct position.
● Each iteration takes fewer steps than the previous iteration because a
continuously larger portion of the array is sorted.
● Bubble sort consists of making multiple passes through a list, comparing
elements one by one, and swapping adjacent items that are out of order.
SLIDESMANIA.COM
Click here for a visualization
of bubble sort -
https://www.youtube.com/watch?
v=nmhjrI-aW5o
SLIDESMANIA.COM
Pseudocode
SLIDESMANIA.COM
Analysis
● Regardless of how the items are arranged in the initial list, n−1 passes will be
made to sort a list of size n.
● It builds the sorted list one element at a time by comparing each item with
the rest of the list and inserting it into its correct position.
● This “insertion” procedure gives the algorithm its name.
● An excellent analogy to explain insertion sort is the way you would sort a
deck of cards.
● Imagine that you’re holding a group of cards in your hands, and you want to
arrange them in order.
● You’d start by comparing a single card step by step with the rest of the cards
until you find its correct position.
● At that point, you’d insert the card in the correct location and start over
with a new card, repeating until all the cards in your hand were sorted.
SLIDESMANIA.COM
● Insertion sort always
maintains a sorted sublist in
the lower positions of the
list.
● Each new item is then
“inserted” back into the
previous sublist such that the
sorted sublist is one item
larger.
SLIDESMANIA.COM
SLIDESMANIA.COM
Pseudocode
SLIDESMANIA.COM
Analysis
● Insertion sort scans backwards from the current key, while selection sort scans forwards.
● Advantage of insertion sort over selection sort is that selection sort must always scan all remaining
elements to find the smallest element in the unsorted portion of the list, while insertion sort
requires only a single comparison when the (k + 1)-st element is greater than the k-th element.
● Thus, if the input array is already sorted or partially sorted, insertion sort is distinctly more
efficient compared to selection sort.
● In the worst case for insertion sort (when the input array is reverse-sorted), it performs just as
many comparisons as selection sort.
● However, a disadvantage of insertion sort over selection sort is that it requires more writes.
● On each iteration, inserting the (k + 1)-st element into the sorted portion of the array requires
many element swaps to shift all of the following elements, while only a single swap is required for
each iteration of selection sort.
● Selection sort may be preferable in cases where writing to memory is significantly more expensive
than reading, such as with EEPROM or flash memory.
SLIDESMANIA.COM
SLIDESMANIA.COM
Fin.
SLIDESMANIA.COM