Search and Sort Algorithm Big O Notation

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/14

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

15 Terms

1
New cards

Linear Search

  • Best Case: O(1) — The target element is the first element.

  • Average Case: O(n) — The target element is somewhere in the middle or not in the array.

  • Worst Case: O(n) — The target element is the last element or not present.

2
New cards

Binary Search

  • Best Case: O(1) — The target element is the middle element.

  • Average Case: O(log ⁡n) — The target element is not immediately found but within the sorted array.

  • Worst Case: O(log⁡ n) — The target element is at the extreme ends or not present.

3
New cards

Interpolation Search

  • Best Case: O(1) — The target element is exactly where the interpolation suggests.

  • Average Case: O(log ⁡log⁡ n) — Uniformly distributed data.

  • Worst Case: O(n) — Highly skewed data distribution or worst interpolation.

4
New cards

Depth-First Search (DFS)

  • Best Case: O(1) — The target node is found immediately.

  • Average Case: O(V+E)— Typically when all nodes and edges must be explored.

  • Worst Case: O(V+E) — The target node is the last one discovered.

5
New cards

Breadth-First Search (BFS)

  • Best Case: O(1) — The target node is the root or the first node checked.

  • Average Case: O(V+E) — All nodes and edges need to be explored.

  • Worst Case: O(V+E) — The target node is the last one explored.

6
New cards

Bubble Sort

  • Best Case: O(n) — The array is already sorted (with an optimized version that stops early).

  • Average Case: O(n2) — Average case with random elements.

  • Worst Case: O(n2) — The array is sorted in reverse order.

7
New cards

Selection Sort

  • Best Case: O(n2) — Selection sort does not improve with better input, always O(n2).

  • Average Case: O(n2) — Average case with random elements.

  • Worst Case: O(n2) — Selection sort is insensitive to input order.

8
New cards

Insertion Sort

  • Best Case: O(n) — The array is already sorted.

  • Average Case: O(n2) — Average case with random elements.

  • Worst Case: O(n2) — The array is sorted in reverse order.

9
New cards

Merge Sort

  • Best Case: O(n log⁡ n) — time complexity is the same in all cases.

  • Average Case: O(n log⁡ n).

  • Worst Case: O(n log⁡ n).

10
New cards

Quicksort

  • Best Case: O(n log⁡ n) — The pivot splits the array into two nearly equal halves.

  • Average Case: O(n log⁡ n) — Average case with random pivots.

  • Worst Case: O(n2) — The pivot is always the smallest or largest element, leading to unbalanced partitions.

11
New cards

Heap Sort

  • Best Case: O(n log⁡ n) — time complexity is the same in all cases.

  • Average Case: O(n log⁡ n).

  • Worst Case: O(n log⁡ n).

12
New cards

Counting Sort

  • Best Case: O(n+k)— k is the range of the input.

  • Average Case: O(n+k).

  • Worst Case: O(n+k).

13
New cards

Radix Sort

  • Best Case: O(n*k) — k is the number of digits in the largest number.

  • Average Case: O(n*k).

  • Worst Case: O(n*k).

14
New cards

Bucket Sort

  • Best Case: O(n+k) — k is the number of buckets; assumes uniform distribution.

  • Average Case: O(n+k).

  • Worst Case: O(n2) — All elements end up in one bucket (degenerate case).

15
New cards

Shell Sort

  • Best Case: O(n log n) — Occurs when the array is already sorted or nearly sorted, especially when using a good gap sequence like the Knuth sequence.

  • Average Case: O(n^(3/2)) or O(n^1.5) — Highly dependent on the gap sequence used. With commonly used sequences like the Knuth sequence, the average-case complexity is approximately O(n^1.5).

  • Worst Case: O(n2) — Can degrade to O(n2), particularly with poorly chosen gap sequences like the original Shell sequence (where the gaps are halved each time).