Sorting Algorithms Flashcards

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

Quick Sort Time & Space Complexity

A

Quick Sort (Divide & Conquer)

  • Time Complexity
    • Best: O(n log n)
    • AVG: O(n log n)
    • Worst: O(n^2) - when the pivot is always the smallest or largest element
  • Space Complexity
    • O(log n) - due to the recursion stack
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Merge Sort Time & Space Complexity

A

Merge Sort (Divide & Conquer)

  • Time Complexity
    • Best: O (n log n)
    • AVG: O (n log n)
    • Worst: O (n log n) - same for all cases
  • Space Complexity
    • O(n) - for temporary arrays used during the merge process
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Heap Sort Time & Space Complexity

A
  • Time Complexity
    • Best: O (n log n)
    • AVG: O (n log n)
    • Worst: O (n log n)
  • Space Complexity
    • O(n) for heapq (min-heap, push/pop)
    • O (1) (only for manual)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When to use Merge Sort

A

large datasets

when stability is needed (not OK if equal elements are moved around after sorting)

linked lists

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

When not to use merge sort

A

small datasets (overhead isnt worth it. overhead = processing power, memory)

memory constrained environments

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

Strengths of Merge Sort

A

Stable sorting (equal objects won’t move around places)

Guaranteed O(n log n) time complexity

good for linked lists

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

weaknesses of merge sort

A

O(n) space complexity (requires extra memory)

slower than quicksort on average

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

when to use quicksort

A

small to medium datasets

when average case performance matters

in-place sorting desired (modifies input data directly, spatially efficient)

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

when not to use quicksort

A

worst-case performance is bad (already sorted data)

when stability is needed (not OK with it moving around values of equal value)

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

strengths of quick sort

A

average case O(n log n) time

in place sorting: O(log n) space

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

weaknesses of quick sort

A

worst case O(n^2) time complexity

not stable

recursive depth can lead to stack overflow on large datasets

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

when to use heap sort

A

when guaranteed O(n log n) performance is needed

memory constrained environments

steaming data (continuous data flow)

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

when not to use heap sort

A

for small datasets (often slower than other algorithms)

when stability is required (no moving around same value objects)

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

strengths of heapsort

A

guaranteed O(n log n) time complexity

useful for priority queues

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

weaknesses of heap sort

A

not stable

slow than quicksort on average

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

explain merge sort

A

Merge sort breaks the data up into halves over and over until every data point is isolated down like a tree.

Then it rebuilds up by sorting each node of the tree one step at a time

17
Q

explain quick sort

A

Quick sort uses a recurring pivot splitting the dataset into halves over and over before reassembling the sorted halves

18
Q

explain heap sort

A

Heap (max heap) method involves creating a heap version of the array.

It extracts the root (max value in the heap) and replaces it with the last element of the real array.

19
Q

Timsort

A

used in Python’s built in sort() and sorted() functions. It’s a hybrid sorting algorithm that combines merge sort and insertion sort. Can handle semi sorted and unsorted data.

20
Q

Is Timesort stable?

A

Timsort is a stable sort, meaning it preserves the relative order of equal elements

21
Q

Difference between sort() and sorted()

A

Sort is a method that’s used on lists (modifies the original list in place)

Sorted is a method that can be used on any iterable object (tuples, lists, strings, etc). It creates a new sorted list without modifying the OG iterable.

22
Q

Time complexity of Timsort

A

Best: O(n) - when already sorted (timsort detects this)
AVG Case: O(n log n) - timsort behaves like merge sort
Worst Case: O(n log n)

23
Q

Space complexity of Timsort

A

O(n) - requires space for temporary storage during merging phase

    - Sorted () is actually O(2n) but it’s O(n) in space complexity representation. Sorted uses more space than Sort( ) as it creates a new object.