# 30. Sorting¶

Binary searches only work on lists that are in order. So how do programs get a list in order? How does a program sort a list of items when the user clicks a column heading, or otherwise needs something sorted?

There are several algorithms that do this. The two easiest algorithms for
sorting are the *selection sort* and the *insertion sort*. Other sorting
algorithms exist as well, such as the shell, merge, heap, and quick sorts.

The best way to get an idea on how these sorts work is to watch them. To see common sorting algorithms in action visit this excellent website:

http://www.sorting-algorithms.com

Each sort has advantages and disadvantages. Some sort a list quickly if the list is almost in order to begin with. Some sort a list quickly if the list is in a completely random order. Other lists sort fast, but take more memory. Understanding how sorts work is important in selecting the proper sort for your program.

## 30.1. Swapping Values¶

Before learning to sort, we need to learn how to swap values between two variables. This is a common operation in many sorting algorithms. Suppose a program has a list that looks like the following:

```
my_list = [15, 57, 14, 33, 72, 79, 26, 56, 42, 40]
```

The developer wants to swap positions 0 and 2, which contain the numbers 15 and 14 respectively. See Figure 18.1.

A first attempt at writing this code might look something like this:

```
my_list[0] = my_list[2]
my_list[2] = my_list[0]
```

See Figure 27.2 to get an idea on what would happen. This clearly does not
work. The first assignment `list[0] = list[2]`

causes the value 15 that exists
in position 0 to be overwritten with the 14 in position 2 and irretrievably
lost. The next line with `list[2] = list[0]`

just copies the 14 back to
cell 2 which already has a 14.

To fix this problem, swapping values in an array should be done in three steps. It is necessary to create a temporary variable to hold a value during the swap operation. See Figure 18.3. The code to do the swap looks like the following:

```
temp = my_list[0]
my_list[0] = my_list[2]
my_list[2] = temp
```

The first line copies the value of position 0 into the `temp`

variable. This
allows the code to write over position 0 with the value in position 2
without data being lost. The final line takes the old value of position 0,
currently held in the `temp`

variable, and places it in position 2.

## 30.2. Selection Sort¶

The selection by looking at element 0. Then code next scans the rest of the list from element 1 to n-1 to find the smallest number. The smallest number is swapped into element 0. The code then moves on to element 1, then 2, and so forth. Graphically, the sort looks like Figure 18.4.

The code for a selection sort involves two nested loops. The outside loop tracks the current position that the code wants to swap the smallest value into. The inside loop starts at the current location and scans to the right in search of the smallest value. When it finds the smallest value, the swap takes place.

```
1def selection_sort(my_list):
2 """ Sort a list using the selection sort """
3
4 # Loop through the entire array
5 for cur_pos in range(len(my_list)):
6 # Find the position that has the smallest number
7 # Start with the current position
8 min_pos = cur_pos
9
10 # Scan left to right (end of the list)
11 for scan_pos in range(cur_pos + 1, len(my_list)):
12
13 # Is this position smallest?
14 if my_list[scan_pos] < my_list[min_pos]:
15
16 # It is, mark this position as the smallest
17 min_pos = scan_pos
18
19 # Swap the two values
20 temp = my_list[min_pos]
21 my_list[min_pos] = my_list[cur_pos]
22 my_list[cur_pos] = temp
```

The outside loop will always run \(n\) times. The inside loop will run an average of \(\frac{n}{2}\) times per run of the outside loop. Therefore the inside loop will run a total of \(n \cdot \frac{n}{2}\) or \(\frac{n^2}{2}\) times.

This will be the case regardless if the list is in order or not. The loops’
efficiency may be improved by checking if `min_pos`

and `cur_pos`

are equal
before line 20. If those variables are equal, there is no need to do the
three lines of swap code.

In order to test the selection sort code above, the following code may be
used. The first function will print out the list. The next code will create
a list of random numbers, print it, sort it, and then print it again. On
line 5 the print statement right-aligns the numbers to make the column
of numbers easier to read. Formatting `print`

statements will be covered in
a later chapter.

```
1# Before this code, paste the selection sort and import random
2
3def print_list(my_list):
4 for item in my_list:
5 print("{:3}".format(item), end="")
6 print()
7
8# Create a list of random numbers
9my_list = []
10for i in range(10):
11 my_list.append(random.randrange(100))
12
13# Try out the sort
14print_list(my_list)
15selection_sort(my_list)
16print_list(my_list)
```

See an animation of the selection sort at:

http://www.sorting-algorithms.com/selection-sort

For a truly unique visualization of the selection sort, search YouTube for “selection sort dance” or use this link:

You also can trace through the code using Selection Sort on Python Tutor.

## 30.3. Insertion Sort¶

The insertion sort is similar to the selection sort in how the outer loop works. The insertion sort starts at the left side of the array and works to the right side. The difference is that the insertion sort does not select the smallest element and put it into place; the insertion sort selects the next element to the right of what was already sorted. Then it slides up each larger element until it gets to the correct location to insert. Graphically, it looks like Figure 18.5.

The insertion sort breaks the list into two sections, the “sorted” half and the “unsorted” half. In each round of the outside loop, the algorithm will grab the next unsorted element and insert it into the list.

In the code below, the `key_pos`

marks the boundary between the sorted and
unsorted portions of the list. The algorithm scans to the left of `key_pos`

using the variable `scan_pos`

. Note that in the insertion sort, `scan_pos`

goes down to the left, rather than up to the right. Each cell location
that is larger than `key_value`

gets moved up (to the right) one location.

When the loop finds a location smaller than `key_value`

, it stops and
puts `key_value`

to the left of it.

The outside loop with an insertion sort will run \(n\) times. For each run of the outside loop, the inside loop will run an average of \(\frac{n}{4}\) times if the loop is randomly shuffled. In total, the inside loop would run \(n\cdot\frac{n}{4}\) times, or simplified, \(\frac{n^2}{4}\) times.

What’s really important: If
the loop is close to a sorted loop already, then the inside loop does
not run very much, and the sort time is closer to *n*. The insertion sort
is the fastest sort for nearly-sorted lists. If the list is reversed, then
the insertion sort is terrible.

The selection sort doesn’t really care what order the list is in to begin with. It performs the same regardless.

```
1def insertion_sort(my_list):
2 """ Sort a list using the insertion sort """
3
4 # Start at the second element (pos 1).
5 # Use this element to insert into the
6 # list.
7 for key_pos in range(1, len(my_list)):
8
9 # Get the value of the element to insert
10 key_value = my_list[key_pos]
11
12 # Scan from right to the left (start of list)
13 scan_pos = key_pos - 1
14
15 # Loop each element, moving them up until
16 # we reach the position the
17 while (scan_pos >= 0) and (my_list[scan_pos] > key_value):
18 my_list[scan_pos + 1] = my_list[scan_pos]
19 scan_pos = scan_pos - 1
20
21 # Everything's been moved out of the way, insert
22 # the key into the correct location
23 my_list[scan_pos + 1] = key_value
```

See an animation of the insertion sort at:

http://www.sorting-algorithms.com/insertion-sort

For another dance interpretation, search YouTube for “insertion sort dance” or use this link:

You can trace through the code using Insertion Sort on Python Tutor.

## 30.4. Full Sorting Example¶

```
1import random
2
3
4def selection_sort(my_list):
5 """ Sort a list using the selection sort """
6
7 # Loop through the entire array
8 for cur_pos in range(len(my_list)):
9 # Find the position that has the smallest number
10 # Start with the current position
11 min_pos = cur_pos
12
13 # Scan left to right (end of the list)
14 for scan_pos in range(cur_pos + 1, len(my_list)):
15
16 # Is this position smallest?
17 if my_list[scan_pos] < my_list[min_pos]:
18 # It is, mark this position as the smallest
19 min_pos = scan_pos
20
21 # Swap the two values
22 temp = my_list[min_pos]
23 my_list[min_pos] = my_list[cur_pos]
24 my_list[cur_pos] = temp
25
26
27def insertion_sort(my_list):
28 """ Sort a list using the insertion sort """
29
30 # Start at the second element (pos 1).
31 # Use this element to insert into the
32 # list.
33 for key_pos in range(1, len(my_list)):
34
35 # Get the value of the element to insert
36 key_value = my_list[key_pos]
37
38 # Scan from right to the left (start of list)
39 scan_pos = key_pos - 1
40
41 # Loop each element, moving them up until
42 # we reach the position the
43 while (scan_pos >= 0) and (my_list[scan_pos] > key_value):
44 my_list[scan_pos + 1] = my_list[scan_pos]
45 scan_pos = scan_pos - 1
46
47 # Everything's been moved out of the way, insert
48 # the key into the correct location
49 my_list[scan_pos + 1] = key_value
50
51
52# This will point out a list
53# For more information on the print formatting {:3}
54# see the chapter on print formatting.
55def print_list(my_list):
56 for item in my_list:
57 print(f"{item:3}", end="")
58 print()
59
60
61def main():
62 # Create two lists of the same random numbers
63 list_for_selection_sort = []
64 list_for_insertion_sort = []
65 list_size = 10
66 for i in range(list_size):
67 new_number = random.randrange(100)
68 list_for_selection_sort.append(new_number)
69 list_for_insertion_sort.append(new_number)
70
71 # Print the original list
72 print("Original List")
73 print_list(list_for_selection_sort)
74
75 # Use the selection sort and print the result
76 print("Selection Sort")
77 selection_sort(list_for_selection_sort)
78 print_list(list_for_selection_sort)
79
80 # Use the insertion sort and print the result
81 print("Insertion Sort")
82 insertion_sort(list_for_insertion_sort)
83 print_list(list_for_insertion_sort)
84
85
86main()
```