Week 6 Flashcards
are fundamental algorithms
Searching data and sorting through data
involves going through elements in a data structure to find specific data,
Searching
involves arranging elements in a particular order.
while sorting
is the task of looking for a specific element inside a data structure.
As mentioned, searching
works by going through each element of the array one index after another sequentially.
A linear search
Sequentially checks each element in a list until the target value is found or the list ends.
Linear Search Code
Is a searching algorithm that works on sorted data. Unlike the linear search algorithm, in which every element of the array is checked,
Binary Search
can check the middle value to see whether the desired value is greater or smaller than it. If the desired value is smaller, this algorithm can search through the smaller parts, or it can search through the bigger parts if the desired value is bigger
binary searches
is one of the most important topics in computer science; it is faster and easier to locate items in a sorted array than in an unsorted sorted array.
Sorting
is the simplest sorting algorithm. It simply iterates over the entire array and swaps elements if one is bigger than the other.
Bubble sorting
is the worst type of sort because it compares every pair possible, whereas other sorting algorithms take advantage of the presorted parts of the array. Because bubble sort uses nested loops, it has a time complexity of O(n2).
Bubble sort
works by scanning the elements for the smallest element and inserting it into the current position of the array. This algorithm is marginally better than bubble sort.
Selection sorting
works similarly to selection sort by searching the array sequentially and moving the unsorted items into a sorted sublist on the left side of the array.
Insertion sort
works by obtaining a pivot and partitioning the array around it (bigger elements on one side and smaller elements on the other side) until everything is sorted. The ideal pivot is the median of the array since it will partition the array evenly but getting the median of an unsorted array linear time to compute. Hence, a pivot is typically obtained by taking the median value of the first, middle, and last elements in the partition. This sort is a recursive one and uses the divide-and-conquer methodology to break the quadratic complexity barrier and get the time complexity down to O(nlog2(n)). However, with a pivot that partitions everything on one side, the time complexity is worse case: O(n2).
Quicksort
is a selection algorithm to find the kth smallest element in an unordered list. Quickselect uses the same approach as a quicksort algorithm. A pivot is chosen, and the array is partitioned. Instead of recursing both sides like quicksort, however, it recurses only the side for the element. This reduces the complexity from O(nlog2(n)) to O(n)
Quickselect