week 4 - What is sorting? - Naive Sorting Flashcards

1
Q

What is sorting?

A

Sorting is the process of placing elements from a collection in some kind of order.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the connection between sorting and ordering?

A

Central to sorting is the concept of ordering: it must be possible to say whether, of any two values, one is larger or smaller than the other.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Two different units of computation are commonly considered when measuring the complexity of a sorting algorithm. What are they?

A

a) Comparison of one item with another (whether this one is larger or smaller than that one) is the most common unit of computation used in measuring the complexity of a sorting algorithm.
b) If, during sorting, two values are found to be out of order, they are swapped. This can be expensive computationally, and so a second possible unit of computation is the swap: exchanging the positions of two items.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What the devil is a bubble sort?

A

Each pair of a list is compared and they are swapped if necessary, the ‘largest’ term bubbles to the ‘top’ or ‘end’ of the list after each pass, so that n-1 passes are necessary to complete a bubble swap.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a short bubble?

A

A bubble pass has the benefit that it can detect when a list has been sorted early, it can therefore stop prematurely.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the Big O Notation for a bubble swap, why?

A

it is n^2, this is because the sum of the first n integers of a list is 0.5(n^2) + 0.5(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In what respects are bubble sort and selection sort similar?

A

Both algorithms make multiple passes over the sequence being processed, accumulating sorted items at the end of the sequence.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is selection sort?

A

Selection sort iterates through alist n-1 times, each time finding the largest item in the list and moving it to the ‘next fill spot’ initially the last item in the list which then moves down 1 with each iteration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does the inner loop processing carried out by selection sort differ from that in bubble sort?

A

Bubble sort bubbles larger items up towards the end of the sequence in a series of comparisons and swaps within the unsorted portion. Selection sort processes the whole unsorted portion to find the largest item and then moves it into place in a single swap.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the key similarities and differences between insertion sort and the two algorithms we’ve discussed: bubble sort and selection sort?

A

Insertion sort, like bubble sort and selection sort, involves two nested loops. It also processes a partition of the sequence that diminishes in size by 1 on each iteration of the outer loop. By contrast, though, the inner loop takes the first item of the unsorted part of the list. It then successively shifts items in the sorted part to the right until it finds the correct place for the item from the unsorted part and inserts it there. Sorted items thus accumulate at the beginning of the sequence. The use of shifts instead of exchanges also means that fewer assignments are required, somewhat reducing the number of processing steps. Insertion sort does well on benchmark studies, though it has the same worst-case performance of O(n2) as bubble sort and selection sort

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is in-place sorting?

A

no other list needs to be created; the items are moved around within the original list. all of them depend on two loops, one embedded inside the other.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly