Week 4 Flashcards

1
Q

What are the three simple algorithms?

A

Bubble sort, selection sort, insertion sort.

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

What are the two complex sorting algorithms.

A

Merge sort and quick sort.

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

What is the complexity for the best case of insertion sort?

A

The best case complexity is O(n)

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

What is the complexity for the average and worst case for insertion sort.

A

The worst case is O(n^2) and the average case is O(n^2).

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

What is the theoretical best running time we can achieve for a comparision based sort?

A

O(n log(n)) for the worst and average cases.

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

What is Quick Sort?

A

It is another divide and conquer based technique.

Choose one array element to be the pivot and partitions the array into 2 sub arrays such that,
* subarray 1 contains only elements less than or equal to the pivot
* Sub array 2 contains only elements greater than or equal to the pivot
* The pivot stays in place and is in its final position.

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

For quick sort, why is it inefficient if the pivot is the first or last element?

A

The algorithm will fare poorly when the array is nearly in sorted order.

Try to choose pivots that divide the array into nearly equal halves.

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

During quick sort what happens when we exchange the first or the last element?

A

During partitioning, the pivot is sometimes moved out of the way by exchanging with the first or last element.

To avoid index bound checks, the largest element is put into the last array position. This is done before the actual sort.

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

In quick sort do we care if we choose the partition as the first, last or middle? (Assume the data is random).

A

No since the data is random, the choice won’t matter.

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

When does the worst case occur in quick sort?

A

It occurs when the pivot is the largest or the smallest element.

Results in a sub array of size 0 and another of size n -1.

Complexity of O(n^2)

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

When does the best case occur for quick sort?

A

The best case occurs when the pivots always create equal sized subarrays. The complexity is O(nlog(n))

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

For quick sort what is the average case complexity?

A

The average case complexity is O(nlog(n)).

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

What are the advantages of quick sort vs merge sort?

A

Quick sort is normally the quickest algorthm, (unless the pivot is chosen as the smallest or largest).

Unlike merge sort, quick sort does not need extra memory.

Quick sort is not appropriate for an array that is less than 30 items. (In this case an insertion sort is better).

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

What is an internal sort?

A

Data is kept in primary memory.

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

What is an external sort?

A

Data is kept in secondary storage. Used when data is too large to be loaded to memory at once.

Not all elements are accessible.

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

What is an inplace sort.

A

A sort achieved by exchanging items in place within an array.

Does not use extra memory.

17
Q

What is a stable sort?

A

A stable sort is a sort that preserves the order of equal keys.

This may be important if there are duplicate key values.

18
Q

What do we consider for sorting

A

Comparisons

Data movements, exchanges or swaps.

19
Q

What is a bubble sort?

A

Compares successive pairs of items
* The largest item bubbles up to the end
* The next largest bubbles up, etc.

20
Q

True or false, in a bubble sort, the number of comparisons is the same for best, average and worst cases.

A

This is true.

21
Q

What is the complexity for a bubble sort?

A

It is O(n^2) regardless of input order.

22
Q

What is a selection sort?

A

Works by selecting the smallest item above the current item in the array and swaps them.

This is repeated up to the second last item.

23
Q

True or false, for a selection sort, the number of comparisions is the same for the best, average and worst cases.

A

This is true.

24
Q

What is an insertion sort?

A

Starts from the 2nd element of the sequence, and compares the item to the left.

If the current is higher, it does nothing, but if it is less, it traverses backwards to find the correct position.

25
Q

What is the complexity for a selection sort?

A

The worst case is O(n^2).
The best case is when it is already sorted: no index update.
The average case is O(n^2)

26
Q

Is a selection sort stable?

A

No, it is unstable.

27
Q
A