Sorting Algorithms
Sorting Algorithms
www.pmt.education
Specification:
● Standard algorithms
○ Bubble sort
○ Insertion sort
○ Merge sort
○ Quick sort
www.pmt.education
Sorting Algorithms
Sorting algorithms are designed to take a number of elements in any order and to output
them in a logical order. This is usually numerical or alphabetical.
Most sorting algorithms will output elements in ascending order, but the algorithms can
typically be slightly altered (for example, by switching an inequality from less than to
greater than) or their output simply reversed in order to produce an output in descending
order.
Bubble Sort
Bubble sort makes comparisons and swaps between pairs of elements. The largest
element in the unsorted part of the input is said to “bubble” to the top of the data with each
iteration of the algorithm.
The algorithm starts at the first element in an array and compares it to the second. If they
are in the wrong order, the algorithm swaps the pair. Otherwise, the algorithm moves on.
The process is then repeated for every adjacent pair of elements in the array, until the end
of the array is reached (at which point the largest element is in the last position of the
array).
This is referred to as one pass of the algorithm. For an array with n elements, the
algorithm will perform n passes through the data, at which point the input is sorted and can
be returned.
A = A
rray of data
for i = 0 to A.length - 1:
for j = 0 to A.length - 2:
if A[j] > A[j+1]:
Swap A[j] and A[j+1]
return A
Example
Use bubble sort to arrange the elements in the array into ascending order.
4 9 1 6 7
The two first elements are compared. They are in the correct order and so the algorithm
moves on.
www.pmt.education
4 9 1 6 7
4 1 9 6 7
The second two elements are in the wrong order, and so they are swapped.
4 1 9 6 7
4 1 6 9 7
Again, the next two elements are in the wrong order, and so they are swapped.
4 1 6 9 7
4 1 6 7 9
The last two elements are also in the wrong order, so are swapped.
This marks the end of the first pass of the algorithm.
The second pass starts at the beginning of the array, and compares the first two elements.
They are in the wrong order, so are swapped.
4 1 6 7 9
1 4 6 7 9
The next two elements are in the correct order, so the algorithm moves on.
1 4 6 7 9
Again the comparison reveals that the elements are in the correct order.
www.pmt.education
1 4 6 7 9
1 4 6 7 9
This is the end of the second pass, and we can see that the data is sorted. However, the
algorithm wouldn’t terminate here. It would carry out another two passes before
terminating.
You might be thinking that this sounds like a waste of time, and you’d be right. If the
algorithm goes on until it terminates, it could possibly make many pointless comparisons.
The bubble sort algorithm can be improved to solve this issue, as the pseudocode below
shows.
A = A
rray of data
for i = 0 to A.length - 1:
noSwap = True
for j = 0 to A.length - 1:
if A[j] > A[j+1]:
swap A[j] and A[j+1]
noSwap = False
if noSwap:
break
return A
The most important modification is the introduction of a flag recording whether a swap has
occurred. If a full pass is made without any swaps, then the algorithm terminates earlier
than it would have done originally.
Make sure you aren’t caught out when using this version of the algorithm though. The
algorithm terminates after a complete pass in which no swaps are completed, not at the
end of the first pass at the end of which the elements are sorted!
www.pmt.education
Insertion Sort
Another sorting algorithm, insertion sort places elements into a sorted sequence. In the i
th
iteration of the algorithm the first ielements of the array are sorted. Be careful though,
although the ielements are sorted, they are not the ismallest elements in the input!
For example, the first three elements in the sequence 3, 7, 9, 4, 8 are sorted, but are not
the three smallest elements in the input.
Insertion sort stars at the second element in the input, and compares it to the element to
its left. If the two elements are in the wrong order, the smaller element is placed in the
lowest position. The third element in the input is then selected. It is inserted into the correct
position in the sorted portion of the input to its left. This continues until the last element is
inserted into the correct position, resulting in a fully sorted array.
A = A
rray of data
for i = 1 to A.length - 1:
elem = A[i]
j = i - 1
while j > 0 and A[j] > elem:
A[j+1] = A[j]
j = j - 1
A[j+1] = elem
The pseudocode for insertion sort, shown above, shows how the algorithm starts at the
second item and places it into a sorted sequence by performing consecutive swaps (within
the while loop) until every item has been inserted, leaving behind a sorted array. Insertion
sort is, like bubble sort, a slow algorithm.
Example
The algorithm can ignore the first element, as a single element is always a sorted
sequence. Starting at the second element, we see it is greater than the element to its left
and so it remains in place.
4 9 1 6 7
Moving to the third element, which is smaller than the element to its left, the algorithm has
to move it into the right place in the sorted sequence that is forming at the start of the data.
It moves the element one place to the left, but it is still smaller than the new element to its
left, so is moved left again at which point it is in the correct place in the sorted sequence.
www.pmt.education
4 9 1 6 7
4 1 9 6 7
1 4 9 6 7
The next element to be sorted is also smaller than the element to its left, so is moved left.
1 4 9 6 7
1 4 6 9 7
The same occurs with the next element, which is also moved left into the correct place.
1 4 6 9 7
1 4 6 7 9
Finally, the last element is checked, and is already in the right place.
1 4 6 7 9
1 4 6 7 9
www.pmt.education
Merge Sort
Merge sort is an example of a class of algorithms known as “divide and conquer” for
reasons which will soon become clear.
Merge sort is formed from two functions. One called MergeSortand another called
Merge . MergeSortdivides its input into two parts and recursively calls MergeSorton
each of those two parts until they are of length 1 at which point Mergeis called. Merge
puts groups of elements back together, ensuring that the final group produced is sorted.
MergeSort is a more efficient and faster algorithm than bubble sort and insertion sort.
Example
Firstly, the input is halved until individual elements are isolated. At this point there are four
sorted lists (remember from insertion sort that a single element counts as being sorted).
7 4 2 6
7 4 2 6
7 4 2 6
Each of the four lists must now be merged back together. This is done by inspecting the
first element of two lists and placing the lowest in a new list. This is repeated until both lists
are empty.
The first two lists are 7 and 4. The first elements of each (the whole list, as these only have
length 1) are compared and the smallest, 4, inserted into a new list.
7 4
Now only 7 remains, and so it is placed behind the 4 in the new list.
4 7
www.pmt.education
The same is applied to the two lists with single elements 2 and 6, resulting in a single list
containing the elements 2 and 6 in that order.
Now the two two-element lists are merged. The first elements in each list are compared
and the lowest, 2, placed into a new four-element list before being removed from the
shorter list.
4 7 2 6
The process is now repeated, and the first two remaining elements of each list (now 4 and
6) are compared. The lowest, 4, is then added to the new list and removed from the
shorter list.
4 7 6
2 4
7 6
2 4 6
Now there’s only one element remaining, 7, which is added to the back of the new list.
2 4 6 7
2 4 6 7
www.pmt.education
Quick Sort
Quick sort works by selecting an element, often the central element (called a pivot), and
dividing the input around it. Elements smaller than the pivot are placed in a list to the left of
the pivot and others are placed in a list to the right.
This process is then repeated recursively on each new list until all elements in the input
are old pivots themselves or form a list of length 1. Despite the name, quick sort isn’t
particularly fast, in fact it runs in the same time as bubble sort and insertion sort.
Example
In this example, the pivot is chosen to be the central element. The first pivot is 6.
Comparing elements from left to right, elements larger than the pivot are placed in a list to
the right and elements smaller are placed in a list to the left.
2 8 9 6 3 7 1
6 is now an old pivot, and is in the correct position, so is shaded. There are now two lists -
both of length 3. A new pivot is selected for each of these lists and the elements
partitioned around the pivot as before.
2 3 1 6 8 9 7
3,6 and 9 are now old pivots, so are shaded. Two lists now remain, both of length two.
New pivots are chosen (we round up to find the pivot if no middle element exists) and the
lists are partitioned around the pivots.
1 2 3 6 8 7 9
Again the old pivots are shaded. Note that once a pivot has been shaded, it never moves.
This is because an old pivot is always in the correct position, even if the data isn’t yet
sorted.
1 2 3 6 7 8 9
We’ve now reached the stopping condition. All elements are lists of length 1 or old pivots.
The algorithm terminates and the sorted data can be returned.
1 2 3 6 7 8 9
www.pmt.education
www.pmt.education