What Big O measures
Timing a program tells you about the computer, the language and whatever else was running, not about the algorithm. So instead we count how many operations it performs as a function of the input size , and look at how that number grows as gets large. Big O describes that growth as an upper bound, and in the exam it means the worst case unless you are told otherwise. Constants and smaller terms are dropped: is .
Five classes cover almost everything: for indexing a list or pushing onto a stack, for binary search, for linear search or summing a list, for efficient sorts such as merge sort, and for bubble sort, selection sort and most nested loops. If goes from 1000 to 1,000,000, an algorithm takes 1000 times longer, an one a million times longer, and an one goes from about 10 steps to about 20.
Space complexity is the same idea for the extra memory used, not counting the input. Sorting in place by swapping is in space; building a copy of the list is .
Reading complexity from code
A fixed number of statements is , even if the fixed number is 10. A loop that runs times with an body is . Nested loops multiply, so two loops over are ; that stays true when the inner loop gets shorter each time, because is still of order . A loop that halves the problem each time (n = n // 2) runs about times, so it is . Consecutive blocks add, and the largest one wins: an loop followed by an one is .
The justification is what earns the second mark: “there are two nested loops that each go through the list, so the comparison runs of the order of times, which is ”.
Linear and binary search
Linear search checks each element in turn until it finds the target or reaches the end. It works on any list, sorted or not. The best case is the target in first place, ; the worst is the target last or missing, .
Binary search needs a sorted list. It keeps a range from low to high, compares the target with the middle element v[mid], where mid = (low + high) // 2, and throws away the half that cannot contain it: low = mid + 1 if the middle value is too small, high = mid - 1 if it is too big. When low > high the range is empty and the target is not there. Each comparison halves the range, so the worst case is about comparisons: at most 10 for 1000 elements and 20 for a million.
The usual bugs are low = mid (an infinite loop), while low < high (the last element is never checked), / instead of // (a decimal index) and running it on an unsorted list. For a trace, draw columns for low, high, mid, v[mid] and the outcome, one row per comparison, and show the final values when the target is missing.
Bubble sort and selection sort
Bubble sort compares each pair of adjacent elements and swaps them if they are in the wrong order. After the first pass the largest value has moved to the end, after the second the second largest, and passes are always enough. The worst case, a list in reverse order, needs comparisons, so it is . With a swapped flag the algorithm stops after a pass with no swaps, so an already sorted list takes one pass of comparisons: in the best case.
Selection sort finds the minimum of the unsorted part and swaps it into the first unsorted position, so after pass the first positions are final. It always makes comparisons, best case and worst case alike, so it is either way, but it makes at most swaps, which helps when writing to memory is expensive. Both sorts are in place and use extra space.
Count comparisons from the code you are given. If the inner loop does not get shorter each pass, a 5-element list takes 4 passes of 4 comparisons, which is 16, not 10.