SORTING ALGOS MEMORIZATION

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/53

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.

54 Terms

1
New cards

Core sorting rule

Every sorting algorithm answers: what part is already sorted, and what operation repeats

2
New cards

Binary search requirement

Array must be sorted before binary search can be used

3
New cards

Binary search mental model

Cut the search space in half each step

4
New cards

Binary search key variables

left, right, mid

5
New cards

Binary search loop condition

while (left <= right)

6
New cards

Binary search mid calculation snippet

int mid = left + (right - left) / 2;

7
New cards

Binary search comparison snippet

if (arr[mid] == target)

8
New cards

Binary search left move snippet

left = mid + 1;

9
New cards

Binary search right move snippet

right = mid - 1;

10
New cards

Binary search worst case time

O(log n)

11
New cards

Binary search best case time

O(1)

12
New cards

Binary search exam trap

Using binary search on an unsorted array

13
New cards

Selection sort mental model

Find the smallest element and move it to the front

14
New cards

Selection sort sorted region

Left side of the array grows sorted

15
New cards

Selection sort outer loop meaning

Controls the boundary of the sorted region

16
New cards

Selection sort min index snippet

int minIndex = i;

17
New cards

Selection sort comparison snippet

if (arr[j] < arr[minIndex]) minIndex = j;

18
New cards

Selection sort swap snippet

swap(arr[i], arr[minIndex]);

19
New cards

Selection sort swaps per pass

Exactly one swap per outer loop

20
New cards

Selection sort worst case time

O(n^2)

21
New cards

Selection sort best case time

O(n^2)

22
New cards

Selection sort exam trap

Assuming it becomes faster if array is already sorted

23
New cards

Insertion sort mental model

Sorting cards in your hand

24
New cards

Insertion sort sorted region

Everything to the left of the current index

25
New cards

Insertion sort outer loop start

i = 1 because first element is already sorted

26
New cards

Insertion sort swap-based while condition

while (j > 0 && arr[j] < arr[j-1])

27
New cards

Insertion sort swap snippet

swap(arr[j], arr[j-1]);

28
New cards

Insertion sort pointer movement snippet

j--;

29
New cards

Insertion sort best case time

O(n)

30
New cards

Insertion sort worst case time

O(n^2)

31
New cards

Insertion sort exam trap

Forgetting to move j after swap

32
New cards

Bubble sort mental model

Large values bubble to the right

33
New cards

Bubble sort sorted region

Right side of the array grows sorted

34
New cards

Bubble sort outer loop meaning

Reduces how far inner loop must go

35
New cards

Bubble sort neighbor comparison snippet

if (arr[j] > arr[j+1])

36
New cards

Bubble sort swap snippet

swap(arr[j], arr[j+1]);

37
New cards

Bubble sort optimized swap flag snippet

bool swapped = true;

38
New cards

Bubble sort early exit condition

sorted if no swaps occur in a pass

39
New cards

Bubble sort best case time

O(n) with swap check

40
New cards

Bubble sort worst case time

O(n^2)

41
New cards

Bubble sort exam trap

Claiming O(n) without swap optimization

42
New cards

Merge sort mental model

Split array until size 1, then merge back up

43
New cards

Merge sort base case snippet

if (left >= right) return;

44
New cards

Merge sort mid calculation snippet

int mid = left + (right - left) / 2;

45
New cards

Merge sort left recursion snippet

mergeSort(arr, left, mid);

46
New cards

Merge sort right recursion snippet

mergeSort(arr, mid + 1, right);

47
New cards

Merge sort merge call snippet

merge(arr, left, mid, right);

48
New cards

Merge sort worst case time

O(n log n)

49
New cards

Merge sort best case time

O(n log n)

50
New cards

Merge sort exam trap

Thinking merge sort is in-place

51
New cards

Tracing selection sort rule

Track minIndex each outer loop

52
New cards

Tracing insertion sort rule

Watch elements swap backward one position at a time

53
New cards

Tracing bubble sort rule

After each pass, the largest value is fixed at the end

54
New cards

Tracing merge sort rule