Lab Demo#2 Flashcards

(37 cards)

1
Q

What makes a sorting algorithm stable?

A

-if it preserves the order of duplicate keys

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

How can a sorting algorithm be made stable?

A

-can be made stable by considering the “index” as a comparison parameter

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

Is bubble sort stable or unstable?

A

Stable

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

is insertion sort stable or unstable?

A

Stable

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

Is selection sort stable or unstable?

A

Unstable
-but it can be coded to be stable

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

Is shell sort stable or unstable?

A

-unstable

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

List the advanced sorting algorithms

A

-Merge Sort
-Quick Sort
-Radix Sort/Bucket Sort
-Heap Sort

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

Is merge sort stable or unstable?

A

-Stable

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

-Is quick sort stable or unstable?

A

-Unstable
can be made stable using the Partition Algorithm

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

Is Radix Sort/Bucket Sort stable or unstable?

A

-Unstable

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

Is Heap Sort stable or unstable?

A

Unstable

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

What is the best time complexity of Bubble Sort?

A

O(n)

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

What is the worst time complexity of Bubble Sort?

A

O(n^2)

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

What is “natural order”?

A

It is the default order in which elements are arranged when sorted
ex. integers are sorted in ascending numerical order
ex.

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

How to define natural order of a class?

A

by implementing the Comparable<> interface

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

What is “order by comparator”?

A

sorting based on custom order defined by a Comparator<T> implementation
-differs from natural order</T>

17
Q

How do you implement “order by comparator”?

A
  1. Implement the comparator<T> interface</T>
  2. Override the compare(T o1, T o2) method
18
Q

What do you need to do to sort instantiated objects of a class MyClass using natural order?

A
  1. Implement the Comparable<MyClass> interface in MyClass</MyClass>
  2. Override the compareTo(MyClass other) to define the natural order
19
Q

What is the stability of a sorting algorithm?

A

Depends on how it treats equal elements in a dataset
-its stable if it preserves the relative order of equal elements in a dataset

20
Q

How is the stability of a sorting algorithm ensured?

A

Ensured by:
1. comparing elements carefully
2. avoid unnecessary swaps of equal elements

21
Q

Can any sorting algorithm be made stable?

A

Yes
-but you would need to add extra information to the data or change how comparisons and swaps are performed

22
Q

What are the inherently stable sorting algorithms?

A

-Bubble Sort
-Insertion Sort
-Merge Sort

23
Q

Can Radix sort be used to sort the instantiated objects of a class called MyClass?

A

Yes
-only if the sorting criteria for MyClass can be broken into keys that Radix Sort can handle (like integers and strings)

24
Q

If you sort an array with the same dataset, same algorithm, and same machine twice, will the execution time be the same?

A

The execution time will slightly vary
-for an example, a randomly selected pivot in a Quick Sort

25
How have you picked the pivot in the Quick Sort algorithm? What is the justification?
Lab 6, but common pivot positions are: -First element -Last element -Median of the first, middle, and last elements -random element
26
What happens if Quick Sort is called on an already sorted array?
-The partitions would be highly unbalanced if the first or last element is chosen (worst time complexity = On^2
27
What is the space complexity of a Quick Sort being called on already sorted array?
O(n) as recursive calls add overhead
28
Modify any algorithm to do the sorting in descending order
Practice using Bubble Sort
29
Generate a random number between min and max inclusive using Math.random().
Practice int randomNumber = (int)(Math.random() * (max - min + 1)) + min;
30
For a small dataset (50 elements), which algorithm would you choose and why?
Insertion Sort -low overhead -has a best time complexity of On Bubble Sort would also be good
31
If you create a class MyClass that implements Comparable interface, would you prefer choosing primitive datatype (when needed) over Wrapper class object reference. What is the basis of your response?
Primitives are usually more memory efficient and faster -However, if I wanted to use ArrayList and Collection.sort(), I would need to use wrapper classes
32
If you would like to do Bucket-search Strings that use alphabetical characters, how many buckets would you need to accomplish this? Why?
26 buckets for each letter -for ex. all names with A go into the A bucket
33
What would be your preference between Merge and quick sort? Why?
small datasets where stability is required = Merge Sort. large datasets with no stability requirement = Quick Sort due to its faster average-case performance.
34
What are the advantages of Merge Sort?
-Stable -Time complexity is always Onlogn -really good for linked lists
35
What are the disadvantages of Merge Sort?
-requires additional space for merging -slower for small datasets due to overhead
36
What are the advantages of Quick Sort?
-It is in-place with O(logn) O(logn) space complexity. -Generally faster in practice due to fewer memory operations. -Can be optimized using pivot selection techniques.
37
If you are sorting integer data set, what would be your sorting algorithm choice? Why?
-Radix Sort: If integers have a limited range and dataset size is large. -Quick Sort: For general-purpose sorting with in-place requirements.