Sorting and Searching

Sorting and Searching are two very important tasks used in many different computer applications. There are several technique for each that we will talk about in CS 171.

Searching

The searching problem involves finding if an element (called the key) exists in a list and the location of the element. This problem is simplified if the list is sorted.

Sequential Search

Sequential search starts at the beginning of the sorted list and compares each element for the key. The search ends when the key is located or the element in the list is larger than the key.

Binary Search

Binary search starts at the middle of a sorted list, comparing the element in the middle (say m) with the key. Comparing the key and the middle element will results in one of three possibilities:

  • key = m: The element was found!
  • key < m: The element was not found. If the list on the left of the middle exists, binary search the left sublist. Otherwise the key is not in the list.
  • key > m: The element was not found. If the list on the right of the middle exists, binary search the right sublist. Otherwise the key is not in the list.

Sorting

The sorting problem involves taking a list and sorting the elements, typically in nondecreasing order.

Insertion Sort

Proceed by inserting the elements, one at a time, into an increasingly larger sublist. For the ith element, find its location in the beginning of the list; shuffle all larger elements, creating a hole where the ith element can be placed.

Selection Sort

Proceed by selecting the smallest element in the unsorted list. Swap it with the smallest element in the unsorted list. Proceed until the entire list is sorted.

Merge-Sort

Merge-Sort uses a divide and conquer strategy. Divide list into two equal (+1 or -1) sublists. Repeat division until lists are of size 1, which is a sorted list! Now merge smaller lists back into larger lists by comparing leading elements. Merge lists into larger lists in the reverse order in whihc they were divided. The last merge results in a sorted list.

Other Sorting Algorithms

Explore the other sorting methods on you own. These are discussed in other courses such as CS 173 and CS 356.

References

  1. Sequential search from wikipedia.
  2. Binary search from wikipedia.
  3. Selection Sort from wikipedia.
  4. Insertion Sort from wikipedia.
  5. Merge sort from wikipedia.
  6. Dancing Sorts.
  7. Sorting Algorithm Animations.


Copyright © 2019, David A. Reimann. All rights reserved.