Sorting Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is the worst case and best case of selection sort?

A

Worst when array reverse sorted: N^2 /2 comparisons and N swaps
Best when array already sorted:
N^2/2 comparisons and 0 swaps.

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

What is the best and worst case of insertion sort?

A

Worst: reverse order arrayN^2 / 4 compares, N^2/2 exchanges
Best: Sorted array N - 1 compares and 0 exchanges

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

What is the best and worst case of mergesort?

A

Between 0.5N log N and N log N compares
at most 6N lg N array acccesses

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

How efficient is merge sort at random/sorted/reverse sorted/identical array?

A

Always has complexity of N log N

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

Why use random shuffling in quick sort and what are the issues with distinct keys?

A

Random shuffling is employed in QuickSort to prevent worst-case time complexity scenarios, particularly when the input array is already sorted or nearly sorted.

When input arrays contain duplicates, the partitioning process selects pivot elements that are the same as the majority of elements in the array. This makes partitioning highly unbalanced.

Poorly chosen pivots that do not divide array into equal halves, leading to unbalanced partitions. This leads to a higher probability of worst-case scenarios.

For example, if the pivot is always chosen as the first or last element and it is already sorted, Quick Sort will end up with unbalanced partitions leading to a time complexity of $N^2$.

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

How does one maintain the heap order of a binary tree?

A

The insert and delete operations must be able to move
a large element up the tree (swim) or move small elements to the bottom (sink), in order to maintain heap
order.

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

How does the data type/object to be sorted, influence the efficiency of the sorting algorithm?

A
  1. Comparison overhead: Efficiency of comparisons vary based on the complexity of operation for the data type.
  2. Data distribution: [[Quick Sort]] performs poorly on sorted data but excels on average or random
  3. Data Size
  4. Stability
  5. Memory requirements: [[Mergesort]] requires additional storage
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

why a heap-ordered binary tree has height ~lg N in all cases (why can it not have height N, like a normal binary tree that is created using sorted data?)

A
  1. Balanced Structure
  2. Complete Binary Tree Property: Amount of nodes are doubled after each parent.
  3. Heap Property: Number of comparisons needed to reach maximum node is logarithmic to the number of elements in the heap.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When should quick sort be used?

A

Large sets where average-case performance is acceptable and when data is not sorted.

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

When should merge sort be used?

A

Sorting large lists. Merge sort executes operational steps even if the initial list is already sorted.

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

What is heap sort’s time complexity?

A

Always $N \log N$.

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

What is the worst case for Heap Sort?

A

2N lg N + 2N

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

When is an array partially sorted?

A

If the number of in-versions < constant multiple of size.

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

How is a binary heap implemented using an array?

A
  1. Indexing using (2k, 2k+1, k/2) formulas
  2. Insertion added to end of array
  3. Deletion where maximum element is removed and replaced with last element. Compare new root with children.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the average case for quick sort?

A

2N ln N compares and 1/3 N ln N exchanges

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

What is the cost of partitioning in quick sort?

A

N + 1

16
Q
A