Searching & sorting

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

1/36

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.

37 Terms

1
New cards

What is linear search?

Starts at one end of the list and checks each element until either the target is found or the list ends

2
New cards

What is linear search best for?

Best for "small" lists (less than 50 elements)

3
New cards

What is the best case (efficiency) for linear search?

O(1)

4
New cards

What is the average case (efficiency) for linear search?

O(n) - average of n/2 comparisons for a list of n elements

5
New cards

What is the worse case (efficiency) for linear search?

Worst case: O(n)

6
New cards

Give a generic code for linear search

knowt flashcard image
7
New cards

What is the condition for binary search?

Only works on sorted lists

8
New cards

How does binary search work?

Divides the search interval in half repeatedly divide & conquer:

  • It determines which half of the list contains the search term.

  • It recursively checks that half and stops if item found or list empty.

9
New cards

What is the best case (efficiency) for binary search?

O(1)

10
New cards

What is the average case (efficiency) for binary search?

O(log n) - divides the interval by 2 each time: log₂n

11
New cards

What is the worst case (efficiency) for binary search?

O(log n)

12
New cards

Compare the efficiency between the linear search and binary search and when you wanna use which one

  • Binary search is faster than linear search, but only works on sorted lists.

  • Searching an unsorted list with linear sort is still faster than first sorting a list and then using binary search though

13
New cards

Which searching does dictionary used and what is the efficiency of it?

Python dictionaries use 'hash table' searching, which is always O(1)

14
New cards

What is selection sort?

Selects the smallest item in the list and swaps it with the first item, then repeats for the rest of the list until fully processed.

15
New cards

What is the space complexity of selection sort

Sorts "in place" - doesn't create a new list: Space complexity O(1).

16
New cards

What are the 3 cases (best, average and worse) for selection sort (time complexity)

knowt flashcard image
17
New cards

What is merge sort?

Divides the list into n sublists and repeatedly merges them producing sorted sublists until one sorted sublist remains

18
New cards

What should you do when drawing a merge sort tree?

When drawing the merge sort tree, show all individual splits and merges

19
New cards

What is the space complexity of the merge sort?

Not "in place" – creates a new list: Space complexity O(n).

20
New cards

What are the 3 cases (best, average and worse) for merge sort (time complexity)

knowt flashcard image
21
New cards

Who proposed the merge sort?

Proposed by von Neumann in 1945

22
New cards

Give a generic code for selection sort

knowt flashcard image
23
New cards

Give a generic code for merge sort

knowt flashcard image
24
New cards

Give a generic code for quick sort

knowt flashcard image
25
New cards

What is quick sort

Partitions the list based on a pivot value, then sorts each partition recursively

26
New cards

What is the space complexity of the quick sort

"in place" sort: Space complexity O(1)

27
New cards

What are the 3 cases (best, average and worse) for quick sort (time complexity)

knowt flashcard image
28
New cards

What is a Big-O notation?

An upper bound estimate on algorithm efficiency → it compares how the algorithm responds to changes in input size

29
New cards

Is Big O notation exact?

Nope

30
New cards

How do you simplify the Big O notation

knowt flashcard image
31
New cards

What are the common time complexity

knowt flashcard image
32
New cards

What do we mean by a stable sort?

A "stable sort" is one where the relative positions of items with the same key do not change

33
New cards

What can the efficicy class tell you about the in

knowt flashcard image
34
New cards

Compare the advantages of each sorting algorithm

knowt flashcard image
35
New cards

Give generic code for binary search

knowt flashcard image
36
New cards

Give generic code for linear search

knowt flashcard image
37
New cards

Give generic code for selection sort

knowt flashcard image