Comparing efficiency
- Bubble sort’s efficiency is О(n^2)
- Merge sort’s efficiency is O(n log n)
O(n log n)
means that it runs in what is called Linearithmic time. You can see from the graph below that, although the number of operations does increase as the size of the list to be sorted increases, the rate of increase is significantly smaller than for quadratic time.
Case Study: Timsort
Python has built a built in function for sorting lists. This example program uses the Pythonsorted
function to create a sorted list from an unsorted list of randomly generated data.#!/bin/python3
import random
data = []
for x in range (0, 100):
data.append(random.randint(0, 100))
print(data)
print(sorted(data))
Sample output:[92, 79, 31, 28, 72, 94, 23, 40, 82, 79, 51, 19, 20, 86, 23, 64, 100, 8, 53, 13, 28, 5, 40, 48, 26, 95, 89, 88, 26, 58, 5, 17, 80, 83, 50, 32, 22, 53, 5, 9, 41, 23, 57, 42, 3, 66, 30, 38, 28, 1, 21, 86, 29, 30, 70, 2, 97, 56, 84, 20, 67, 61, 46, 33, 6, 91, 37, 48, 14, 95, 48, 53, 62, 56, 9, 60, 68, 36, 68, 50, 57, 13, 40, 4, 46, 25, 76, 58, 56, 17, 60, 24, 80, 81, 45, 92, 95, 62, 13, 99]
[1, 2, 3, 4, 5, 5, 5, 6, 8, 9, 9, 13, 13, 13, 14, 17, 17, 19, 20, 20, 21, 22, 23, 23, 23, 24, 25, 26, 26, 28, 28, 28, 29, 30, 30, 31, 32, 33, 36, 37, 38, 40, 40, 40, 41, 42, 45, 46, 46, 48, 48, 48, 50, 50, 51, 53, 53, 53, 56, 56, 56, 57, 57, 58, 58, 60, 60, 61, 62, 62, 64, 66, 67, 68, 68, 70, 72, 76, 79, 79, 80, 80, 81, 82, 83, 84, 86, 86, 88, 89, 91, 92, 92, 94, 95, 95, 95, 97, 99, 100]
For sorted
, Python uses an algorithm called Timsort, which was developed by Tim Peters based on techniques by Peter McIlroy. The algorithm is designed to work well on a variety of input data because it is used in many different situations. It uses a combination of merge sort and another algorithm called insertion sort, and adapts its choice of algorithm based on properties of the input data.Which sort is best?
You might be wondering why we, or students, should care about how a sort works it there’s already a function in Python (and other languages) for sorting. This is one area where we can make the distinction between simply programming and thinking more like a computer scientist.Often when programming, we know we need to sort something, but are not very concerned with how it’s done. All we need is a general-purpose “good enough” solution. However, as we’ve discovered with the merge and bubble sorts, different algorithms are better suited to different datasets and scenarios. Computer scientists concern themselves with selecting (or creating) the right algorithm to suit the data being processed.Challenge
Can you add a Timsort function to your earlier program comparing the completion times of algorithms? Share your code and findings in the comments.Programming 102: Think Like a Computer Scientist

Our purpose is to transform access to education.
We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.
We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.
Learn more about how FutureLearn is transforming access to education