Which of the following are examples of algorithms? choose all that apply.

There are certain algorithms that come up again and again. In this tutorial, we will explore three of the most common: searching, sorting, and adding to/removing from a linked list. The ideas surrounding these algorithm examples permeate throughout many other algorithms . Understanding these three examples, will help us build a solid foundation so we can tackle future algorithm problems with confidence!

Algorithm Examples, #1: Binary Search

Binary search is an essential search algorithm that takes in a sorted array and returns the index of the value we are searching for. We do this with the following steps:

  1. Find the midpoint of the sorted array.
  2. Compare the midpoint to the value of interest.
  3. If the midpoint is larger than the value, perform binary search on right half of the array.
  4. If the midpoint is smaller than the value, perform binary search on left half of the array.
  5. Repeat these steps until the midpoint value is equal to the value of interest or we know the value is not in the array.

From the steps above, it is clear that our solution can be recursive. We will pass in a smaller array to our method on each iteration until our array only contains the value we are interested in. The tricky parts are indexing our array properly and keeping track of our index offset on each iteration so that we can return the index of our value from the original array. See below for our version of the binary search algorithm.

def binary_search[arr, value, offset=0]
  mid =  [arr.length] / 2
  
  if value < arr[mid] binary_search[arr[0...mid], value, offset] elsif value > arr[mid]
     binary_search[arr[[mid + 1]..-1], value, offset + mid + 1]
  
  else
     return offset + mid
  end

end

Binary search has a time complexity of O[logn]. We know this because if we double the size of our input array, we only need one more iteration of our algorithm to arrive at our final answer. This is why binary search is such a significant algorithm in computer science.

Algorithm Examples, #2: Merge Sort

Merge sort,uses a similar “divide and conquer” methodology to efficiently sort arrays. See the following steps for how merge sort is implemented.

  1. Return if array is only one element long, because it is already sorted.
  2. Divide array into two halves until it cannot be divided anymore.

  1. Merge smaller arrays in sorted order until we have our original sorted array.

To implement merge sort, we will define two methods. One will take care of the splitting up of the array and the other will take care of merging two unsorted arrays back into a single sorted array. We call the dividing-up-method [merge_sort] recursively until our array is only one element long. We then merge them back together and finally return our sorted array. See below:

def merge_sort[array]
  return array if array.length == 1

  mid_point = array.length / 2
  left = array[0...mid_point]
  right = array[mid_point..-1]

  merge[merge_sort[left], merge_sort[right]]

end
def merge[left, right]
    merged_arr = []

    until left.empty? && right.empty?
      if left.length == 0
        merged_arr 

Chủ Đề