Lab Demo#2 Flashcards
(37 cards)
What makes a sorting algorithm stable?
-if it preserves the order of duplicate keys
How can a sorting algorithm be made stable?
-can be made stable by considering the “index” as a comparison parameter
Is bubble sort stable or unstable?
Stable
is insertion sort stable or unstable?
Stable
Is selection sort stable or unstable?
Unstable
-but it can be coded to be stable
Is shell sort stable or unstable?
-unstable
List the advanced sorting algorithms
-Merge Sort
-Quick Sort
-Radix Sort/Bucket Sort
-Heap Sort
Is merge sort stable or unstable?
-Stable
-Is quick sort stable or unstable?
-Unstable
can be made stable using the Partition Algorithm
Is Radix Sort/Bucket Sort stable or unstable?
-Unstable
Is Heap Sort stable or unstable?
Unstable
What is the best time complexity of Bubble Sort?
O(n)
What is the worst time complexity of Bubble Sort?
O(n^2)
What is “natural order”?
It is the default order in which elements are arranged when sorted
ex. integers are sorted in ascending numerical order
ex.
How to define natural order of a class?
by implementing the Comparable<> interface
What is “order by comparator”?
sorting based on custom order defined by a Comparator<T> implementation
-differs from natural order</T>
How do you implement “order by comparator”?
- Implement the comparator<T> interface</T>
- Override the compare(T o1, T o2) method
What do you need to do to sort instantiated objects of a class MyClass using natural order?
- Implement the Comparable<MyClass> interface in MyClass</MyClass>
- Override the compareTo(MyClass other) to define the natural order
What is the stability of a sorting algorithm?
Depends on how it treats equal elements in a dataset
-its stable if it preserves the relative order of equal elements in a dataset
How is the stability of a sorting algorithm ensured?
Ensured by:
1. comparing elements carefully
2. avoid unnecessary swaps of equal elements
Can any sorting algorithm be made stable?
Yes
-but you would need to add extra information to the data or change how comparisons and swaps are performed
What are the inherently stable sorting algorithms?
-Bubble Sort
-Insertion Sort
-Merge Sort
Can Radix sort be used to sort the instantiated objects of a class called MyClass?
Yes
-only if the sorting criteria for MyClass can be broken into keys that Radix Sort can handle (like integers and strings)
If you sort an array with the same dataset, same algorithm, and same machine twice, will the execution time be the same?
The execution time will slightly vary
-for an example, a randomly selected pivot in a Quick Sort