CITS2200 - Lecture 3
Insertion Sort & Intro to Complexity - Slides
Insertion Sort
- Insertion sort sorts elements in an array into non-decreasing order
- Requires comparable elements in array (like binary sort)
- Very simple, but also very inefficient sort method
- Insertion sort method:
- Place two elements and such that they are sorted
- If or → do nothing
- If , swap and
- This repeats along the array until the entirety is sorted
Each element is compared with all elements along its left and is “inserted” into the correct place → hence insertion sort
- ! To insert an element into an array, we need to shift all the elements forward to “make room” → prevent overwriting data
- Worst case → requires time where is length of array
We don’t care about this since it is a lower power than the overall complexity of the sort method. This is known as an asymptotic complexity as it has a less than proportional impact on cost. - See Insertion - Slides
- Worst case → requires time where is length of array
def insertion_sort(A):
for k in range(1, len(A)):
cur = A[k]
j = k
while j > 0 and A[j-1] > cur:
A[j] = A[j-1]
j -= 1
A[j] = cur
return A- This sort method has complexity of
- This means that with array size , it takes steps to sort
- As we can see, insertion sort is very slow for large arrays
Complexity
is a constant used to represent operation cost in complexity
- It is known as big O notation
In the example above, represents the number of insertions, which we assume takes 1 unit of time to compute. But it actually takes more than that based on a number of varying factors Thus, we use to represent this varying operation cost.
In big O notation, we omit any constants that might be in front of . We only care about the degree of the polynomial of as that is what affects the growth rate the most.