Selection SortTopSorting

Sorting

We'll now look at sorting, since we will need to be able to sort an array to use binary search. As we will see, sorting takes a fair amount of time, but if we are going to be searching a large array a lot, the savings obtained by using binary search over linear will more than make up for the cost of sorting the array once.

Suppose our goal is to take a shuffled deck of cards and to sort it in ascending order. We'll ignore suits, so there is a four-way tie at each rank.

Describing a sorting algorithm precisely can be difficult. Let's consider a few.

  1. selection sort
  2. insertion sort
  3. merge sort

Selection SortTopSorting