Explore how the number of steps in a sorting algorithm varies with the number of items to be sorted

Consider a simple sorting algorithm that says

  1. Start from the beginning of a set
  2. Compare first and second item
  3. Swap if first is larger than second
  4. Compare next two items, etc. until end of set is reached.
  5. Then repeat the process but stop one short of the end
  6. Repeat until finished

If you count each compare and each swap as an operation, how many operations are necessary if the list contains 4, 5, 6, 7 items.

27 May 2018 19:37